加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:27:26 所属栏目:大数据 来源:网络整理
导读:我已经尝试了所有可以在任何地方找到的选项,我查看了之前就该主题提出的所有问题并尝试了解决方案,但没有任何效果,我得到的就是这个错误. 错误 INFO: Loading XML bean definitions from class path resource [spring.xml]Exception in thread "main" org.sp

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

错误

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.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读