当前位置: 首页 > 知识库问答 >
问题:

NouniQueBeanDefinitionException:...需要一个匹配bean,但找到2:

孙朗
2023-03-14

我有这个超级班:

@Component
public class DAOBase {
}
@Component
public class VoceDAO extends DAOBase{       
}
@Service
public class TransactionService {
    @Autowired
    private DAOBase dAOBase;
}
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.jeansedizioni.dao.DAOBase] is defined: expected single matching bean but found 2: DAOBase,voceDAO
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:865)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 37 more
@Component("DAOBaseBeanName")
public class DAOBase {
}

我想确定我完全理解了解决方案。通过@component(“daoBaseBeanname”),我给类DAOBase指定了特定的名称“daoBaseBeanname”,应用程序可以用它来标识类DAOBase,这样就不会把它与扩展DAOBase的其他类混淆了。对吗?

谢谢你。

共有1个答案

龚迪
2023-03-14

尝试添加@qualifier注释,如下所示:

 @Autowired
 @Qualifier("dAOBase")
 private DAOBase dAOBase;

指定要在类中注入哪个bean(daobasevocedao)。

 类似资料: