pull/8196/head
Phillip Webb 8 years ago
parent daf6be46f6
commit 2cf93f89f5

@ -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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
@ -1961,9 +1963,9 @@
<version>${mongodb.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jasper</groupId>
<artifactId>apache-el</artifactId>
<version>${jetty-el.version}</version>
<groupId>org.mortbay.jasper</groupId>
<artifactId>apache-el</artifactId>
<version>${jetty-el.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
@ -2669,4 +2671,4 @@
<id>integration-test</id>
</profile>
</profiles>
</project>
</project>

@ -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 create one of the standard data sources (if it is on the classpath). The builder can
detect the one to use based on the ones available on the classpath and it also auto
detects the driver based on the JDBC url.
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). The builder can
detect the one to use based on what's available on the classpath. It also auto detects the
driver based on the JDBC url.
[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
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
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:
[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.
[[howto-use-spring-data-repositories]]
=== Use Spring Data repositories
Spring Data can create implementations for you of `@Repository` interfaces of various

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save