日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

错误记录:expected single matching bean but found 2

發(fā)布時間:2024/7/23 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 错误记录:expected single matching bean but found 2 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

springboot項目,之前有mysql數(shù)據(jù)源,現(xiàn)在又新增了clickhouse數(shù)據(jù)源,于是

新增了一個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;}

用這個bean的時候:

@@Autowiredprivate ClickHouseDataSource clickHouseDataSource;

但是這樣就報錯了,如下:

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),是因為Spring認為這兩個bean是同一類型的bean

看ClickHouseDataSource這個類的源碼:

public class ClickHouseDataSource implements DataSource{... }

看上面那個DataSource返回的真正實體類:HikariDataSource源碼(注:Hikari連接池,號稱是java平臺最快的,替換druid)

public class HikariDataSource extends HikariConfig implements DataSource, Closeable {... }

發(fā)現(xiàn)ClickHouseDataSource也是實現(xiàn)了DataSource類,而上面那個HikariDataSource也是實現(xiàn)了HikariDataSource ,所以這也算bean重復了。所以得在Bean定義的時候分別申明。
但由于之前這個DataSource,已經(jīng)有很多地方在用了,改的時候不方便,所以就采用默認是HikariDataSource,用ClickHouseDataSource的時候,再手動申明的策略。

解決:

原來DataSource的Bean定義上,再加注解@Primary,如下:

@Bean@Primarypublic DataSource dataSource() throws PropertyVetoException {......return dataSource;}

用到ClickhouseDataSource Bean的地方,用@Resource注入,指明name是哪個方法名。

@Resource(name = "getClickHouseDataSource")private ClickHouseDataSource clickHouseDataSource;

再次啟動,ok

總結

以上是生活随笔為你收集整理的错误记录:expected single matching bean but found 2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。