我正在嘗試讓應用程序在 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;
}

白衣非少年
TA貢獻1155條經驗 獲得超0個贊
您在類@Bean
中對以下方法有注釋嗎?OnlineWsApplication
createAuthReadDataSoure
createOnlineDSReadDataSoure
createOnlineDSWriteDataSoure
如果是的話,擺脫它們。
完整的代碼OnlineWsApplication
對于入侵它非常有用。
添加回答
舉報
0/150
提交
取消