@ConfigurationProperties主要作用:就是綁定application.properties中的屬性
java代碼
@Configuration
public class DataSourceConfig { //數據源設置
@Bean(name = "primaryDataSource")//primary : 英文“主要的”
@Qualifier("primaryDataSource")//是一個合格者標識--表明那個類才是我們需要調用的類
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")//secondary : 英文“次要的”
@Qualifier("secondaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
application.properties代碼:
spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1 spring.datasource.primary.username=root spring.datasource.primary.password=root spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2 spring.datasource.secondary.username=root spring.datasource.secondary.password=root spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
