Restore support for spring.datasource.type

This commit restores the support of "spring.datasource.type". If this
property is set to an unsupported `DataSource` implementation, we now
properly create it again, rather than ignoring it because its value did
not match any value we support.

Since Spring Boot 1.4 now redirects `DataSource` implementation settings
to dedicated namespaces, the documentation has been updated to explicitly
mention how one can create such arrangement for an unsupported
implementation.

As a convenience, `DataSourceProperties` can initialize a
`DataSourceBuilder` based on its internal state, making it very easy for
anyone to create a `DataSource` from a `DataSourceProperties` instance.

Closes gh-6695
This commit is contained in:
Stephane Nicoll
2016-08-22 11:49:32 +02:00
parent a6ef3741ef
commit 54a3ce8827
5 changed files with 130 additions and 31 deletions

View File

@@ -1642,15 +1642,14 @@ a| `log4j2.json` +
[[howto-configure-a-datasource]]
=== Configure a DataSource
To override the default settings just define a `@Bean` of your own of type `DataSource`.
Spring Boot provides a utility builder class `DataSourceBuilder` that can be used
to create one of the standard ones (if it is on the classpath), or you can just create
your own, and bind it to a set of `Environment` properties as explained in
<<spring-boot-features.adoc#boot-features-external-config-3rd-party-configuration>>, e.g.
As explained in
<<spring-boot-features.adoc#boot-features-external-config-3rd-party-configuration>> you
can easily bind it to a set of `Environment` properties:
[source,java,indent=0,subs="verbatim,quotes,attributes"]
----
@Bean
@ConfigurationProperties(prefix="datasource.mine")
@ConfigurationProperties(prefix="datasource.fancy")
public DataSource dataSource() {
return new FancyDataSource();
}
@@ -1658,11 +1657,38 @@ your own, and bind it to a set of `Environment` properties as explained in
[source,properties,indent=0]
----
datasource.mine.jdbcUrl=jdbc:h2:mem:mydb
datasource.mine.user=sa
datasource.fancy.jdbcUrl=jdbc:h2:mem:mydb
datasource.fancy.username=sa
datasource.fancy.poolSize=30
----
Spring Boot also provides a utility builder class `DataSourceBuilder` that can be used
to create one of the standard data sources (if it is on the classpath), or you can just
create your own. If you want to reuse the customizations of `DataSourceProperties`, you
can easily initialize a `DataSourceBuilder` from it:
[source,java,indent=0,subs="verbatim,quotes,attributes"]
----
@Bean
@ConfigurationProperties(prefix="datasource.mine")
public DataSource dataSource(DataSourceProperties properties) {
return properties.initializeDataSourceBuilder()
// additional customizations
.build();
}
----
[source,properties,indent=0]
----
spring.datasource.url=jdbc:h2:mem:mydb
spring.datasource.username=sa
datasource.mine.poolSize=30
----
In this scenario, you keep the standard properties exposed by Spring Boot with your
custom `DataSource` arrangement. By adding `@ConfigurationProperties`, you can also
expose additional implementation-specific settings in a dedicated namespace.
See _<<spring-boot-features.adoc#boot-features-configure-datasource>>_ in the
'`Spring Boot features`' section and the
{sc-spring-boot-autoconfigure}/jdbc/DataSourceAutoConfiguration.{sc-ext}[`DataSourceAutoConfiguration`]