配置文件:
spring.datasource.app.oracle.jdbc-url =jdbc:oracle:thin:@knight.com:1521/RAC
spring.datasource.app.oracle.username = app
spring.datasource.app.oracle.password = 1234
spring.datasource.app.oracle.driver-class-Name = oracle.jdbc.driver.OracleDriver
spring.datasource.crm.oracle.jdbc-url =jdbc:oracle:thin:@knight.com:1521/RAC
spring.datasource.crm.oracle.username = crm
spring.datasource.crm.oracle.password = 1234
spring.datasource.crm.oracle.driver-class-Name = oracle.jdbc.driver.OracleDriver
config配置:
@Configuration
public class DataSourceConfig {
@Bean(name = "appDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.app.oracle")
public DataSource appDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "crmDataSource")
@ConfigurationProperties(prefix = "spring.datasource.crm.oracle")
public DataSource crmDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name="appJdbcTemplate")
public JdbcTemplate appJdbcTemplate (
@Qualifier("appDataSource") DataSource appDataSource ) {
return new JdbcTemplate(appDataSource);
}
@Bean(name = "crmJdbcTemplate")
public JdbcTemplate crmJdbcTemplate(
@Qualifier("crmDataSource") DataSource crmDataSource){
return new JdbcTemplate(crmDataSource);
}
}
在controller中引用:
@Autowired
@Qualifier("appJdbcTemplate")
public JdbcTemplate appJdbcTemplate;
@Autowired
@Qualifier("crmJdbcTemplate")
public JdbcTemplate crmJdbcTemplate;