GenericDAO and NoSuchBeanDefinitionException: No unique bean



我正在使用带有通用DAO的Spring 3.0。我有这个:

public interface GenericDAO<T, ID extends Serializable>
...
@Repository("genericDAO")
public class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> {
...
public interface A extends GenericDAO<Turno, Long> {
...
public interface B extends GenericDAO<TipoTurno, Long> {
...
@Repository("A")
public class AImpl extends GenericDAOImpl<TipoTurno, Long>  implements A{
...
@Repository("B")
public class BImpl extends GenericDAOImpl<TipoTurno, Long>  implements B{

但是当我尝试按如下方式注射它们时:

@Autowired
A a;

我得到:

预期的单个匹配 bean,但找到 3:[泛型 DAOImpl, A, B]

我不明白为什么。我也尝试用

@Resource(name="A")

甚至

@Resource(type=A.class)

我也尝试使用@Qualifier但我总是得到相同的例外,看起来 Spring 一直在寻找通用道而不是特定的类。

但它仍然不起作用。我不明白为什么。

有什么建议吗?

谢谢。

我重现了您的确切错误消息,然后修复了它。这正是我使用的源代码,减去包名称。我建议复制它,运行它并与您的差异。我注意到的一个区别是,正如问题中所写,AImpl 无法编译。它实现了 GenericDAOImpl 和 GenericDAO。我将第一个泛型参数更改为 Turno,以使其编译。我认为这是一个错字。当我最初将所有字段设置为 @Autowired 时,我完全重现了您的错误。然后我添加了@Qualifier("通用DAO"),它成功地连接了这三个。

public interface GenericDAO<T, ID extends Serializable> {}
...
@Repository("genericDAO")
public class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> {}
...
public interface A extends GenericDAO<Turno, Long> {}
...
public interface B extends GenericDAO<TipoTurno, Long> {}
...
@Repository("A")
public class AImpl extends GenericDAOImpl<Turno, Long> implements A {}
...
@Repository("B")
public class BImpl extends GenericDAOImpl<TipoTurno, Long>  implements B {}
...
public class TipoTurno {}
...
public class Turno {}
...
@Component
public class Thingy {
    @Autowired
    private A a;
    @Autowired
    private B b;
    @Autowired
    @Qualifier(value="genericDAO")
    private GenericDAO genericDao;
}
...
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("genericdao.xml");
        context.getBean(Thingy.class);
    }
}
...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="genericdao"/>
</beans>

请注意,实际错误消息比您提供的错误消息长。阅读整件事寻找原因很重要。Spring 喜欢具有 5 级甚至 10 级原因的异常堆栈跟踪。这是我收到的消息的相关子字符串:

无法自动连线字段:泛型道。泛型DAO 泛型道。Thingy.genericDao;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有类型 [genericdao.泛型DAO]被定义:预期的单个匹配bean,但找到3:[A,B,泛型DAO]

它表示未自动连线的字段是Thingy.genericDao,而不是Thingy.a。我强烈怀疑你的错误也是如此。

@Repository("AImpl")
public class AImpl extends GenericDAOImpl<TipoTurno, Long>  implements A{
...
@Repository("BImpl")
public class BImpl extends GenericDAOImpl<TipoTurno, Long>  implements B{
...
@Resource(name = "AImpl")
A whatever; //I would normally call it aImpl

只要限定符名称匹配,您就可以开始了。该错误是由自动接线注入引起的,其中接口实现了两次。

最新更新