亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring-Boot:錯誤注入多個bean

Spring-Boot:錯誤注入多個bean

慕村225694 2023-08-04 15:26:50
我正在嘗試讓應用程序在 Spring-boot 中工作,但遇到注入錯誤。我有一個帶有一些@Autowire 類的@Service。我們的類只是帶有一個public setDatSource方法的 POJO,我需要通過運行時設置數據源。見下文:    @Bean    @Qualifier("datasetDao")    public com.lexi.dao.core.DatasetDAO getDatasetDao() throws NamingException {        DatasetDAOImpl ds = new DatasetDAOImpl();        ds.setDataSource(createAuthReadDataSoure());        return ds;    }    @Bean    public LicenseDAO getLicenseDao() throws NamingException {        LicenseDAOImpl ds = new LicenseDAOImpl();        ds.setReadDataSource(createOnlineDSReadDataSoure());        ds.setWriteDataSource(createOnlineDSWriteDataSoure());        ds.setDistribDataSource(createAuthReadDataSoure());        return ds;    }我有一個服務定義如下:@Servicepublic class LicenseService {    @Autowired    @Qualifier("datasetDao")    private DatasetDAO datasetDao;    @Autowired    private LicenseDAO licenseDao;但是,當我運行該應用程序時,我得到以下信息:***************************APPLICATION FAILED TO START***************************Description:Field datasetDao in com.wk.online.services.LicenseService required a single bean, but 3 were found:    - createAuthReadDataSoure: defined by method 'createAuthReadDataSoure' in com.wk.online.ws.OnlineWsApplication    - createOnlineDSReadDataSoure: defined by method 'createOnlineDSReadDataSoure' in com.wk.online.ws.OnlineWsApplication    - createOnlineDSWriteDataSoure: defined by method 'createOnlineDSWriteDataSoure' in com.wk.online.ws.OnlineWsApplicationAction:Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed我嘗試添加一個@Qualifier,但這似乎與Spring 不一致。我錯過了什么,我已經這樣做了一段時間,并認為我正在做一些非常愚蠢的事情。
查看完整描述

3 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

定義 bean 時,您需要指定名稱,而不是限定符,應在自動裝配它的地方使用限定符注釋:


@Bean(name = "datasetDao")

public com.lexi.dao.core.DatasetDAO getDatasetDao() throws NamingException {

    DatasetDAOImpl ds = new DatasetDAOImpl();

    ds.setDataSource(createAuthReadDataSoure());


    return ds;

}


查看完整回答
反對 回復 2023-08-04
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

您在類@Bean中對以下方法有注釋嗎?OnlineWsApplication

  • createAuthReadDataSoure

  • createOnlineDSReadDataSoure

  • createOnlineDSWriteDataSoure

如果是的話,擺脫它們。

完整的代碼OnlineWsApplication對于入侵它非常有用。


查看完整回答
反對 回復 2023-08-04
?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

在bean定義中,用@Qualifier("datasetDao")代替@Bean

嘗試使用以下內容:@Bean(name="datasetDao")


查看完整回答
反對 回復 2023-08-04
  • 3 回答
  • 0 關注
  • 252 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號