@Configuration
public class DruidConfig
{
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DataSource masterDataSource()
{
return DruidDataSourceBuilder.create().build();
}
@Bean
@Qualifier("userDbDataSource")
@ConfigurationProperties("spring.datasource.druid.userDb")
@ConditionalOnProperty(prefix = "spring.datasource.druid.userDb", name = "enabled", havingValue = "true")
public DataSource userDbDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@Qualifier("slaveDataSource")
@ConfigurationProperties("spring.datasource.druid.slave")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
public DataSource slaveDataSource()
{
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource, DataSource userDbDataSource , DataSource slaveDataSource)
{
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
targetDataSources.put(DataSourceType.USERDB.name(), userDbDataSource);
targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
return new DynamicDataSource(masterDataSource, targetDataSources);
}
报错这里不是有提供解决办法嘛,给其中一个bean加上@Primary,或者在注入的地方写上bean的名字.
– 。淑女范erり 4年前