错误记录:expected single matching bean but found 2
springboot項(xiàng)目,之前有mysql數(shù)據(jù)源,現(xiàn)在又新增了clickhouse數(shù)據(jù)源,于是
新增了一個(gè)clickhouseDatasource的配置bean,如下:
@Beanpublic DataSource dataSource() throws PropertyVetoException {HikariConfig config = new HikariConfig();String jdbcUrl = ConfigCentre.getString(ConfigConst.EC_MYSQL_URL);if (StringUtils.isBlank(jdbcUrl)) {return null;}String userName = ConfigCentre.getString(ConfigConst.EC_MYSQL_USERNAME);String password = ConfigCentre.getString(ConfigConst.EC_MYSQL_PASSWORD);config.setJdbcUrl(jdbcUrl);config.setUsername(userName);config.setPassword(password);config.addDataSourceProperty("cachePrepStmts", "true");config.addDataSourceProperty("prepStmtCacheSize", "250");config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");HikariDataSource dataSource = new HikariDataSource(config);return dataSource;}@Beanpublic ClickHouseDataSource getClickHouseDataSource(){ClickHouseProperties properties = new ClickHouseProperties();properties.setDataTransferTimeout(5*60*1000);properties.setMaxExecutionTime(5*60*1000);properties.setUseTimeZone("UTC");properties.setUseServerTimeZone(false);ClickHouseDataSource clickHouseDataSource = new ClickHouseDataSource(CLICKHOUSEFORADRESPONSEURL, properties);return clickHouseDataSource;}用這個(gè)bean的時(shí)候:
@@Autowiredprivate ClickHouseDataSource clickHouseDataSource;但是這樣就報(bào)錯(cuò)了,如下:
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getClickHouseDataSource' defined in com.yeahmobi.dsp.DBBeans: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: dataSource,getClickHouseDataSource主要提示在DataSource … but but found 2:dataSource,getClickHouseDataSource
最后發(fā)現(xiàn),是因?yàn)镾pring認(rèn)為這兩個(gè)bean是同一類型的bean
看ClickHouseDataSource這個(gè)類的源碼:
public class ClickHouseDataSource implements DataSource{... }看上面那個(gè)DataSource返回的真正實(shí)體類:HikariDataSource源碼(注:Hikari連接池,號稱是java平臺最快的,替換druid)
public class HikariDataSource extends HikariConfig implements DataSource, Closeable {... }發(fā)現(xiàn)ClickHouseDataSource也是實(shí)現(xiàn)了DataSource類,而上面那個(gè)HikariDataSource也是實(shí)現(xiàn)了HikariDataSource ,所以這也算bean重復(fù)了。所以得在Bean定義的時(shí)候分別申明。
但由于之前這個(gè)DataSource,已經(jīng)有很多地方在用了,改的時(shí)候不方便,所以就采用默認(rèn)是HikariDataSource,用ClickHouseDataSource的時(shí)候,再手動(dòng)申明的策略。
解決:
原來DataSource的Bean定義上,再加注解@Primary,如下:
@Bean@Primarypublic DataSource dataSource() throws PropertyVetoException {......return dataSource;}用到ClickhouseDataSource Bean的地方,用@Resource注入,指明name是哪個(gè)方法名。
@Resource(name = "getClickHouseDataSource")private ClickHouseDataSource clickHouseDataSource;再次啟動(dòng),ok
總結(jié)
以上是生活随笔為你收集整理的错误记录:expected single matching bean but found 2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JQuery初探
- 下一篇: [转载] --- Fastjson1.2