【问题标题】:Why NoUniqueBeanDefinitionException: No qualifying bean of type is defined: expected single matching bean but found 2为什么 NoUniqueBeanDefinitionException:没有定义类型的限定 bean:预期的单个匹配 bean 但找到 2
【发布时间】:2026-02-11 05:25:01
【问题描述】:

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

Pizaa 类

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

【问题讨论】:

  • 因为AutowiredAnnotationBeanPostProcessor 不知道@Qualifier 注释。如果您确实想显式配置解析器,请使用 &lt;context:annotation-config /&gt; ro 添加 CustomAutowireConfigurer 以启用对 @Qualifier 的检测。
  • @Deinum,它很奇怪,因为我在这个link 中学习了一个教程,它在那里工作。

标签: java spring beancreationexception


【解决方案1】:

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

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

【讨论】:

  • 不!同样的例外。无论如何,我之前已经添加了 annotation.AutowiredAnnotationBeanPostProcessor
  • 编辑后,现在异常已更改为Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
  • 对不起,您的两个更改都有效。配置中有一个小错字
【解决方案2】:

尝试将@Qualifier 放在参数而不是方法上:

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

【讨论】:

    【解决方案3】:

    删除所有注释 bean 后处理器,只需在 xml 中添加以下内容。

    <context:annotation-config/>
    

    【讨论】:

      最近更新 更多