SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary
一、
假设有如下三个类实现同一个接口,则自动装配时会产生歧义
- @Component
- public class Cake implements Dessert { ... }
- @Component
- public class Cookies implements Dessert { ... }
- @Component
- public class IceCream implements Dessert { ... }
- @Autowired
- public void setDessert(Dessert dessert) {
- this.dessert = dessert;
- }
二、@Primary的3种用法
Let’s say that ice cream is your favorite dessert. You can express that favorite choice
in Spring using the @Primary annotation. @Primary can be used either alongside
@Component for beans that are component-scanned or alongside @Bean for beans
declared in Java configuration. For example, here’s how you might declare the
@Component -annotated IceCream bean as the primary choice:
1.在自动扫描
- @Component
- @Primary
- public class IceCream implements Dessert { ... }
2.在java配置文件中
- @Bean
- @Primary
- public Dessert iceCream() {
- return new IceCream();
- }
3.xml配置文件
- <bean id="iceCream"
- class="com.desserteater.IceCream"
- primary="true" />
如果有两个合适的bean都标记为@Primary,则Spring还是无法确定要装配哪个bean,这时要用@Qulifier来解决歧义
SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary的更多相关文章
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍
一. 1.SpEL expressions are framed with #{ ... } 2.SpEl的作用 Sp EL has a lot of tricks up its sleeves, ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value
一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)
一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode
一. Spring的bean默认是单例的 But sometimes you may find yourself working with a mutable class that does main ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles
一. Spring honors two separate properties when determining which profiles are active:spring.profiles. ...
- SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile
一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值
1.When injecting properties and constructor arguments on beans that are created via component-scanni ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解
一. The @Qualifier annotation is the main way to work with qualifiers. It can beapplied alongside @Au ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile
一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...
随机推荐
- C#三元运算符
?:表达式1?表达式2:表达式3如果1为真则执行2,为假执行3
- mysql的having语句
mysql> use qq; Database changed mysql> #查询本店价比市场价省的钱,并且要求省钱200元以上的取出来 mysql> select goods_i ...
- 浅谈C#随机数发生器
我们在做能自动生成试卷的考试系统时,常常需要随机生成一组不重复的题目,在.net Framework中提供了一个专门用来产生随机数的类System.Random. 对于随机数,大家都知道,计算机不 可 ...
- js 函数命名
1 函数命名可以使用匿名: var f=function(x){return x*2;} 2 可以使用变量: function double(x){return x*2;} 二者区别:后者会绑定到与其 ...
- Come and join us at English corner
2012.12.26 Hi all, How are you doing? Merry post-Christmas and happy upcoming New year!! I wish you ...
- JDBC——数据层DAO
DAO:Data Access Object DAO 数据层 Service 逻辑业务层 View 视图层 entity 实体层 实现增.删.改.查的数据层 public class EmpDA ...
- java.util.Map源码分析
/** * An object that maps keys to values. A map cannot contain duplicate keys; * each key can map to ...
- 23种设计模式全解析 (java版本)
转自:http://blog.csdn.net/longyulu/article/details/9159589 其中PHP常用的五种设计模式分别为:工厂模式,单例模式,观察者模式,策略模式,命令模式 ...
- 青瓷qici - H5小游戏 抽奖机 3 效果设置
现在是万事俱备,只欠东风,好,我们一起动手,先来东风东. 烟花粒子效果 第一个来实现我们的烟花粒子效果,点击我们的粒子,按照下图方式配置. 注意此时我们已经加入了white.png作为粒子特效使用. ...
- js 中对象属性特性2
对象的存储描述: get 和 set 方法 <script> var obj ={ get age(){ return 22 }, set age(value){ console. ...