首页 新闻 会员 周边 捐助

springboot配置多数据源 required a single bean, but 2 were found:

0
[待解决问题]

@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);
}

Flower_Jun的主页 Flower_Jun | 初学一级 | 园豆:183
提问于:2020-04-23 15:38

报错这里不是有提供解决办法嘛,给其中一个bean加上@Primary,或者在注入的地方写上bean的名字.

。淑女范erり 4年前
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册