Skip to content

Commit 722199f

Browse files
committed
Use code includes and tabs in connections.adoc
See spring-projectsgh-22171
1 parent 9cb8766 commit 722199f

File tree

11 files changed

+249
-64
lines changed

11 files changed

+249
-64
lines changed

framework-docs/framework-docs.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ repositories {
6060

6161
dependencies {
6262
api(project(":spring-context"))
63+
api(project(":spring-jdbc"))
6364
api(project(":spring-jms"))
6465
api(project(":spring-web"))
6566

6667
api("org.jetbrains.kotlin:kotlin-stdlib")
6768
api("jakarta.jms:jakarta.jms-api")
6869
api("jakarta.servlet:jakarta.servlet-api")
70+
api("org.apache.commons:commons-dbcp2:2.11.0")
71+
api("com.mchange:c3p0:0.9.5.5")
6972

7073
implementation(project(":spring-core-test"))
7174
implementation("org.assertj:assertj-core")

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

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -46,80 +46,21 @@ To configure a `DriverManagerDataSource`:
4646
for the correct value.)
4747
. Provide a username and a password to connect to the database.
4848

49-
The following example shows how to configure a `DriverManagerDataSource` in Java:
50-
51-
[tabs]
52-
======
53-
Java::
54-
+
55-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
56-
----
57-
DriverManagerDataSource dataSource = new DriverManagerDataSource();
58-
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
59-
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:");
60-
dataSource.setUsername("sa");
61-
dataSource.setPassword("");
62-
----
63-
64-
Kotlin::
65-
+
66-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
67-
----
68-
val dataSource = DriverManagerDataSource().apply {
69-
setDriverClassName("org.hsqldb.jdbcDriver")
70-
url = "jdbc:hsqldb:hsql://localhost:"
71-
username = "sa"
72-
password = ""
73-
}
74-
----
75-
======
76-
77-
The following example shows the corresponding XML configuration:
78-
79-
[source,xml,indent=0,subs="verbatim,quotes"]
80-
----
81-
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
82-
<property name="driverClassName" value="${jdbc.driverClassName}"/>
83-
<property name="url" value="${jdbc.url}"/>
84-
<property name="username" value="${jdbc.username}"/>
85-
<property name="password" value="${jdbc.password}"/>
86-
</bean>
87-
88-
<context:property-placeholder location="jdbc.properties"/>
89-
----
49+
The following example shows how to configure a `DriverManagerDataSource`:
50+
51+
include-code::./DriverManagerDataSourceConfiguration[tag=dataSourceBean,indent=0]
9052

9153
The next two examples show the basic connectivity and configuration for DBCP and C3P0.
9254
To learn about more options that help control the pooling features, see the product
9355
documentation for the respective connection pooling implementations.
9456

9557
The following example shows DBCP configuration:
9658

97-
[source,xml,indent=0,subs="verbatim,quotes"]
98-
----
99-
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
100-
<property name="driverClassName" value="${jdbc.driverClassName}"/>
101-
<property name="url" value="${jdbc.url}"/>
102-
<property name="username" value="${jdbc.username}"/>
103-
<property name="password" value="${jdbc.password}"/>
104-
</bean>
105-
106-
<context:property-placeholder location="jdbc.properties"/>
107-
----
59+
include-code::./BasicDataSourceConfiguration[tag=dataSourceBean,indent=0]
10860

10961
The following example shows C3P0 configuration:
11062

111-
[source,xml,indent=0,subs="verbatim,quotes"]
112-
----
113-
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
114-
<property name="driverClass" value="${jdbc.driverClassName}"/>
115-
<property name="jdbcUrl" value="${jdbc.url}"/>
116-
<property name="user" value="${jdbc.username}"/>
117-
<property name="password" value="${jdbc.password}"/>
118-
</bean>
119-
120-
<context:property-placeholder location="jdbc.properties"/>
121-
----
122-
63+
include-code::./ComboPooledDataSourceConfiguration[tag=dataSourceBean,indent=0]
12364

12465
[[jdbc-DataSourceUtils]]
12566
== Using `DataSourceUtils`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.jdbcdatasource;
18+
19+
import org.apache.commons.dbcp2.BasicDataSource;
20+
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
24+
@Configuration
25+
class BasicDataSourceConfiguration {
26+
27+
// tag::dataSourceBean[]
28+
@Bean(destroyMethod = "close")
29+
BasicDataSource dataSource() {
30+
BasicDataSource dataSource = new BasicDataSource();
31+
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
32+
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:");
33+
dataSource.setUsername("sa");
34+
dataSource.setPassword("");
35+
return dataSource;
36+
}
37+
// end::dataSourceBean[]
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
package org.springframework.docs.dataaccess.jdbc.jdbcdatasource;
17+
18+
import java.beans.PropertyVetoException;
19+
20+
import com.mchange.v2.c3p0.ComboPooledDataSource;
21+
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
25+
@Configuration
26+
class ComboPooledDataSourceConfiguration {
27+
28+
// tag::dataSourceBean[]
29+
@Bean(destroyMethod = "close")
30+
ComboPooledDataSource dataSource() throws PropertyVetoException {
31+
ComboPooledDataSource dataSource = new ComboPooledDataSource();
32+
dataSource.setDriverClass("org.hsqldb.jdbcDriver");
33+
dataSource.setJdbcUrl("jdbc:hsqldb:hsql://localhost:");
34+
dataSource.setUser("sa");
35+
dataSource.setPassword("");
36+
return dataSource;
37+
}
38+
// end::dataSourceBean[]
39+
40+
}
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.jdbcdatasource;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
22+
23+
@Configuration
24+
class DriverManagerDataSourceConfiguration {
25+
26+
// tag::dataSourceBean[]
27+
@Bean
28+
DriverManagerDataSource dataSource() {
29+
DriverManagerDataSource dataSource = new DriverManagerDataSource();
30+
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
31+
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:");
32+
dataSource.setUsername("sa");
33+
dataSource.setPassword("");
34+
return dataSource;
35+
}
36+
// end::dataSourceBean[]
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.springframework.docs.dataaccess.jdbc.jdbcdatasource
2+
3+
import org.apache.commons.dbcp2.BasicDataSource
4+
import org.springframework.context.annotation.Bean
5+
import org.springframework.context.annotation.Configuration
6+
7+
@Configuration
8+
class BasicDataSourceConfiguration {
9+
10+
// tag::dataSourceBean[]
11+
@Bean(destroyMethod = "close")
12+
fun dataSource(): BasicDataSource {
13+
val dataSource = BasicDataSource()
14+
dataSource.driverClassName = "org.hsqldb.jdbcDriver"
15+
dataSource.url = "jdbc:hsqldb:hsql://localhost:"
16+
dataSource.username = "sa"
17+
dataSource.password = ""
18+
return dataSource
19+
}
20+
// end::dataSourceBean[]
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.springframework.docs.dataaccess.jdbc.jdbcdatasource
2+
3+
import com.mchange.v2.c3p0.ComboPooledDataSource
4+
import org.springframework.context.annotation.Bean
5+
import org.springframework.context.annotation.Configuration
6+
7+
@Configuration
8+
internal class ComboPooledDataSourceConfiguration {
9+
10+
// tag::dataSourceBean[]
11+
@Bean(destroyMethod = "close")
12+
fun dataSource(): ComboPooledDataSource {
13+
val dataSource = ComboPooledDataSource()
14+
dataSource.driverClass = "org.hsqldb.jdbcDriver"
15+
dataSource.jdbcUrl = "jdbc:hsqldb:hsql://localhost:"
16+
dataSource.user = "sa"
17+
dataSource.password = ""
18+
return dataSource
19+
}
20+
// end::dataSourceBean[]
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2022 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.jdbcdatasource
18+
19+
import org.springframework.context.annotation.Bean
20+
import org.springframework.context.annotation.Configuration
21+
import org.springframework.jdbc.datasource.DriverManagerDataSource
22+
23+
@Configuration
24+
class DriverManagerDataSourceConfiguration {
25+
26+
// tag::dataSourceBean[]
27+
@Bean
28+
fun dataSource() = DriverManagerDataSource().apply {
29+
setDriverClassName("org.hsqldb.jdbcDriver")
30+
url = "jdbc:hsqldb:hsql://localhost:"
31+
username = "sa"
32+
password = ""
33+
}
34+
// end::dataSourceBean[]
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:context="http://www.springframework.org/schema/context">
4+
5+
<!-- tag::dataSourceBean[] -->
6+
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
7+
<property name="driverClassName" value="${jdbc.driverClassName}"/>
8+
<property name="url" value="${jdbc.url}"/>
9+
<property name="username" value="${jdbc.username}"/>
10+
<property name="password" value="${jdbc.password}"/>
11+
</bean>
12+
13+
<context:property-placeholder location="jdbc.properties"/>
14+
<!-- end::dataSourceBean[] -->
15+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:context="http://www.springframework.org/schema/context">
4+
5+
<!-- tag::dataSourceBean[] -->
6+
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
7+
<property name="driverClass" value="${jdbc.driverClassName}"/>
8+
<property name="jdbcUrl" value="${jdbc.url}"/>
9+
<property name="user" value="${jdbc.username}"/>
10+
<property name="password" value="${jdbc.password}"/>
11+
</bean>
12+
13+
<context:property-placeholder location="jdbc.properties"/>
14+
<!-- end::dataSourceBean[] -->
15+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:context="http://www.springframework.org/schema/context">
4+
5+
<!-- tag::dataSourceBean[] -->
6+
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
7+
<property name="driverClassName" value="${jdbc.driverClassName}"/>
8+
<property name="url" value="${jdbc.url}"/>
9+
<property name="username" value="${jdbc.username}"/>
10+
<property name="password" value="${jdbc.password}"/>
11+
</bean>
12+
13+
<context:property-placeholder location="jdbc.properties"/>
14+
<!-- end::dataSourceBean[] -->
15+
</beans>

0 commit comments

Comments
 (0)