java - 为什么 NoUniqueBeanDefinitionException : No qualifying bean of type is defined: expected single matching bean but found 2

标签 java spring beancreationexception

在这个小应用程序中,我使用了 @Autowired 注释和 @Qualifier 注释来配置依赖项,但仍然会抛出异常,如下所述。

比萨类

public class Pizza {

    private Address deliverydest;

    @Autowired
    @Qualifier("ForPizza")
    public void setDeliverydest(Address deliverydest) {
        this.deliverydest = deliverydest;
    }
}

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>

抛出的异常是

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

.

现在为什么 Spring 不考虑使用 @Qualifier 注释来找到具有限定符 value="ForPizza" 的正确 bean dest1

最佳答案

尝试将以下内容添加到您的 Spring 配置中:

<context:annotation-config/>
<context:component-scan base-package="com.test.Shopping"/>

关于java - 为什么 NoUniqueBeanDefinitionException : No qualifying bean of type is defined: expected single matching bean but found 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24430346/

相关文章:

javascript - Spring Boot GetMapping 404

java - 使用 Spring boot 在 Listener 中调用使用 @RequestScope 注解的类

java - BeanCreationException 通过使用 tomcat 和 hibernate 获取本地数据

spring - NoUniqueBeanDefinitionException : No qualifying bean of type [javax. persistence.EntityManagerFactory] ​​已定义:预期单个匹配 bean

java - 为什么 PrintStream.close() 最终会被调用两次?

java - 查找从文件读取的字母数字数组中的数字元素并将其写入新文件

java - 在 javafx 中获取所有文本字段值和 id

java - Eclipse 将 SWT 按钮选择绑定(bind)到模型函数

java - Spring bean java.lang.NoSuchMethodError 错误

java - 使用 Spring JdbcTemplate 协调多个 Daos 之间的事务