@ConfigurationProperties 主要作用:绑定 application.properties 中的属性
例如:
@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
引用自:大象 https://www.cnblogs.com/MaxElephant/p/8182463.html