微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用限定符注入自动连接的依赖项失败

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

错误

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.sspathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClasspathXmlApplicationContext.sspathXmlApplicationContext.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,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,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
这是架构定义

spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

这是我想申请自动装配的地方.

 

添加了这些类.

    

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;
    }

}
最佳答案
我建议你删除< qualifier value =“CircleRelated”/>从xml中直接从id注入依赖项.在大多数情况下,xml中的id就足够了,限定符只能用于特定情况.惯例是使用小写字母作为id或限定符的第一个字母.

因此,将@Qualifier(“CircleRelated”)的CircleRelated更改为现有ID,如@Qualifier(“pointA”),@ Qualifier(“pointB”)或@Qualifier(“pointC”)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