我有这个超级班:

@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的其他类混在一起。这样对吗?

谢谢。

最佳答案

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

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

要指定要在您的 class 中插入的bean(DAOBasevoceDAO)。

09-25 21:01