Skip to content

Commit

Permalink
Polish JdbcTemplate Best Practices section
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed May 6, 2024
1 parent 12272d6 commit 3988974
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 42 deletions.
35 changes: 2 additions & 33 deletions framework-docs/modules/ROOT/pages/data-access/jdbc/core.adoc
Expand Up @@ -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:

Expand Down
Expand Up @@ -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[]
Expand Up @@ -13,7 +13,7 @@ public class JdbcCorporateEventDaoConfiguration {
// tag::snippet[]
@Bean
JdbcCorporateEventDao corporateEventDao(DataSource dataSource) {
return new JdbcCorporateEventDao();
return new JdbcCorporateEventDao(dataSource);
}

@Bean(destroyMethod = "close")
Expand Down
Expand Up @@ -8,7 +8,7 @@

// tag::snippet[]
@Configuration
@ComponentScan("org.springframework.docs.dataaccess.jdbc")
@ComponentScan("org.example.jdbc")
public class JdbcCorporateEventRepositoryConfiguration {

@Bean(destroyMethod = "close")
Expand Down
@@ -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[]
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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")
Expand Down
Expand Up @@ -9,8 +9,8 @@
https://www.springframework.org/schema/context/spring-context.xsd">

<!-- tag::snippet[] -->
<bean id="corporateEventDao" class="org.springframework.docs.dataaccess.jdbc.jdbcjdbctemplateidioms.JdbcCorporateEventDao">
<property name="dataSource" ref="dataSource"/>
<bean id="corporateEventDao" class="org.example.jdbc.JdbcCorporateEventDao">
<constructor-arg ref="dataSource"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
Expand Down
Expand Up @@ -10,7 +10,7 @@

<!-- tag::snippet[] -->
<!-- Scans within the base package of the application for @Component classes to configure as beans -->
<context:component-scan base-package="org.springframework.docs.dataaccess.jdbc.jdbcjdbctemplateidioms" />
<context:component-scan base-package="org.example.jdbc" />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
Expand Down

0 comments on commit 3988974

Please sign in to comment.