Spring中Bean装配的歧义性
来源:互联网 发布:mac os 10.11原版镜像 编辑:程序博客网 时间:2024/06/05 17:52
在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去装载,更容易。
阅读全文
0 0
- Spring中Bean装配的歧义性
- spring--bean自动装配的歧义性
- Spring-处理自动装配的歧义性
- spring处理自动装配的歧义性
- spring中Bean的装配
- Spring学习笔记之处理自动装配的歧义性
- Spring 全注解配置 bean 和 调用 (4) @Primary 自动装配歧义性
- Spring 全注解配置 bean 和 调用 (5) @Qualifier 自动装配歧义性消除
- Spring 全注解配置 bean 和 调用 (6) 自定义注解 自动装配歧义性消除
- Spring中装配bean
- spring中bean的自动装配(6)
- spring中bean的注解装配(8)
- spring中bean的自动装配
- Spring中Bean的自动装配
- spring框架中Bean的自动装配
- Spring-Bean的装配
- Spring-bean的装配
- Spring的Bean装配
- CSS第三级选择器 Selectors Level 3 文档翻译
- 纯虚函数和模板函数
- 方向梯度直方图(HOG)
- ReactNative组件详解
- centos7 ssh远程登录服务器
- Spring中Bean装配的歧义性
- hbase和es在搜索场景的应用
- 04Ext js学习之第一个Ext js程序
- 简单易接入的Zookeeper
- 比特币中交易延展性问题
- Nginx配置文件解析
- Android 简单相机和照片查看器的制作
- 在应用层调用OKhttp进行数据请求,更新View
- java匿名类