This commit is contained in:
Phillip Webb 2017-01-25 16:45:59 -08:00
parent daf6be46f6
commit 2cf93f89f5
11 changed files with 28 additions and 40 deletions

View File

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId> <artifactId>spring-boot-dependencies</artifactId>
@ -1961,9 +1963,9 @@
<version>${mongodb.version}</version> <version>${mongodb.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mortbay.jasper</groupId> <groupId>org.mortbay.jasper</groupId>
<artifactId>apache-el</artifactId> <artifactId>apache-el</artifactId>
<version>${jetty-el.version}</version> <version>${jetty-el.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.neo4j</groupId> <groupId>org.neo4j</groupId>
@ -2669,4 +2671,4 @@
<id>integration-test</id> <id>integration-test</id>
</profile> </profile>
</profiles> </profiles>
</project> </project>

View File

@ -1732,11 +1732,10 @@ You can apply the same principle if you are configuring a custom JNDI `DataSourc
} }
---- ----
Spring Boot also provides a utility builder class `DataSourceBuilder` that can be used to
Spring Boot also provides a utility builder class `DataSourceBuilder` that can be used create one of the standard data sources (if it is on the classpath). The builder can
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 auto detects the
detect the one to use based on the ones available on the classpath and it also auto driver based on the JDBC url.
detects the driver based on the JDBC url.
[source,java,indent=0,subs="verbatim,quotes,attributes"] [source,java,indent=0,subs="verbatim,quotes,attributes"]
---- ----
@ -1759,7 +1758,7 @@ There is a catch however. Because the actual type of the connection pool is not
no keys are generated in the metadata for your custom `DataSource` and no completion is no keys are generated in the metadata for your custom `DataSource` and no completion is
available in your IDE (The `DataSource` interface doesn't expose any property). Also, if available in your IDE (The `DataSource` interface doesn't expose any property). Also, if
you happen to _only_ have Hikari on the classpath, this basic setup will not work because you happen to _only_ have Hikari on the classpath, this basic setup will not work because
Hikari has no `url` parameter (but a `jdbcUrl` parameter). You should have to rewrite Hikari has no `url` parameter (but a `jdbcUrl` parameter). You will have to rewrite
your configuration as follows: your configuration as follows:
[source,properties,indent=0] [source,properties,indent=0]
@ -1861,6 +1860,7 @@ This final example configures two data sources on custom namespaces with the sam
than what Spring Boot would do in auto-configuration. than what Spring Boot would do in auto-configuration.
[[howto-use-spring-data-repositories]] [[howto-use-spring-data-repositories]]
=== Use Spring Data repositories === Use Spring Data repositories
Spring Data can create implementations for you of `@Repository` interfaces of various Spring Data can create implementations for you of `@Repository` interfaces of various

View File

@ -45,4 +45,5 @@ public class BasicDataSourceExample {
// end::configuration[] // end::configuration[]
} }
} }

View File

@ -25,8 +25,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
/** /**
* Example configuration for configuring two data sources with what Spring Boot does * Example configuration for configuring two data sources with what Spring Boot does in
* in auto-configuration. * auto-configuration.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@ -50,9 +50,7 @@ public class CompleteTwoDataSourcesExample {
@Primary @Primary
@ConfigurationProperties("app.datasource.foo") @ConfigurationProperties("app.datasource.foo")
public DataSource fooDataSource() { public DataSource fooDataSource() {
return fooDataSourceProperties() return fooDataSourceProperties().initializeDataSourceBuilder().build();
.initializeDataSourceBuilder()
.build();
} }
@Bean @Bean
@ -61,13 +59,10 @@ public class CompleteTwoDataSourcesExample {
return new DataSourceProperties(); return new DataSourceProperties();
} }
@Bean @Bean
@ConfigurationProperties("app.datasource.bar") @ConfigurationProperties("app.datasource.bar")
public DataSource barDataSource() { public DataSource barDataSource() {
return barDataSourceProperties() return barDataSourceProperties().initializeDataSourceBuilder().build();
.initializeDataSourceBuilder()
.build();
} }
// end::configuration[] // end::configuration[]

View File

@ -52,10 +52,10 @@ public class ConfigurableDataSourceExample {
@ConfigurationProperties("app.datasource") @ConfigurationProperties("app.datasource")
public HikariDataSource dataSource(DataSourceProperties properties) { public HikariDataSource dataSource(DataSourceProperties properties) {
return (HikariDataSource) properties.initializeDataSourceBuilder() return (HikariDataSource) properties.initializeDataSourceBuilder()
.type(HikariDataSource.class) .type(HikariDataSource.class).build();
.build();
} }
// end::configuration[] // end::configuration[]
} }
} }

View File

@ -43,8 +43,7 @@ public class SimpleDataSourceExample {
@ConfigurationProperties("app.datasource") @ConfigurationProperties("app.datasource")
public HikariDataSource dataSource() { public HikariDataSource dataSource() {
return (HikariDataSource) DataSourceBuilder.create() return (HikariDataSource) DataSourceBuilder.create()
.type(HikariDataSource.class) .type(HikariDataSource.class).build();
.build();
} }
// end::configuration[] // end::configuration[]

View File

@ -53,18 +53,14 @@ public class SimpleTwoDataSourcesExample {
@Primary @Primary
@ConfigurationProperties("app.datasource.foo") @ConfigurationProperties("app.datasource.foo")
public DataSource fooDataSource() { public DataSource fooDataSource() {
return fooDataSourceProperties() return fooDataSourceProperties().initializeDataSourceBuilder().build();
.initializeDataSourceBuilder()
.build();
} }
@Bean @Bean
@ConfigurationProperties("app.datasource.bar") @ConfigurationProperties("app.datasource.bar")
public BasicDataSource barDataSource() { public BasicDataSource barDataSource() {
return (BasicDataSource) DataSourceBuilder.create() return (BasicDataSource) DataSourceBuilder.create()
.type(BasicDataSource.class) .type(BasicDataSource.class).build();
.build();
} }
// end::configuration[] // end::configuration[]

View File

@ -33,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Test for {@link BasicDataSourceExample}. * Test for {@link BasicDataSourceExample}.
*
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)

View File

@ -47,12 +47,10 @@ public class CompleteTwoDataSourcesExampleTests {
@Test @Test
public void validateConfiguration() throws SQLException { public void validateConfiguration() throws SQLException {
assertThat(this.context.getBeansOfType(DataSource.class)).hasSize(2); assertThat(this.context.getBeansOfType(DataSource.class)).hasSize(2);
DataSource dataSource = this.context.getBean(DataSource.class); DataSource dataSource = this.context.getBean(DataSource.class);
assertThat(this.context.getBean("fooDataSource")).isSameAs(dataSource); assertThat(this.context.getBean("fooDataSource")).isSameAs(dataSource);
assertThat(dataSource.getConnection().getMetaData().getURL()) assertThat(dataSource.getConnection().getMetaData().getURL())
.startsWith("jdbc:h2:mem:"); .startsWith("jdbc:h2:mem:");
DataSource barDataSource = this.context.getBean("barDataSource", DataSource barDataSource = this.context.getBean("barDataSource",
DataSource.class); DataSource.class);
assertThat(barDataSource.getConnection().getMetaData().getURL()) assertThat(barDataSource.getConnection().getMetaData().getURL())

View File

@ -23,8 +23,8 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/** /**
* A sample {@link SpringBootConfiguration} that only enables the auto-configuration * A sample {@link SpringBootConfiguration} that only enables the auto-configuration for
* for the {@link DataSource}. * the {@link DataSource}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */

View File

@ -38,8 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(properties = { @SpringBootTest(properties = { "app.datasource.bar.url=jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1",
"app.datasource.bar.url=jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1",
"app.datasource.bar.max-total=42" }) "app.datasource.bar.max-total=42" })
@Import(SimpleTwoDataSourcesExample.SimpleDataSourcesConfiguration.class) @Import(SimpleTwoDataSourcesExample.SimpleDataSourcesConfiguration.class)
public class SimpleTwoDataSourcesExampleTests { public class SimpleTwoDataSourcesExampleTests {
@ -50,16 +49,13 @@ public class SimpleTwoDataSourcesExampleTests {
@Test @Test
public void validateConfiguration() throws SQLException { public void validateConfiguration() throws SQLException {
assertThat(this.context.getBeansOfType(DataSource.class)).hasSize(2); assertThat(this.context.getBeansOfType(DataSource.class)).hasSize(2);
DataSource dataSource = this.context.getBean(DataSource.class); DataSource dataSource = this.context.getBean(DataSource.class);
assertThat(this.context.getBean("fooDataSource")).isSameAs(dataSource); assertThat(this.context.getBean("fooDataSource")).isSameAs(dataSource);
assertThat(dataSource.getConnection().getMetaData().getURL()) assertThat(dataSource.getConnection().getMetaData().getURL())
.startsWith("jdbc:h2:mem:"); .startsWith("jdbc:h2:mem:");
BasicDataSource barDataSource = this.context.getBean("barDataSource", BasicDataSource barDataSource = this.context.getBean("barDataSource",
BasicDataSource.class); BasicDataSource.class);
assertThat(barDataSource.getUrl()) assertThat(barDataSource.getUrl()).isEqualTo("jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1");
.isEqualTo("jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1");
assertThat(barDataSource.getMaxTotal()).isEqualTo(42); assertThat(barDataSource.getMaxTotal()).isEqualTo(42);
} }