Configure Two DataSources ---

简介: 67. Data Access 67.1 Configure a DataSource To override the default settings just define a @Bean of your own of type DataSource.

67. Data Access

67.1 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 e.g.

@Bean
@ConfigurationProperties(prefix="datasource.mine")
public DataSource dataSource() {
    return new FancyDataSource();
}
datasource.mine.jdbcUrl=jdbc:h2:mem:mydb
datasource.mine.user=sa
datasource.mine.poolSize=30

See Section 28.1, “Configure a DataSource” in the ‘Spring Boot features’ section and the DataSourceAutoConfiguration class for more details.

67.2 Configure Two DataSources

Creating more than one data source works the same as creating the first one. You might want to mark one of them as @Primary if you are using the default auto-configuration for JDBC or JPA (then that one will be picked up by any @Autowired injections).

@Bean
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="datasource.secondary")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

 

http://docs.spring.io/spring-boot/docs/1.2.0.BUILD-SNAPSHOT/reference/htmlsingle/

 

相关文章
|
5月前
|
Linux 编译器 Apache
make与 configure
【5月更文挑战第10天】make与 configure。
51 1
|
XML 前端开发 JavaScript
configure: error: cups-config not found
configure: error: cups-config not found
96 0
autoreconf: ‘configure.ac‘ or ‘configure.in‘ is required
autoreconf: ‘configure.ac‘ or ‘configure.in‘ is required
324 0
|
应用服务中间件 Linux nginx
./configure: error: C compiler cc is not found
./configure: error: C compiler cc is not found
779 0
|
JavaScript 前端开发