qualifiers
2019独角兽企业重金招聘Python工程师标准>>>
5.9.3 Fine-tuning annotation-based autowiring with qualifiers
Because autowiring by type may lead to multiple candidates, it is often necessary to have more control over the selection process.
因为按照类型来自动装配,可能会导致类型匹配重复的问题。
One way to accomplish this is with Spring’s @Qualifier
annotation. You can associate qualifier values with specific arguments, narrowing the set of type matches so that a specific bean is chosen for each argument. In the simplest case, this can be a plain descriptive value:
public class MovieRecommender { @Autowired@Qualifier("main")private MovieCatalog movieCatalog; // ...}
The @Qualifier
annotation can also be specified on individual constructor arguments or method parameters:
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(@Qualifier("main")MovieCatalog movieCatalog,CustomerPreferenceDao customerPreferenceDao) { this.movieCatalog = movieCatalog; this.customerPreferenceDao = customerPreferenceDao;} // ...}
The corresponding bean definitions appear as follows. The bean with qualifier value "main" is wired with the constructor argument that is qualified with the same value.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:annotation-config/><bean class="example.SimpleMovieCatalog"><qualifier value="main"/><!-- inject any dependencies required by this bean --></bean><bean class="example.SimpleMovieCatalog"><qualifier value="action"/><!-- inject any dependencies required by this bean --></bean><bean id="movieRecommender" class="example.MovieRecommender"/></beans>
For a fallback match, the bean name is considered a default qualifier value. Thus you can define the bean with an id "main" instead of the nested qualifier element, leading to the same matching result. However, although you can use this convention to refer to specific beans by name, @Autowired
is fundamentally about type-driven injection with optional semantic qualifiers. This means that qualifier values, even with the bean name fallback, always have narrowing semantics within the set of type matches; they do not semantically express a reference to a unique bean id. Good qualifier values are "main" or "EMEA" or "persistent", expressing characteristics of a specific component that are independent from the bean id, which may be auto-generated in case of an anonymous bean definition like the one in the preceding example.
Qualifiers also apply to typed collections, as discussed above, for example, to Set<MovieCatalog>
. In this case, all matching beans according to the declared qualifiers are injected as a collection. This implies that qualifiers do not have to be unique; they rather simply constitute filtering criteria. For example, you can define multiple MovieCatalog
beans with the same qualifier value "action"; all of which would be injected into a Set<MovieCatalog>
annotated with@Qualifier("action")
.
转载于:https://my.oschina.net/u/2308739/blog/469922
qualifiers相关推荐
- error: passing ‘const xxx’ as ‘this’ argument discards qualifiers c++primer 5th文本查询程序一个错误请各位指教(已解决)
文件main.cc 文件main.cc #include <string> #include <iostream> #include <memory> #inclu ...
- C++编程常见问题—error: passing 'const std::map]' discards qualifiers或pass-by-reference-to-const-map导致的“d
产生问题的场景: int func(const map<int, string> &aMap) { string value = amap[0]; } 或者 int Test:: ...
- OpenCL Function Qualifiers (函数限定符)
OpenCL Function Qualifiers (函数限定符) OpenCL 3.0 Reference Pages -> OpenCL Compiler -> Function Q ...
- C++ ERROR:error: passing 'XXX' as 'this' argument of 'XXX' discards qualifiers
遇到了如题目的这种错误: error: passing 'XXX' as 'this' argument of 'XXX' discards qualifiers 如何产生的? 我将一个const变量 ...
- C语言编译时产生的警告:initializing ‘char *‘ with an expression of type ‘const char *‘ discards qualifiers
警告的产生: char *my_strstr(const char *str1,const char *str2) {const char *s1 = NULL;const char *s2 = NU ...
- 翻译:Identifier Qualifiers标识限定符
本文为mariadb官方手册:Identifier Qualifiers的译文. 原文:https://mariadb.com/kb/en/library/identifier-qualifiers/ ...
- error: passing xxx as 'this' argument of xxx discards qualifiers的解决办法
写demo的时候碰到一个C++编译报错,初见也是奇怪了,编译器指向我调用类成员函数出错了. 然后百度看了下别人的博客瞬间就醍醐灌顶了,原来是编译器认为成员函数可能会修改被const限定的变量 解决办法 ...
- Error: “incorrect inclusion of a cudart header file”
CUDA header files with such qualifiers should ONLY be included in *.cu files.
- Linux tcpdump命令详解与Wireshark
简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...
- 【C++】动态内存管理/move/以及移动构造与移动赋值运算符
文章目录 1 .对象移动与右值引用 实际应用过程中遇到的问题及其解决方案 c++中临时变量不能作为非const的引用参数 2. 动态内存管理类 3. 对象移动与右值引用 4. 移动构造与移动复制运算符 ...
最新文章
- 微信小程序swiper组件宽高自适应方法
- 【T07】不要低估tcp的性能
- Visual Studio 2017 15.8概览
- boost::geometry::transform用法的测试程序
- yii2事务运用举例
- luoguP4755 Beautiful Pair
- 库克去年薪酬约6亿元,超员工1400倍,机构建议其降薪
- 笔记本电脑散热器声音很大怎么办_笔记本电脑长时间高温运行有什么危害
- java中if条件中删除此行代码_Java中我如何去除if...else...语句?
- NHibernate Issues之1898:命名查询
- 简要html漂浮广告代码,JS漂浮广告代码
- AotucCrawler 快速爬取美女图片
- 架构师的主要职责是什么?
- SPSS怎么筛选无效数据
- Swift语言针对机器学习
- 成熟的项目架构设计是什么样的?
- 克服焦虑--图解JVM内存模型和JVM线程模型
- Topic test not present in metadata after 60000 ms
- 实现SSO单点登录的思考
- WebWork教程一