SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解
一、
The @Qualifier annotation is the main way to work with qualifiers. It can be
applied alongside @Autowired or @Inject at the point of injection to specify which
bean you want to be injected. For example, let’s say you want to ensure that the
IceCream bean is injected into setDessert() :
@Autowired
@Qualifier("iceCream")
public void setDessert(Dessert dessert) {
this.dessert = dessert;
}
id为“iceCream”的bean会被注入
二、
以id为筛选条件,则万一类名一更改,则此自动装配会失效(@componet默认是以类名首字母小写为bean的id),另一相对的较好的做好是在bean的定义端也写@Qualifier,但不是指明id,而是指明特性
@Component
@Qualifier("cold")
public class IceCream implements Dessert { ... }
或
@Bean
@Qualifier("cold")
public Dessert iceCream() {
return new IceCream();
}
注入
@Autowired
@Qualifier("cold")
public void setDessert(Dessert dessert) {
this.dessert = dessert;
}
三、
但上述的例子,有多个合适的bean都标明@Qualifier("cold")时,还是会产生歧义,此时要自定义注解来解决
1.
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD,
ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Cold { }
2.
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD,
ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Creamy { }
Now you can revisit IceCream and annotate it with @Cold and @Creamy , like this:
@Component
@Cold
@Creamy
public class IceCream implements Dessert { ... }
Similarly, the Popsicle class can be annotated with @Cold and @Fruity
Similarly, the Popsicle class can be annotated with @Cold and @Fruity :
@Component
@Cold
@Fruity
public class Popsicle implements Dessert { ... }
Finally, at the injection point, you can use any combination of qualifier annotations
necessary to narrow the selection to the one bean that meets your specifications. To
arrive at the IceCream bean, the setDessert() method can be annotated like this:
@Autowired
@Cold
@Creamy
public void setDessert(Dessert dessert) {
this.dessert = dessert;
}
SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解的更多相关文章
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary
一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...
- 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-003-@Conditional根据条件生成bean及处理profile
一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...
随机推荐
- 动态添加组件(XML)
1.利用LayoutInflater的inflate动态加载XMLmLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID);La ...
- JS插件-日期
原文出处 源码下载 原文出处 源码下载
- java ssm框架入门(一)面向接口编程
因为工作上用到spring + strtus2 + mybatis ,所以开始学习下这个框架. 这里用到的是MySQL数据库 首先从web.xml 开始 <?xml version=" ...
- python中关于正则表达式一
ab+,描述一个'a'和任意个'b',那么'ab','abb','abbbbb' 正则表达式可以:1.验证字符串是否符合指定特征,比如验证是否是合法的邮件地址 2.用来查找字符串,从一个长的文本中查找 ...
- RAC分解步骤之一,在oracle linux 4u4上安装oracle 10.2.0.1.0操作日志
练习oracle的rac组建过程,第一步,先练习4u4上安装oracle 10.2.0.1.0.直接安装rac,有些难度.从简单的做起.总RAC步骤,参照小布老师的RAC组建. 1. 启动vc,登陆v ...
- Ngen生成Native代码实战及优缺点分析
先科普一下,.Net是一个用于Windows的托管代码模型,用于高效构建具有视觉上引人注目的用户体验的应用程序.但这个模型生成的代码并非可执行代码,而是由.Net公共语言运行库环境执行的IL代码.所以 ...
- UVA 11078 Open Credit System(扫描 维护最大值)
Open Credit System In an open credit system, the students can choose any course they like, but there ...
- Codevs 1066 引水入城 2010年NOIP全国联赛提高组
1066 引水入城 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 在一个遥远的国度 ...
- linux采用模块方法,添加一个新的设备
该文转载自:http://rangercyh.blog.51cto.com/1444712/521244 系统调用是操作系统内核和应用程序之间的接口,而设备驱动程序是操作系统内核和机器硬件之间的接口. ...
- [翻译][MVC 5 + EF 6] 1:创建数据模型
原文:Getting Started with Entity Framework 6 Code First using MVC 5 1.新建MVC项目: 2.修改Views\Shared\_Layou ...