Skip to content

Commit fcd58bd

Browse files
committed
Merge branch '3.3.x'
Closes gh-43059
2 parents 3625930 + 519ec86 commit fcd58bd

File tree

9 files changed

+52
-46
lines changed

9 files changed

+52
-46
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/data-access.adoc

+25-21
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@ app:
5454
pool-size: 30
5555
----
5656

57-
However, there is a catch.
58-
Because the actual type of the connection pool is not exposed, no keys are generated in the metadata for your custom `DataSource` and no completion is available in your IDE (because the `DataSource` interface exposes no properties).
59-
Also, if you happen to have Hikari on the classpath, this basic setup does not work, because Hikari has no `url` property (but does have a `jdbcUrl` property).
60-
In that case, you must rewrite your configuration as follows:
57+
However, there is a catch due to the method's `DataSource` return type.
58+
This hides the actual type of the connection pool so no configuration property metadata is generated for your custom `DataSource` and no auto-completion is available in your IDE.
59+
To address this problem, use the builder's `type(Class)` method to specify the type of `DataSource` to be built and update the method's return type.
60+
For example, the following shows how to create a `HikariDataSource` with `DataSourceBuilder`:
61+
62+
include-code::simple/MyDataSourceConfiguration[]
63+
64+
Unfortunately, this basic setup does not work because Hikari has no `url` property.
65+
Instead, it has a `jdbc-url` property which means that you must rewrite your configuration as follows:
6166

6267
[configprops%novalidate,yaml]
6368
----
@@ -69,22 +74,15 @@ app:
6974
pool-size: 30
7075
----
7176

72-
You can fix that by forcing the connection pool to use and return a dedicated implementation rather than `DataSource`.
73-
You cannot change the implementation at runtime, but the list of options will be explicit.
74-
75-
The following example shows how to create a `HikariDataSource` with `DataSourceBuilder`:
76-
77-
include-code::simple/MyDataSourceConfiguration[]
78-
79-
You can even go further by leveraging what `DataSourceProperties` does for you -- that is, by providing a default embedded database with a sensible username and password if no URL is provided.
80-
You can easily initialize a `DataSourceBuilder` from the state of any `DataSourceProperties` object, so you could also inject the DataSource that Spring Boot creates automatically.
81-
However, that would split your configuration into two namespaces: `url`, `username`, `password`, `type`, and `driver` on `spring.datasource` and the rest on your custom namespace (`app.datasource`).
82-
To avoid that, you can redefine a custom `DataSourceProperties` on your custom namespace, as shown in the following example:
77+
To address this problem, make use of `DataSourceProperties` which will handle the `url` to `jdbc-url` translation for you.
78+
You can initialize a `DataSourceBuilder` from the state of any `DataSourceProperties` object using its `initializeDataSourceBuilder()` method.
79+
You could inject the `DataSourceProperties` that Spring Boot creates automatically, however, that would split your configuration across `+spring.datasource.*+` and `+app.datasource.*+`.
80+
To avoid this, define a custom `DataSourceProperties` with a custom configuration properties prefix, as shown in the following example:
8381

8482
include-code::configurable/MyDataSourceConfiguration[]
8583

86-
This setup puts you _in sync_ with what Spring Boot does for you by default, except that a dedicated connection pool is chosen (in code) and its settings are exposed in the `app.datasource.configuration` sub namespace.
87-
Because `DataSourceProperties` is taking care of the `url`/`jdbcUrl` translation for you, you can configure it as follows:
84+
This setup is equivalent to what Spring Boot does for you by default, except that the pool's type is specified in code and its settings are exposed as `app.datasource.configuration.*` properties.
85+
`DataSourceProperties` takes care of the `url` to `jdbc-url` translation, so you can configure it as follows:
8886

8987
[configprops%novalidate,yaml]
9088
----
@@ -97,13 +95,16 @@ app:
9795
maximum-pool-size: 30
9896
----
9997

98+
Note that, as the custom configuration specifies in code that Hikari should be used, `app.datasource.type` will have no effect.
99+
100+
As described in xref:reference:data/sql.adoc#data.sql.datasource.connection-pool[], `DataSourceBuilder` supports several different connection pools.
101+
To use a pool other than Hikari, add it to the classpath, use the `type(Class)` method to specify the pool class to use, and update the `@Bean` method's return type to match.
102+
This will also provide you with configuration property metadata for the specific connection pool that you've chosen.
103+
100104
TIP: Spring Boot will expose Hikari-specific settings to `spring.datasource.hikari`.
101105
This example uses a more generic `configuration` sub namespace as the example does not support multiple datasource implementations.
102106

