繁体   English   中英

春季启动:错误注入多个bean

[英]Spring-Boot: Error injection multiple beans

我正在尝试让应用程序在Spring-boot中运行,但是遇到注入错误。 我有一个带有几个@Autowire类的@Service。 使用public setDatSource方法(我们需要通过运行时设置DataSource)来对我们的POJO进行public setDatSource 见下文:

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

我有一个服务定义如下:

@Service
public 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.OnlineWsApplication


Action:

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无关。 我想念的是什么,我呆了一段时间,觉得自己做的事很愚蠢。

OnlineWsApplication类的以下方法上,您是否具有@Bean批注?

  • createAuthReadDataSoure
  • createOnlineDSReadDataSoure
  • createOnlineDSWriteDataSoure

如果是,请摆脱它们。

OnlineWsApplication完整代码对于调查它非常有用。

定义bean时,需要指定名称而不是限定符,在自动装配的位置应使用限定符注释:

@Bean(name = "datasetDao")
public com.lexi.dao.core.DatasetDAO getDatasetDao() throws NamingException {
    DatasetDAOImpl ds = new DatasetDAOImpl();
    ds.setDataSource(createAuthReadDataSoure());

    return ds;
}

在bean定义中,而不是@Bean @Qualifier(“ datasetDao”)

尝试使用以下命令:@Bean(name =“ datasetDao”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM