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

前端之家收集整理的这篇文章主要介绍了使用限定符注入自动连接的依赖项失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

错误

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.Metadata.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
这是架构定义

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

 

添加了这些类.

    

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”)

原文链接:https://www.f2er.com/spring/432108.html

猜你在找的Spring相关文章