103-
NOTE: Because your custom configuration chooses to go with Hikari, `app.datasource.type` has no effect.
104-
In practice, the builder is initialized with whatever value you might set there and then overridden by the call to `.type()`.
105-
106-
See xref:reference:data/sql.adoc#data.sql.datasource[] in the "`Spring Boot Features`" section and the {code-spring-boot-autoconfigure-src}/jdbc/DataSourceAutoConfiguration.java[`DataSourceAutoConfiguration`] class for more details.
107+
See xref:reference:data/sql.adoc#data.sql.datasource[] and the {code-spring-boot-autoconfigure-src}/jdbc/DataSourceAutoConfiguration.java[`DataSourceAutoConfiguration`] class for more details.
107108

108109

109110

@@ -149,6 +150,9 @@ include-code::MyCompleteAdditionalDataSourceConfiguration[]
149150
The preceding example configures the additional data source with the same logic as Spring Boot would use in auto-configuration.
150151
Note that the `app.datasource.configuration.*` properties provide advanced settings based on the chosen implementation.
151152

153+
As with xref:how-to:data-access.adoc#howto.data-access.configure-custom-datasource[configuring a single custom `DataSource`], the type of one or both of the `DataSource` beans can be customized using the `type(Class)` method on `DataSourceBuilder`.
154+
See xref:reference:data/sql.adoc#data.sql.datasource.connection-pool[] for details of the supported types.
155+
152156

153157

154158
[[howto.data-access.spring-data-repositories]]

spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/testing.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ include-code::MyConfiguration[]
3333
For a `@WebMvcTest` for an application with the above `@Configuration` class, you might expect to have the `SecurityFilterChain` bean in the application context so that you can test if your controller endpoints are secured properly.
3434
However, `MyConfiguration` is not picked up by @WebMvcTest's component scanning filter because it doesn't match any of the types specified by the filter.
3535
You can include the configuration explicitly by annotating the test class with `@Import(MyConfiguration.class)`.
36-
This will load all the beans in `MyConfiguration` including the `BasicDataSource` bean which isn't required when testing the web tier.
36+
This will load all the beans in `MyConfiguration` including the `HikariDataSource` bean which isn't required when testing the web tier.
3737
Splitting the configuration class into two will enable importing just the security configuration.
3838

3939
include-code::MySecurityConfiguration[]

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/howto/dataaccess/configuretwodatasources/MyAdditionalDataSourceConfiguration.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.docs.howto.dataaccess.configuretwodatasources;
1818

19-
import org.apache.commons.dbcp2.BasicDataSource;
19+
import com.zaxxer.hikari.HikariDataSource;
2020

