Skip to content

Commit d2e55a2

Browse files
committed
Use code includes and tabs in jdbc/core.adoc
See gh-22171
1 parent ae6c64a commit d2e55a2

File tree

11 files changed

+300
-99
lines changed

11 files changed

+300
-99
lines changed

framework-docs/modules/ROOT/pages/data-access/jdbc/core.adoc

Lines changed: 6 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -387,112 +387,19 @@ Kotlin::
387387
======
388388
--
389389

390-
The following example shows the corresponding XML configuration:
390+
The following example shows the corresponding configuration:
391391

392-
[source,xml,indent=0,subs="verbatim,quotes"]
393-
----
394-
<?xml version="1.0" encoding="UTF-8"?>
395-
<beans xmlns="http://www.springframework.org/schema/beans"
396-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
397-
xmlns:context="http://www.springframework.org/schema/context"
398-
xsi:schemaLocation="
399-
http://www.springframework.org/schema/beans
400-
https://www.springframework.org/schema/beans/spring-beans.xsd
401-
http://www.springframework.org/schema/context
402-
https://www.springframework.org/schema/context/spring-context.xsd">
403-
404-
<bean id="corporateEventDao" class="com.example.JdbcCorporateEventDao">
405-
<property name="dataSource" ref="dataSource"/>
406-
</bean>
407-
408-
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
409-
<property name="driverClassName" value="${jdbc.driverClassName}"/>
410-
<property name="url" value="${jdbc.url}"/>
411-
<property name="username" value="${jdbc.username}"/>
412-
<property name="password" value="${jdbc.password}"/>
413-
</bean>
414-
415-
<context:property-placeholder location="jdbc.properties"/>
416-
417-
</beans>
418-
----
392+
include-code::./JdbcCorporateEventDaoConfiguration[tag=snippet,indent=0]
419393

420394
An alternative to explicit configuration is to use component-scanning and annotation
421395
support for dependency injection. In this case, you can annotate the class with `@Repository`
422-
(which makes it a candidate for component-scanning) and annotate the `DataSource` setter
423-
method with `@Autowired`. The following example shows how to do so:
424-
425-
--
426-
[tabs]
427-
======
428-
Java::
429-
+
430-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
431-
----
432-
@Repository // <1>
433-
public class JdbcCorporateEventDao implements CorporateEventDao {
434-
435-
private JdbcTemplate jdbcTemplate;
436-
437-
@Autowired // <2>
438-
public void setDataSource(DataSource dataSource) {
439-
this.jdbcTemplate = new JdbcTemplate(dataSource); // <3>
440-
}
441-
442-
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
443-
}
444-
----
445-
<1> Annotate the class with `@Repository`.
446-
<2> Annotate the `DataSource` setter method with `@Autowired`.
447-
<3> Create a new `JdbcTemplate` with the `DataSource`.
396+
(which makes it a candidate for component-scanning). The following example shows how to do so:
448397

449-
Kotlin::
450-
+
451-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
452-
----
453-
@Repository // <1>
454-
class JdbcCorporateEventDao(dataSource: DataSource) : CorporateEventDao { // <2>
398+
include-code::./JdbcCorporateEventRepository[tag=snippet,indent=0]
455399

456-
private val jdbcTemplate = JdbcTemplate(dataSource) // <3>
400+
The following example shows the corresponding configuration:
457401

458-
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
459-
}
460-
----
461-
<1> Annotate the class with `@Repository`.
462-
<2> Constructor injection of the `DataSource`.
463-
<3> Create a new `JdbcTemplate` with the `DataSource`.
464-
======
465-
--
466-
467-
468-
The following example shows the corresponding XML configuration:
469-
470-
[source,xml,indent=0,subs="verbatim,quotes"]
471-
----
472-
<?xml version="1.0" encoding="UTF-8"?>
473-
<beans xmlns="http://www.springframework.org/schema/beans"
474-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
475-
xmlns:context="http://www.springframework.org/schema/context"
476-
xsi:schemaLocation="
477-
http://www.springframework.org/schema/beans
478-
https://www.springframework.org/schema/beans/spring-beans.xsd
479-
http://www.springframework.org/schema/context
480-
https://www.springframework.org/schema/context/spring-context.xsd">
481-
482-
<!-- Scans within the base package of the application for @Component classes to configure as beans -->
483-
<context:component-scan base-package="org.springframework.docs.test" />
484-
485-
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
486-
<property name="driverClassName" value="${jdbc.driverClassName}"/>
487-
<property name="url" value="${jdbc.url}"/>
488-
<property name="username" value="${jdbc.username}"/>
489-
<property name="password" value="${jdbc.password}"/>
490-
</bean>
491-
492-
<context:property-placeholder location="jdbc.properties"/>
493-
494-
</beans>
495-
----
402+
include-code::./JdbcCorporateEventRepositoryConfiguration[tag=snippet,indent=0]
496403

