mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge branch '1.4.x' into 1.5.x
This commit is contained in:
commit
71601c3b96
@ -1702,11 +1702,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"]
|
||||
----
|
||||
@ -1729,7 +1728,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]
|
||||
@ -1831,6 +1830,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…
Reference in New Issue
Block a user