Polish JdbcTemplate Best Practices section
This commit is contained in:
parent
12272d6e41
commit
39889744b0
|
|
@ -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:
|
||||
|
||||
|
|
|
|||
|
|
@ -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[]
|
||||
|
|
@ -13,7 +13,7 @@ public class JdbcCorporateEventDaoConfiguration {
|
|||
// tag::snippet[]
|
||||
@Bean
|
||||
JdbcCorporateEventDao corporateEventDao(DataSource dataSource) {
|
||||
return new JdbcCorporateEventDao();
|
||||
return new JdbcCorporateEventDao(dataSource);
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "close")
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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[]
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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">
|
||||
|
|
|
|||
|
|
@ -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}"/>
|
||||
|
|
|
|||
Loading…
Reference in New Issue