From d2e55a20383912d7f55254d6f93960ffa8e64592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Fri, 8 Mar 2024 12:08:37 +0100 Subject: [PATCH] Use code includes and tabs in jdbc/core.adoc See gh-22171 --- .../ROOT/pages/data-access/jdbc/core.adoc | 105 +----------------- .../CorporateEventDao.java | 20 ++++ .../CorporateEventRepository.java | 20 ++++ .../JdbcCorporateEventDao.java | 32 ++++++ .../JdbcCorporateEventDaoConfiguration.java | 30 +++++ .../JdbcCorporateEventRepository.java | 37 ++++++ ...CorporateEventRepositoryConfiguration.java | 25 +++++ .../JdbcCorporateEventDaoConfiguration.kt | 41 +++++++ ...bcCorporateEventRepositoryConfiguration.kt | 38 +++++++ .../JdbcCorporateEventDaoConfiguration.xml | 26 +++++ ...cCorporateEventRepositoryConfiguration.xml | 25 +++++ 11 files changed, 300 insertions(+), 99 deletions(-) create mode 100644 framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/CorporateEventDao.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/CorporateEventRepository.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDao.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepository.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.java create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.kt create mode 100644 framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.xml create mode 100644 framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.xml diff --git a/framework-docs/modules/ROOT/pages/data-access/jdbc/core.adoc b/framework-docs/modules/ROOT/pages/data-access/jdbc/core.adoc index ce2aa6f47f7..c8e927deacb 100644 --- a/framework-docs/modules/ROOT/pages/data-access/jdbc/core.adoc +++ b/framework-docs/modules/ROOT/pages/data-access/jdbc/core.adoc @@ -387,112 +387,19 @@ Kotlin:: ====== -- -The following example shows the corresponding XML configuration: +The following example shows the corresponding configuration: -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - - - - - - - - - ----- +include-code::./JdbcCorporateEventDaoConfiguration[tag=snippet,indent=0] An alternative to explicit configuration is to use component-scanning and annotation support for dependency injection. In this case, you can annotate the class with `@Repository` -(which makes it a candidate for component-scanning) and annotate the `DataSource` setter -method with `@Autowired`. The following example shows how to do so: +(which makes it a candidate for component-scanning). The following example shows how to do so: --- -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes",role="primary"] ----- - @Repository // <1> - public class JdbcCorporateEventDao implements CorporateEventDao { +include-code::./JdbcCorporateEventRepository[tag=snippet,indent=0] - private JdbcTemplate jdbcTemplate; +The following example shows the corresponding configuration: - @Autowired // <2> - public void setDataSource(DataSource dataSource) { - this.jdbcTemplate = new JdbcTemplate(dataSource); // <3> - } - - // JDBC-backed implementations of the methods on the CorporateEventDao follow... - } ----- -<1> Annotate the class with `@Repository`. -<2> Annotate the `DataSource` setter method with `@Autowired`. -<3> Create a new `JdbcTemplate` with the `DataSource`. - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] ----- - @Repository // <1> - class JdbcCorporateEventDao(dataSource: DataSource) : CorporateEventDao { // <2> - - private val jdbcTemplate = JdbcTemplate(dataSource) // <3> - - // JDBC-backed implementations of the methods on the CorporateEventDao follow... - } ----- -<1> Annotate the class with `@Repository`. -<2> Constructor injection of the `DataSource`. -<3> Create a new `JdbcTemplate` with the `DataSource`. -====== --- - - -The following example shows the corresponding XML configuration: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - - - - - - - - - - - - - ----- +include-code::./JdbcCorporateEventRepositoryConfiguration[tag=snippet,indent=0] If you use Spring's `JdbcDaoSupport` class and your various JDBC-backed DAO classes extend from it, your sub-class inherits a `setDataSource(..)` method from the diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/CorporateEventDao.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/CorporateEventDao.java new file mode 100644 index 00000000000..275ca463fcf --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/CorporateEventDao.java @@ -0,0 +1,20 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; + +public interface CorporateEventDao { +} diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/CorporateEventRepository.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/CorporateEventRepository.java new file mode 100644 index 00000000000..d92db6627bb --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/CorporateEventRepository.java @@ -0,0 +1,20 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; + +public interface CorporateEventRepository { +} diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDao.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDao.java new file mode 100644 index 00000000000..621e4fa777c --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDao.java @@ -0,0 +1,32 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; + +import javax.sql.DataSource; + +import org.springframework.jdbc.core.JdbcTemplate; + +public class JdbcCorporateEventDao implements CorporateEventDao { + + private JdbcTemplate jdbcTemplate; + + public void setDataSource(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + // JDBC-backed implementations of the methods on the CorporateEventDao follow... +} diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.java new file mode 100644 index 00000000000..3b88198c8f6 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.java @@ -0,0 +1,30 @@ +package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; + +import javax.sql.DataSource; + +import org.apache.commons.dbcp2.BasicDataSource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class JdbcCorporateEventDaoConfiguration { + + // tag::snippet[] + @Bean + JdbcCorporateEventDao corporateEventDao(DataSource dataSource) { + return new JdbcCorporateEventDao(); + } + + @Bean(destroyMethod = "close") + BasicDataSource dataSource() { + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); + dataSource.setUrl("jdbc:hsqldb:hsql://localhost:"); + dataSource.setUsername("sa"); + dataSource.setPassword(""); + return dataSource; + } + // end::snippet[] + +} diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepository.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepository.java new file mode 100644 index 00000000000..f73b1ee55a6 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepository.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; + +import javax.sql.DataSource; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; + +// tag::snippet[] +@Repository +public class JdbcCorporateEventRepository implements CorporateEventRepository { + + private JdbcTemplate jdbcTemplate; + + // Implicitly autowire the DataSource constructor parameter + public JdbcCorporateEventRepository(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + // JDBC-backed implementations of the methods on the CorporateEventRepository follow... +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.java new file mode 100644 index 00000000000..a3a79041274 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.java @@ -0,0 +1,25 @@ +package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms; + +import org.apache.commons.dbcp2.BasicDataSource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +// tag::snippet[] +@Configuration +@ComponentScan("org.springframework.docs.dataaccess.jdbc") +public class JdbcCorporateEventRepositoryConfiguration { + + @Bean(destroyMethod = "close") + BasicDataSource dataSource() { + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); + dataSource.setUrl("jdbc:hsqldb:hsql://localhost:"); + dataSource.setUsername("sa"); + dataSource.setPassword(""); + return dataSource; + } + +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.kt new file mode 100644 index 00000000000..06d7cde5d47 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms + +import org.apache.commons.dbcp2.BasicDataSource +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.docs.dataaccess.jdbc.JdbcCorporateEventDao +import javax.sql.DataSource + +@Configuration +class JdbcCorporateEventDaoConfiguration { + + // tag::snippet[] + @Bean + fun corporateEventDao(dataSource: DataSource) = JdbcCorporateEventDao() + + @Bean(destroyMethod = "close") + fun dataSource() = BasicDataSource().apply { + driverClassName = "org.hsqldb.jdbcDriver" + url = "jdbc:hsqldb:hsql://localhost:" + username = "sa" + password = "" + } + // end::snippet[] + +} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.kt new file mode 100644 index 00000000000..3b5fb0954a8 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms + +import org.apache.commons.dbcp2.BasicDataSource +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.ComponentScan +import org.springframework.context.annotation.Configuration + +// tag::snippet[] +@Configuration +@ComponentScan("org.springframework.docs.dataaccess.jdbc") +class JdbcCorporateEventRepositoryConfiguration { + + @Bean(destroyMethod = "close") + fun dataSource() = BasicDataSource().apply { + driverClassName = "org.hsqldb.jdbcDriver" + url = "jdbc:hsqldb:hsql://localhost:" + username = "sa" + password = "" + } + +} +// end::snippet[] diff --git a/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.xml new file mode 100644 index 00000000000..1a842fa95b6 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventDaoConfiguration.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.xml new file mode 100644 index 00000000000..2a66a431a00 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/jdbc/jdbcJdbcTemplateidioms/JdbcCorporateEventRepositoryConfiguration.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + +