@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方法就进行注入,就可以了