使用限定符注入自动装配的依赖项失败

Posted

技术标签:

【中文标题】使用限定符注入自动装配的依赖项失败【英文标题】:Injection of autowired dependencies failed with Qualifier 【发布时间】:2014-04-13 16:29:47 【问题描述】:

我已经尝试了所有可以在任何地方找到的选项,我查看了之前就该主题提出的所有问题并尝试了其中给出的解决方案,但没有任何效果,我得到的只是这个错误。

错误

INFO: Loading XML bean definitions from class path resource [spring.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circle': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void spring.springdemo.Circle.setCenter(spring.springdemo.Point); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.springdemo.Point] is defined: expected single matching bean but found 3: pointA,pointB,pointC
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at spring.springdemo.DrawingApp.main(DrawingApp.java:10)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void spring.springdemo.Circle.setCenter(spring.springdemo.Point); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.springdemo.Point] is defined: expected single matching bean but found 3: pointA,pointB,pointC
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 13 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.springdemo.Point] is defined: expected single matching bean but found 3: pointA,pointB,pointC
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:553)
    ... 15 more

我在这个例子中创建的文件如下。

spring.xml 这是架构定义

<?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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

这是我想要应用自动接线的地方。

 <bean id="circle" class="spring.springdemo.Circle">

</bean>

<bean id="pointA" class="spring.springdemo.Point">
    <qualifier value="CircleRelated"/>
    <property name="x" value="0"/>
    <property name="y" value="0"/>
</bean>
<bean id="pointB" class="spring.springdemo.Point">
        <property name="x" value="-20"/>
        <property name="y" value="0"/>
    </bean>
    <bean id="pointC" class="spring.springdemo.Point">
        <property name="x" value="20"/>
        <property name="y" value="0"/>
    </bean>

这些是我添加的类。

    <bean class="org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver"/>
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

Circle.java 该类,即对象将由 autowire 填充

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;


public class Circle implements Shape
    private Point center;

    @Override
    public void draw() 
        System.out.println("Drawing Circle");
        System.out.println("Point is : ("+center.getX()+", "+center.getY()+")");
    
    public Point getCenter() 
        return center;
    
    @Autowired
    @Qualifier("CircleRelated")
    public void setCenter(Point center) 
        this.center = center;
    


【问题讨论】:

你的 context.xml 中有 。检查弹簧参考章节 5.9。 docs.spring.io/spring/docs/3.2.0.RELEASE/… @MartinBaumgartner 这行&lt;bean class="org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver"/&gt; &lt;bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/&gt; 我添加了它们,我不应该需要&lt;context:annotation-config/&gt; 但我不得不请你告诉为什么? @MartinBaumgartner 为什么我需要通过编写 来提供所有 BeanPostProcessor 而我已经提供了 class="org.springframework.beans.factory.annotation.QualifierAnnotationAutowireC‌​andidateResolver"/ > bean。请解释一下...... 【参考方案1】:

我建议您从 xml 中删除 &lt;qualifier value="CircleRelated"/&gt; 并直接从 id 中注入依赖项。 xml 中的 id 在大多数情况下就足够了,限定符只能用于特定情况。并且约定是对 id 或限定符的第一个字母使用小写。

因此,将@Qualifier("CircleRelated") 中的CircleRelated 更改为现有的ID,例如@Qualifier("pointA")@Qualifier("pointB")@Qualifier("pointC")

【讨论】:

没关系,是的,ID 在其中非常独特,但在这种情况下它们不起作用。【参考方案2】:

添加以下代码行并确保添加上下文命名空间

<?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.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>

</beans>

【讨论】:

以上是关于使用限定符注入自动装配的依赖项失败的主要内容,如果未能解决你的问题,请参考以下文章

创建 bean 时出错。注入自动装配的依赖项失败。无法自动装配字段

Spring data jpa-未定义名为“entityManagerFactory”的bean;注入自动装配的依赖项失败

创建名为“securityConfig”的 bean 时出错:注入自动装配的依赖项失败

创建名为“homeController”的 bean 时出错:自动装配依赖项的注入失败

这是 MVC DAO 的正确方法吗?我收到类似自动装配依赖项注入失败的错误

限定符注解