转载 :
Spring中Bean装配的歧义性
本文链接:https://blog.youkuaiyun.com/u010502101/article/details/76563699
在spring容器中有多个同类的Bean时,该如何装载呢?例如有一个服务接口A,该接口有3个实现类,在容器中就会生成3个A的实现类的Bean,当对A进行装载时,容器不会判断装载哪一个,就会报一个没有唯一的一个Bean。要想解决此问题,最简单的方法就是用Primary注解,Primary注解表示优先装载Bean。但是最方便的方法是指定装载Bean的名称。下面用代码演示
一、优先装载
1、服务接口
package com.spring.primary.annotation;
public interface Animal {
void eat();
}
2、接口的实现类
总共有猪、狗、猫实现接口
猪:
package com.spring.primary.annotation;
import org.springframework.context.annotation.Primary;import org.springframework.stereotype.Component;
@Component@Primary //当容器中有多个Bean同类的Bean时,优先注入该Beanpublic class Pig implements Animal {
@Override
public void eat() {
System.out.println("我是猪,我吃猪食");
}
}
猫:
package com.spring.primary.annotation;
import org.springframework.stereotype.Component;
@Componentpublic class Cat implements Animal {
@Override
public void eat() {
System.out.println("我是猫,我吃猫食");
}
}
狗:
package com.spring.primary.annotation;
import org.springframework.stereotype.Component;
@Componentpublic class Dog implements Animal {
@Override
public void eat() {
System.out.println("我是狗,我吃狗食");
}
}
3、配置类
新建一个配置类,用于配置要装载的Bean。
package com.spring.primary.annotation;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;
@Configuration@ComponentScan//默认扫描所在的包public class AnnotationConfig {
private Animal animal;
@Autowired
//容器中有三个关于Animal的Bean,优先注入猪的那个Bean
public void setAnimal(Animal animal) {
this.animal = animal;
}
}
4、测试方法如下
package com.spring.primary.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AnnotationConfig.class);
Animal animal = ctx.getBean(Animal.class);
//此处获取到的Bean是猪的Bean
animal.eat();
}
}
5、输出结果如下
我是猪,我吃猪食
6、总结
从上面实例可以看出,注解Primary一定程度上避免了容器中有多个同类型的Bean时,不知道注入哪个Bean的程序异常。但是这不是最好的方法,下面用限定名装载Bean。
二、限定名装载Bean
1、服务接口不变,实现类分别为:
猪
package com.spring.primary.annotation;
import org.springframework.context.annotation.Primary;import org.springframework.stereotype.Component;
@Component("pigIml")public class Pig implements Animal {
@Override
public void eat() {
System.out.println("我是猪,我吃猪食");
}
}
狗和猫的限定名分别改为:
@Component(“dogIml”)和@Component(“catIml”)
2、配置类
package com.spring.primary.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
//默认扫描所在的包
public class AnnotationConfig {
}
3、测试类
package com.spring.primary.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AnnotationConfig.class);
Animal animal = (Animal) ctx.getBean("pigIml");
animal.eat();
}
}
4、输出结果为
我是猪,我吃猪食
因此从上面对比可以看出,优先用限定名的Bean去装载,更容易。