2121
import org.springframework.beans.factory.annotation.Qualifier;
2222
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -30,8 +30,8 @@ public class MyAdditionalDataSourceConfiguration {
3030
@Qualifier("second")
3131
@Bean(defaultCandidate = false)
3232
@ConfigurationProperties("app.datasource")
33-
public BasicDataSource secondDataSource() {
34-
return DataSourceBuilder.create().type(BasicDataSource.class).build();
33+
public HikariDataSource secondDataSource() {
34+
return DataSourceBuilder.create().type(HikariDataSource.class).build();
3535
}
3636

3737
}

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/howto/dataaccess/configuretwodatasources/MyCompleteAdditionalDataSourceConfiguration.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.docs.howto.dataaccess.configuretwodatasources;
1818

19-
import org.apache.commons.dbcp2.BasicDataSource;
19+
import com.zaxxer.hikari.HikariDataSource;
2020

2121
import org.springframework.beans.factory.annotation.Qualifier;
2222
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
@@ -37,9 +37,9 @@ public DataSourceProperties secondDataSourceProperties() {
3737
@Qualifier("second")
3838
@Bean(defaultCandidate = false)
3939
@ConfigurationProperties("app.datasource.configuration")
40-
public BasicDataSource secondDataSource(
40+
public HikariDataSource secondDataSource(
4141
@Qualifier("secondDataSourceProperties") DataSourceProperties secondDataSourceProperties) {
42-
return secondDataSourceProperties.initializeDataSourceBuilder().type(BasicDataSource.class).build();
42+
return secondDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
4343
}
4444

4545
}

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/howto/testing/slicetests/MyConfiguration.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.docs.howto.testing.slicetests;
1818

19-
import org.apache.commons.dbcp2.BasicDataSource;
19+
import com.zaxxer.hikari.HikariDataSource;
2020

2121
import org.springframework.boot.context.properties.ConfigurationProperties;
2222
import org.springframework.boot.jdbc.DataSourceBuilder;
@@ -36,8 +36,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
3636

3737
@Bean
3838
@ConfigurationProperties("app.datasource.second")
39-
public BasicDataSource secondDataSource() {
40-
return DataSourceBuilder.create().type(BasicDataSource.class).build();
39+
public HikariDataSource secondDataSource() {
40+
return DataSourceBuilder.create().type(HikariDataSource.class).build();
4141
}
4242

4343
}

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/howto/testing/slicetests/MyDatasourceConfiguration.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.docs.howto.testing.slicetests;
1818

19-
import org.apache.commons.dbcp2.BasicDataSource;
19+
import com.zaxxer.hikari.HikariDataSource;
2020

2121
import org.springframework.boot.context.properties.ConfigurationProperties;
2222
import org.springframework.boot.jdbc.DataSourceBuilder;
@@ -28,8 +28,8 @@ public class MyDatasourceConfiguration {
2828

2929
@Bean
3030
@ConfigurationProperties("app.datasource.second")
31-
public BasicDataSource secondDataSource() {
32-
return DataSourceBuilder.create().type(BasicDataSource.class).build();
31+
public HikariDataSource secondDataSource() {
32+
return DataSourceBuilder.create().type(HikariDataSource.class).build();
3333
}
3434

3535
}

spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/howto/dataaccess/configuretwodatasources/MyAdditionalDataSourceConfiguration.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.docs.howto.dataaccess.configuretwodatasources
1818

19-
import org.apache.commons.dbcp2.BasicDataSource
19+
import com.zaxxer.hikari.HikariDataSource
2020

2121
import org.springframework.beans.factory.annotation.Qualifier
2222
import org.springframework.boot.context.properties.ConfigurationProperties
@@ -30,8 +30,8 @@ class MyAdditionalDataSourceConfiguration {
3030
@Qualifier("second")
3131
@Bean(defaultCandidate = false)
3232
@ConfigurationProperties("app.datasource")
33-
fun secondDataSource(): BasicDataSource {
34-
return DataSourceBuilder.create().type(BasicDataSource::class.java).build()
33+
fun secondDataSource(): HikariDataSource {
34+
return DataSourceBuilder.create().type(HikariDataSource::class.java).build()
3535
}
3636

3737
}

spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/howto/dataaccess/configuretwodatasources/MyCompleteAdditionalDataSourceConfiguration.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.docs.howto.dataaccess.configuretwodatasources
1818

19-
import org.apache.commons.dbcp2.BasicDataSource
19+
import com.zaxxer.hikari.HikariDataSource
2020

2121
import org.springframework.beans.factory.annotation.Qualifier
2222
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
@@ -37,8 +37,8 @@ class MyCompleteAdditionalDataSourceConfiguration {
3737
@Qualifier("second")
3838
@Bean(defaultCandidate = false)
3939
@ConfigurationProperties("app.datasource.configuration")
40-
fun secondDataSource(secondDataSourceProperties: DataSourceProperties): BasicDataSource {
41-
return secondDataSourceProperties.initializeDataSourceBuilder().type(BasicDataSource::class.java).build()
40+
fun secondDataSource(secondDataSourceProperties: DataSourceProperties): HikariDataSource {
41+
return secondDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource::class.java).build()
4242
}
4343

4444
}

spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/howto/dataaccess/configuretwodatasources/MyDataSourcesConfigurationTests.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import javax.sql.DataSource;
2222

23-
import org.apache.commons.dbcp2.BasicDataSource;
23+
import com.zaxxer.hikari.HikariDataSource;
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.beans.factory.annotation.Autowired;
@@ -36,7 +36,8 @@
3636
*
3737
* @author Stephane Nicoll
3838
*/
39-
@SpringBootTest(properties = { "app.datasource.url=jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1", "app.datasource.max-total=42" })
39+
@SpringBootTest(properties = { "app.datasource.jdbc-url=jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1",
40+
"app.datasource.maximum-pool-size=42" })
4041
@Import(MyAdditionalDataSourceConfiguration.class)
4142
class MyDataSourcesConfigurationTests {
4243

@@ -56,9 +57,10 @@ void validateConfiguration() throws SQLException {
5657
assertThat(this.context.getBean("dataSource")).isSameAs(this.dataSource);
5758
assertThat(this.dataSource.getConnection().getMetaData().getURL()).startsWith("jdbc:h2:mem:");
5859
assertThat(this.context.getBean("secondDataSource")).isSameAs(this.secondDataSource);
59-
assertThat(this.secondDataSource).extracting((dataSource) -> ((BasicDataSource) dataSource).getUrl())
60+
assertThat(this.secondDataSource).extracting((dataSource) -> ((HikariDataSource) dataSource).getJdbcUrl())
6061
.isEqualTo("jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1");
61-
assertThat(this.secondDataSource).extracting((dataSource) -> ((BasicDataSource) dataSource).getMaxTotal())
62+
assertThat(this.secondDataSource)
63+
.extracting((dataSource) -> ((HikariDataSource) dataSource).getMaximumPoolSize())
6264
.isEqualTo(42);
6365
}
6466

0 commit comments

Comments
 (0)