zoukankan      html  css  js  c++  java
  • @Qualifier 注解不起作用

     Animal  动物类接口

    package cn.blogspring.chapter2.pojo.definition;
    
    /**
     * @Author: qiuj
     * @Description:动物接口
     * @Date: 2018-12-29 19:41
     */
    public interface Animal {
        void use();
    }
    

     实现类  :dog

    package cn.blogspring.chapter2.pojo;
    
    import cn.blogspring.chapter2.pojo.definition.Animal;
    import org.springframework.context.annotation.Primary;
    import org.springframework.stereotype.Component;
    
    /**
     * @Author: qiuj
     * @Description:
     * @Date: 2018-12-29 19:45
     */
    @Component()
    public class Dog implements Animal {
    
        @Override
        public void use() {
            System.out.println("狗【"+ Dog.class.getSimpleName()+"】是看门用的。");
        }
    
    
    }
    

    实现类  :cat 

    package cn.blogspring.chapter2.pojo;
    
    import cn.blogspring.chapter2.pojo.definition.Animal;
    import org.springframework.context.annotation.Primary;
    import org.springframework.stereotype.Component;
    
    /**
     * @Author: qiuj
     * @Description:
     * @Date: 2018-12-29 20:10
     */
    @Component
    public class Cat implements Animal {
        @Override
        public void use() {
            System.out.println("猫【"+ Cat.class.getSimpleName() +"】是抓老鼠。");
        }
    }
    

     BussinessPerson 人类实现类

    package cn.blogspring.chapter2.pojo;
    
    
    import cn.blogspring.chapter2.pojo.definition.Animal;
    import cn.blogspring.chapter2.pojo.definition.Person;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    /**
     * @Author: qiuj
     * @Description:    人类实现类
     * @Date: 2018-12-29 19:42
     */
    @Component
    public class BussinessPerson implements Person {
        
        @Autowired
        @Qualifier("cat")
        private Animal animal = null;
    
        @Override
        public void service() {
            this.animal.use();
        }
    
        @Autowired
        @Override
        public void setAnimal(Animal animal) {
            this.animal = animal;
        }
    }
    

    当人类类   设置   animal  动物类的时候,由于有两个实现类  , 产生了歧义性 ,Ioc  容器不知道注入哪个,

    使用 @Qualifier("cat") 设置为注入  cat的Bean

    却发现始终注入不进去  , 异常信息:发现了两个 实现类  不知道如何区分

    20:59:24.179 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bussinessPerson': Unsatisfied dependency expressed through method 'setAnimal' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.blogspring.chapter2.pojo.definition.Animal' available: expected single matching bean but found 2: cat,dog
    Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bussinessPerson': Unsatisfied dependency expressed through method 'setAnimal' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.blogspring.chapter2.pojo.definition.Animal' available: expected single matching bean but found 2: cat,dog
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:676)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
    	at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88)
    	at cn.blogspring.chapter2.config.IocTest.main(IocTest.java:36)
    Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.blogspring.chapter2.pojo.definition.Animal' available: expected single matching bean but found 2: cat,dog
    	at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:217)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1215)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:668)
    	... 14 more
    

     结果导致原因在于  虚拟机在初始化类的时候,先执行的流程是所有的 @Autowired  

    所以当走到set 方法上的 Autowired ,Ioc 容器在这时候就要做决定了,   就不知道选择那个实现类了

        @Override
        @Autowired
        public void setAnimal(Animal animal) {
            this.animal = animal;
        }
    

    所以 去掉 set方法上的 @Autowired  ,不要让他在set方法就进行注入,就可以了

  • 相关阅读:
    51nod 1087 1 10 100 1000(找规律+递推+stl)
    51nod 1082 与7无关的数 (打表预处理)
    51 nod 1080 两个数的平方和
    1015 水仙花数(水题)
    51 nod 1003 阶乘后面0的数量
    51nod 1002 数塔取数问题
    51 nod 1001 数组中和等于K的数对
    51 nod 1081 子段求和
    51nod 1134 最长递增子序列 (O(nlogn)算法)
    51nod 1174 区间中最大的数(RMQ)
  • 原文地址:https://www.cnblogs.com/blogspring/p/14191780.html
Copyright © 2011-2022 走看看