【发布时间】:2016-04-12 20:52:11
【问题描述】:
我有这个超类:
@Component
public class DAOBase {
}
另外一个类扩展了 DAOBase
@Component
public class VoceDAO extends DAOBase{
}
当我这样AutoWired类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 的其他类混淆。对吗?
谢谢。
【问题讨论】:
-
恕我直言,您的
DAOBase应该是abstract并且没有@Component注释...旁边,您应该在服务中使用具体类型VoceDao而不是基类.
标签: java spring-mvc autowired