497404
If you use Spring's `JdbcDaoSupport` class and your various JDBC-backed DAO classes
498405
extend from it, your sub-class inherits a `setDataSource(..)` method from the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms;
18+
19+
public interface CorporateEventDao {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms;
18+
19+
public interface CorporateEventRepository {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.springframework.jdbc.core.JdbcTemplate;
22+
23+
public class JdbcCorporateEventDao implements CorporateEventDao {
24+
25+
private JdbcTemplate jdbcTemplate;
26+
27+
public void setDataSource(DataSource dataSource) {
28+
this.jdbcTemplate = new JdbcTemplate(dataSource);
29+
}
30+
31+
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.apache.commons.dbcp2.BasicDataSource;
6+
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
@Configuration
11+
public class JdbcCorporateEventDaoConfiguration {
12+
13+
// tag::snippet[]
14+
@Bean
15+
JdbcCorporateEventDao corporateEventDao(DataSource dataSource) {
16+
return new JdbcCorporateEventDao();
17+
}
18+
19+
@Bean(destroyMethod = "close")
20+
BasicDataSource dataSource() {
21+
BasicDataSource dataSource = new BasicDataSource();
22+
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
23+
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:");
24+
dataSource.setUsername("sa");
25+
dataSource.setPassword("");
26+
return dataSource;
27+
}
28+
// end::snippet[]
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.springframework.jdbc.core.JdbcTemplate;
22+
import org.springframework.stereotype.Repository;
23+
24+
// tag::snippet[]
25+
@Repository
26+
public class JdbcCorporateEventRepository implements CorporateEventRepository {
27+
28+
private JdbcTemplate jdbcTemplate;
29+
30+
// Implicitly autowire the DataSource constructor parameter
31+
public JdbcCorporateEventRepository(DataSource dataSource) {
32+
this.jdbcTemplate = new JdbcTemplate(dataSource);
33+
}
34+
35+
// JDBC-backed implementations of the methods on the CorporateEventRepository follow...
36+
}
37+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms;
2+
3+
import org.apache.commons.dbcp2.BasicDataSource;
4+
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
// tag::snippet[]
10+
@Configuration
11+
@ComponentScan("org.springframework.docs.dataaccess.jdbc")
12+
public class JdbcCorporateEventRepositoryConfiguration {
13+
14+
@Bean(destroyMethod = "close")
15+
BasicDataSource dataSource() {
16+
BasicDataSource dataSource = new BasicDataSource();
17+
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
18+
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:");
19+
dataSource.setUsername("sa");
20+
dataSource.setPassword("");
21+
return dataSource;
22+
}
23+
24+
}
25+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms
18+
19+
import org.apache.commons.dbcp2.BasicDataSource
20+
import org.springframework.context.annotation.Bean
21+
import org.springframework.context.annotation.Configuration
22+
import org.springframework.docs.dataaccess.jdbc.JdbcCorporateEventDao
23+
import javax.sql.DataSource
24+
25+
@Configuration
26+
class JdbcCorporateEventDaoConfiguration {
27+
28+
// tag::snippet[]
29+
@Bean
30+
fun corporateEventDao(dataSource: DataSource) = JdbcCorporateEventDao()
31+
32+
@Bean(destroyMethod = "close")
33+
fun dataSource() = BasicDataSource().apply {
34+
driverClassName = "org.hsqldb.jdbcDriver"
35+
url = "jdbc:hsqldb:hsql://localhost:"
36+
username = "sa"
37+
password = ""
38+
}
39+
// end::snippet[]
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms
18+
19+
import org.apache.commons.dbcp2.BasicDataSource
20+
import org.springframework.context.annotation.Bean
21+
import org.springframework.context.annotation.ComponentScan
22+
import org.springframework.context.annotation.Configuration
23+
24+
// tag::snippet[]
25+
@Configuration
26+
@ComponentScan("org.springframework.docs.dataaccess.jdbc")
27+
class JdbcCorporateEventRepositoryConfiguration {
28+
29+
@Bean(destroyMethod = "close")
30+
fun dataSource() = BasicDataSource().apply {
31+
driverClassName = "org.hsqldb.jdbcDriver"
32+
url = "jdbc:hsqldb:hsql://localhost:"
33+
username = "sa"
34+
password = ""
35+
}
36+
37+
}
38+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans
7+
https://www.springframework.org/schema/beans/spring-beans.xsd
8+
http://www.springframework.org/schema/context
9+
https://www.springframework.org/schema/context/spring-context.xsd">
10+
11+
<!-- tag::snippet[] -->
12+
<bean id="corporateEventDao" class="org.springframework.docs.dataaccess.jdbc.jdbcJdbcTemplateidioms.JdbcCorporateEventDao">
13+
<property name="dataSource" ref="dataSource"/>
14+
</bean>
15+
16+
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
17+
<property name="driverClassName" value="${jdbc.driverClassName}"/>
18+
<property name="url" value="${jdbc.url}"/>
19+
<property name="username" value="${jdbc.username}"/>
20+
<property name="password" value="${jdbc.password}"/>
21+
</bean>
22+
23+
<context:property-placeholder location="jdbc.properties"/>
24+
<!-- end::snippet[] -->
25+
26+
</beans>

0 commit comments

Comments
 (0)