diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc index 9e896e923c2..524aa3b017f 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -1688,19 +1688,6 @@ username, and the pool size, these settings are bound automatically before the (so the relevant sub-set of `spring.datasource.*` can still be used with your custom configuration). -You can apply the same principle if you configure a custom JNDI `DataSource`, as shown in -the following example: - -[source,java,indent=0,subs="verbatim,quotes,attributes"] ----- - @Bean(destroyMethod="") - @ConfigurationProperties(prefix="app.datasource") - public DataSource dataSource() throws Exception { - JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); - return dataSourceLookup.getDataSource("java:comp/env/jdbc/YourDS"); - } ----- - Spring Boot also provides a utility builder class, called `DataSourceBuilder`, that can be used to create one of the standard data sources (if it is on the classpath). The builder can detect the one to use based on what's available on the classpath. It also @@ -1768,18 +1755,22 @@ include::{code-examples}/jdbc/ConfigurableDataSourceExample.java[tag=configurati ---- 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 same -namespace. Because `DataSourceProperties` is taking care of the `url`/`jdbcUrl` -translation for you, you can configure it as follows: +a dedicated connection pool is chosen (in code) and its settings are exposed in the +`app.datasource.configuration` sub namespace. Because `DataSourceProperties` is taking +care of the `url`/`jdbcUrl` translation for you, you can configure it as follows: [source,properties,indent=0] ---- app.datasource.url=jdbc:mysql://localhost/test app.datasource.username=dbuser app.datasource.password=dbpass - app.datasource.maximum-pool-size=30 + app.datasource.configuration.maximum-pool-size=30 ---- +TIP: Spring Boot will expose Hikari-specific settings to `spring.datasource.hikari`. This +example uses a more generic `configuration` sub namespace as the example does not support +multiple datasource implementations. + NOTE: Because your custom configuration chooses to go with Hikari, `app.datasource.type` has no effect. In practice, the builder is initialized with whatever value you might set there and then overridden by the call to `.type()`. @@ -1815,10 +1806,12 @@ configure them as follows: [source,properties,indent=0] ---- - app.datasource.first.type=com.zaxxer.hikari.HikariDataSource - app.datasource.first.maximum-pool-size=30 + app.datasource.first.url=jdbc:mysql://localhost/first + app.datasource.first.username=dbuser + app.datasource.first.password=dbpass + app.datasource.first.configuration.maximum-pool-size=30 - app.datasource.second.url=jdbc:mysql://localhost/test + app.datasource.second.url=jdbc:mysql://localhost/second app.datasource.second.username=dbuser app.datasource.second.password=dbpass app.datasource.second.max-total=30 @@ -1833,7 +1826,8 @@ include::{code-examples}/jdbc/CompleteTwoDataSourcesExample.java[tag=configurati ---- The preceding example configures two data sources on custom namespaces with the same -logic as Spring Boot would use in auto-configuration. +logic as Spring Boot would use in auto-configuration. Note that each `configuration` sub +namespace provides advanced settings based on the chosen implementation. diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/CompleteTwoDataSourcesExample.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/CompleteTwoDataSourcesExample.java index 624bced135e..15818d42a94 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/CompleteTwoDataSourcesExample.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/CompleteTwoDataSourcesExample.java @@ -16,7 +16,8 @@ package org.springframework.boot.docs.jdbc; -import javax.sql.DataSource; +import com.zaxxer.hikari.HikariDataSource; +import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -48,9 +49,10 @@ public class CompleteTwoDataSourcesExample { @Bean @Primary - @ConfigurationProperties("app.datasource.first") - public DataSource firstDataSource() { - return firstDataSourceProperties().initializeDataSourceBuilder().build(); + @ConfigurationProperties("app.datasource.first.configuration") + public HikariDataSource firstDataSource() { + return firstDataSourceProperties().initializeDataSourceBuilder() + .type(HikariDataSource.class).build(); } @Bean @@ -60,9 +62,10 @@ public class CompleteTwoDataSourcesExample { } @Bean - @ConfigurationProperties("app.datasource.second") - public DataSource secondDataSource() { - return secondDataSourceProperties().initializeDataSourceBuilder().build(); + @ConfigurationProperties("app.datasource.second.configuration") + public BasicDataSource secondDataSource() { + return secondDataSourceProperties().initializeDataSourceBuilder() + .type(BasicDataSource.class).build(); } // end::configuration[] diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExample.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExample.java index 3ed8ecba56b..edf5ca61a95 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExample.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExample.java @@ -49,7 +49,7 @@ public class ConfigurableDataSourceExample { } @Bean - @ConfigurationProperties("app.datasource") + @ConfigurationProperties("app.datasource.configuration") public HikariDataSource dataSource(DataSourceProperties properties) { return properties.initializeDataSourceBuilder().type(HikariDataSource.class) .build(); diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/SimpleTwoDataSourcesExample.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/SimpleTwoDataSourcesExample.java index 286f187270b..4e1c431da90 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/SimpleTwoDataSourcesExample.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/SimpleTwoDataSourcesExample.java @@ -18,6 +18,7 @@ package org.springframework.boot.docs.jdbc; import javax.sql.DataSource; +import com.zaxxer.hikari.HikariDataSource; import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; @@ -51,9 +52,10 @@ public class SimpleTwoDataSourcesExample { @Bean @Primary - @ConfigurationProperties("app.datasource.first") - public DataSource firstDataSource() { - return firstDataSourceProperties().initializeDataSourceBuilder().build(); + @ConfigurationProperties("app.datasource.first.configuration") + public HikariDataSource firstDataSource() { + return firstDataSourceProperties().initializeDataSourceBuilder() + .type(HikariDataSource.class).build(); } @Bean diff --git a/spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExampleTests.java b/spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExampleTests.java index 0e658075490..3b951d041e0 100644 --- a/spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExampleTests.java +++ b/spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExampleTests.java @@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(properties = { "app.datasource.url=jdbc:h2:mem:configurable;DB_CLOSE_DELAY=-1", - "app.datasource.maximum-pool-size=42" }) + "app.datasource.configuration.maximum-pool-size=42" }) @Import(ConfigurableDataSourceExample.ConfigurableDataSourceConfiguration.class) public class ConfigurableDataSourceExampleTests {