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 f5d15950d055..8755cb86dafe 100644 --- a/framework-docs/modules/ROOT/pages/data-access/jdbc/core.adoc +++ b/framework-docs/modules/ROOT/pages/data-access/jdbc/core.adoc @@ -352,40 +352,9 @@ A common practice when using the `JdbcTemplate` class (and the associated xref:data-access/jdbc/core.adoc#jdbc-NamedParameterJdbcTemplate[`NamedParameterJdbcTemplate`] class) is to configure a `DataSource` in your Spring configuration file and then dependency-inject that shared `DataSource` bean into your DAO classes. The `JdbcTemplate` is created in -the setter for the `DataSource`. This leads to DAOs that resemble the following: +the setter for the `DataSource` or in the constructor. This leads to DAOs that resemble the following: --- -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes",role="primary"] ----- - 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... - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] ----- - class JdbcCorporateEventDao(dataSource: DataSource) : CorporateEventDao { - - private val jdbcTemplate = JdbcTemplate(dataSource) - - // JDBC-backed implementations of the methods on the CorporateEventDao follow... - } ----- -====== --- +include-code::./JdbcCorporateEventDao[tag=snippet,indent=0] The following example shows the corresponding configuration: 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 index 0fc58e687816..36ab9956b738 100644 --- 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 @@ -20,13 +20,15 @@ import org.springframework.jdbc.core.JdbcTemplate; +// tag::snippet[] public class JdbcCorporateEventDao implements CorporateEventDao { - private JdbcTemplate jdbcTemplate; + private final JdbcTemplate jdbcTemplate; - public void setDataSource(DataSource dataSource) { + public JdbcCorporateEventDao(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } // JDBC-backed implementations of the methods on the CorporateEventDao follow... } +// end::snippet[] \ No newline at end of file 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 index 7ec92bf05678..799b81835584 100644 --- 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 @@ -13,7 +13,7 @@ public class JdbcCorporateEventDaoConfiguration { // tag::snippet[] @Bean JdbcCorporateEventDao corporateEventDao(DataSource dataSource) { - return new JdbcCorporateEventDao(); + return new JdbcCorporateEventDao(dataSource); } @Bean(destroyMethod = "close") 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 index 483488a72925..6a95d3f67e52 100644 --- 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 @@ -8,7 +8,7 @@ // tag::snippet[] @Configuration -@ComponentScan("org.springframework.docs.dataaccess.jdbc") +@ComponentScan("org.example.jdbc") public class JdbcCorporateEventRepositoryConfiguration { @Bean(destroyMethod = "close") diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcjdbctemplateidioms/JdbcCorporateEventDao.kt b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcjdbctemplateidioms/JdbcCorporateEventDao.kt new file mode 100644 index 000000000000..0591b7c2765a --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcjdbctemplateidioms/JdbcCorporateEventDao.kt @@ -0,0 +1,28 @@ +/* + * 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.springframework.jdbc.core.JdbcTemplate +import javax.sql.DataSource + +// tag::snippet[] +class JdbcCorporateEventDao(dataSource: DataSource): CorporateEventDao { + + private val jdbcTemplate = JdbcTemplate(dataSource) + + // JDBC-backed implementations of the methods on the CorporateEventDao follow... +} +// end::snippet[] \ No newline at end of file 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 index 08d8a8abe320..ceb82cc7e0c7 100644 --- 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 @@ -26,7 +26,7 @@ class JdbcCorporateEventDaoConfiguration { // tag::snippet[] @Bean - fun corporateEventDao(dataSource: DataSource) = JdbcCorporateEventDao() + fun corporateEventDao(dataSource: DataSource) = JdbcCorporateEventDao(dataSource) @Bean(destroyMethod = "close") fun dataSource() = BasicDataSource().apply { 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 index 6de238af10df..0c3e8c11a079 100644 --- 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 @@ -23,7 +23,7 @@ import org.springframework.context.annotation.Configuration // tag::snippet[] @Configuration -@ComponentScan("org.springframework.docs.dataaccess.jdbc") +@ComponentScan("org.example.jdbc") class JdbcCorporateEventRepositoryConfiguration { @Bean(destroyMethod = "close") 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 index 44ce2bf339ef..1feeebf5334b 100644 --- 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 @@ -9,8 +9,8 @@ https://www.springframework.org/schema/context/spring-context.xsd"> - - + + 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 index 1031086aa625..5f92e7ca5655 100644 --- 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 @@ -10,7 +10,7 @@ - +