Java 为什么 NoUniqueBeanDefinitionException:没有定义类型的合格 bean:预期单个匹配 bean 但找到 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24430346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why NoUniqueBeanDefinitionException: No qualifying bean of type is defined: expected single matching bean but found 2
提问by aj_blk
In this small app, I have used @Autowired
annotation along with @Qualifier
annotation to config the dependencies, but still an exception is thrown as mentioned below.
在这个小应用程序中,我使用@Autowired
注解和@Qualifier
注解来配置依赖项,但仍然抛出异常,如下所述。
Pizaa class
比萨类
public class Pizza {
private Address deliverydest;
@Autowired
@Qualifier("ForPizza")
public void setDeliverydest(Address deliverydest) {
this.deliverydest = deliverydest;
}
}
Spring Context Configuration
Spring上下文配置
<?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-4.0.xsd">
<bean id="pizza" class="com.test.Shopping.Pizza" />
<bean id="Cust1Address" class="com.test.Shopping.Address" />
<bean id="dest1" class="com.test.Shopping.Address" >
<qualifier value="ForPizza" />
<property name="buildingno" value="flat1/door2" />
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
</beans>
The exception thrown is
抛出的异常是
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pizza': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.test.Shopping.Pizza.setDeliverydest(com.test.Shopping.Address); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] is defined: expected single matching bean but found 2: Cust1Address,dest1
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.test.Shopping.Pizza.setDeliverydest(com.test.Shopping.Address); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] is defined: expected single matching bean but found 2: Cust1Address,dest1
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] is defined: expected single matching bean but found 2: Cust1Address,dest1
.
.
Now why is the Spring not considering the @Qualifier
annotation to find the correct bean dest1having qualifier value="ForPizza"
?
现在为什么 Spring 不考虑@Qualifier
注释以找到具有 qualifier的正确 bean dest1value="ForPizza"
?
采纳答案by Vladimír Sch?fer
Try adding the following to your Spring Configuration:
尝试将以下内容添加到您的 Spring 配置中:
<context:annotation-config/>
<context:component-scan base-package="com.test.Shopping"/>
回答by axtavt
Try to place @Qualifier
on parameter rather than on method:
尝试放置@Qualifier
在参数而不是方法上:
@Autowired
public void setDeliverydest(@Qualifier("ForPizza") Address deliverydest) { ... }
回答by Arindam Mitra
Remove all annotation bean post processors and just add the below in xml.
删除所有注释 bean 后处理器,只需在 xml 中添加以下内容。
<context:annotation-config/>