From 39889744b0d052bc09aa8b6b10ab5f760bb27728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 6 May 2024 18:28:36 +0200 Subject: [PATCH] Polish JdbcTemplate Best Practices section --- .../ROOT/pages/data-access/jdbc/core.adoc | 35 ++----------------- .../JdbcCorporateEventDao.java | 6 ++-- .../JdbcCorporateEventDaoConfiguration.java | 2 +- ...CorporateEventRepositoryConfiguration.java | 2 +- .../JdbcCorporateEventDao.kt | 28 +++++++++++++++ .../JdbcCorporateEventDaoConfiguration.kt | 2 +- ...bcCorporateEventRepositoryConfiguration.kt | 2 +- .../JdbcCorporateEventDaoConfiguration.xml | 4 +-- ...cCorporateEventRepositoryConfiguration.xml | 2 +- 9 files changed, 41 insertions(+), 42 deletions(-) create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/jdbc/jdbcjdbctemplateidioms/JdbcCorporateEventDao.kt 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 f5d15950d05..8755cb86daf 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 0fc58e68781..36ab9956b73 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 javax.sql.DataSource; 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 7ec92bf0567..799b8183558 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 483488a7292..6a95d3f67e5 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 @@ import org.springframework.context.annotation.Configuration; // 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 00000000000..0591b7c2765 --- /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 08d8a8abe32..ceb82cc7e0c 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 6de238af10d..0c3e8c11a07 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 44ce2bf339e..1feeebf5334 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 1031086aa62..5f92e7ca565 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 @@ - +