Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GenericDAO和NoSuchBeanDefinitionException:没有唯一的BeanSpring3.0_Spring_Genericdao - Fatal编程技术网

GenericDAO和NoSuchBeanDefinitionException:没有唯一的BeanSpring3.0

GenericDAO和NoSuchBeanDefinitionException:没有唯一的BeanSpring3.0,spring,genericdao,Spring,Genericdao,我将Spring3.0与genericDAO一起使用。我有这个: public interface GenericDAO<T, ID extends Serializable> ... @Repository("genericDAO") public class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> { ... public interface A ext

我将Spring3.0与genericDAO一起使用。我有这个:

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{
我得到:

应为单个匹配bean,但找到3:[genericDAOImpl,A,B]

我不明白为什么。我也试着用它来做

@Resource(name="A")
甚至

@Resource(type=A.class)
我也尝试过使用@Qualifier,但我总是得到相同的异常,看起来Spring总是在寻找GenericDao而不是特定的类

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

有什么建议吗

非常感谢。

@Repository(“AImpl”)
@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
公共类AImpl扩展了GenericDAOImpl,实现了{ ... @存储库(“BImpl”) 公共类BImpl扩展了GenericDAOImpl实现B{ ... @资源(name=“AImpl”) A随便;//我通常称之为aImpl

只要限定符名称匹配,您就可以继续。错误是由自动连接注入引起的,接口在注入中实现了两次。

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

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>
公共接口GenericDAO{}
...
@存储库(“genericDAO”)
公共类GenericDAOImpl实现GenericDAO{}
...
公共接口A扩展了GenericDAO{}
...
公共接口B扩展了GenericDAO{}
...
@存储库(“A”)
公共类AImpl扩展了GenericDAOImpl,实现了一个{}
...
@存储库(“B”)
公共类BImpl扩展了GenericDAOImpl实现B{}
...
公共类TipoTurno{}
...
公共类Turno{}
...
@组成部分
公共类的东西{
@自动连线
私人A;
@自动连线
私人B,;
@自动连线
@限定符(value=“genericDAO”)
私有GenericDAO GenericDAO;
}
...
公共班机{
公共静态void main(字符串[]args){
ApplicationContext上下文=新的ClassPathXmlApplicationContext(“genericdao.xml”);
getBean(Thingy.class);
}
}
...
请注意,实际的错误消息比您提供的要长。阅读全文查找原因非常重要。Spring喜欢有5级甚至10级原因的异常堆栈跟踪。下面是我得到的消息的相关子字符串:

无法自动关联字段:genericdao.genericdao genericdao.Thingy.genericdao;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义类型为[genericdao.genericdao]的唯一bean:应为单个匹配bean,但找到3:[A,B,genericdao]


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

您确定在这三种情况下都会收到相同的错误消息吗?我可以看到如何获得“预期的单个匹配bean”@Autowired案例中的消息,但绝对不是@Resource(name=“A”)案例中的消息。是否可能您同时对getter和member变量进行了注释?非常感谢您的帮助。当我使用@Resource(name=“specific_name”)时,我已经对其进行了双重检查以回答您的问题,是的我得到org.springframework.beans.factory.NoSuchBean定义异常:未定义类型为[xxx.model.entities.dao.GenericDAO]的唯一bean:应为单个匹配bean,但找到3:[GenericDAO,A,B].关于你的问题,我没有注释getter。@Calamarbiefalo限定符名称与接口相同,这是wrong@NimChimpsky,我更改了限定符名称,而不是A I set cat,而是B I set dog,这次它再次失败,说:应该是单个匹配的bean,但找到了3:[genericDAO,cat,dog]。我尝试使用@Resource执行此操作(name=“cat”)。删除@Repository(“genericDAO”)注释。您不需要将其定义为bean。谢谢您,但正如前面所说,当同时使用@Resource(name=“specific\u name”)和@Resource(type=specific\u class.class)时,我会得到完全相同的异常@Calamarbiefalo看看我所做的更改。你的接口名称与限定符相同,这导致了问题。按照我概述的方式进行操作,效果很好。正如我在上一次答复中所说,它仍然不起作用。但无论如何,非常感谢@NimChimpsky。我已经用实现名称和随机名称进行了尝试。@Calamarbiefalo我无法复制它您使用的是限定符名称吗?GenericDAO“实现了两次”在AImpl上-这会导致自动连接时出现问题。@Calamarbiefalo您需要删除自动连接注入。并且只使用限定符注入。再看看重构,它有点复杂,是由apiIt设计的。事实上,问题是我试图在不限定它的情况下注入“genericDAO”。但这让我发疯,因为我是一个限定符我想在具体的注射中解决这个问题。非常感谢@John Watts,非常感谢你的帮助。我不能投票支持你的答案,因为我没有声誉:'(不客气。感谢您接受我的回答。我希望有关spring异常的评论会有所帮助。尽管它们通常包含必要的信息,但它可能会在原因的海洋中丢失。仔细阅读异常跟踪是我发现错误的方式,所以是的,您的评论真的很有帮助。
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>