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相关推荐

  1. error: passing ‘const xxx’ as ‘this’ argument discards qualifiers c++primer 5th文本查询程序一个错误请各位指教(已解决)

    文件main.cc 文件main.cc #include <string> #include <iostream> #include <memory> #inclu ...

  2. 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:: ...

  3. OpenCL Function Qualifiers (函数限定符)

    OpenCL Function Qualifiers (函数限定符) OpenCL 3.0 Reference Pages -> OpenCL Compiler -> Function Q ...

  4. C++ ERROR:error: passing 'XXX' as 'this' argument of 'XXX' discards qualifiers

    遇到了如题目的这种错误: error: passing 'XXX' as 'this' argument of 'XXX' discards qualifiers 如何产生的? 我将一个const变量 ...

  5. 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 ...

  6. 翻译:Identifier Qualifiers标识限定符

    本文为mariadb官方手册:Identifier Qualifiers的译文. 原文:https://mariadb.com/kb/en/library/identifier-qualifiers/ ...

  7. error: passing xxx as 'this' argument of xxx discards qualifiers的解决办法

    写demo的时候碰到一个C++编译报错,初见也是奇怪了,编译器指向我调用类成员函数出错了. 然后百度看了下别人的博客瞬间就醍醐灌顶了,原来是编译器认为成员函数可能会修改被const限定的变量 解决办法 ...

  8. Error: “incorrect inclusion of a cudart header file”

    CUDA header files with such qualifiers should ONLY be included in *.cu files.

  9. Linux tcpdump命令详解与Wireshark

    简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...

  10. 【C++】动态内存管理/move/以及移动构造与移动赋值运算符

    文章目录 1 .对象移动与右值引用 实际应用过程中遇到的问题及其解决方案 c++中临时变量不能作为非const的引用参数 2. 动态内存管理类 3. 对象移动与右值引用 4. 移动构造与移动复制运算符 ...

最新文章

  1. 微信小程序swiper组件宽高自适应方法
  2. 【T07】不要低估tcp的性能
  3. Visual Studio 2017 15.8概览
  4. boost::geometry::transform用法的测试程序
  5. yii2事务运用举例
  6. luoguP4755 Beautiful Pair
  7. 库克去年薪酬约6亿元,超员工1400倍,机构建议其降薪
  8. 笔记本电脑散热器声音很大怎么办_笔记本电脑长时间高温运行有什么危害
  9. java中if条件中删除此行代码_Java中我如何去除if...else...语句?
  10. NHibernate Issues之1898:命名查询
  11. 简要html漂浮广告代码,JS漂浮广告代码
  12. AotucCrawler 快速爬取美女图片
  13. 架构师的主要职责是什么?
  14. SPSS怎么筛选无效数据
  15. Swift语言针对机器学习
  16. 成熟的项目架构设计是什么样的?
  17. 克服焦虑--图解JVM内存模型和JVM线程模型
  18. Topic test not present in metadata after 60000 ms
  19. 实现SSO单点登录的思考
  20. WebWork教程一

热门文章

  1. 如何在 Python 中调用函数?九种方法任你挑选
  2. 圆锥形怎么画_如何画圆锥体的展开图?
  3. 如何批量压缩图片?这几个方法值得一试
  4. 解决VMware虚拟机安装Win10系统后无网络问题
  5. edge浏览器安装扩展插件报错:出现错误Download interrupted
  6. 【最简单】地图获取经纬度的办法
  7. Spring Cloud 与 Dubbo 功能对比
  8. 读取cpu温度的api_读取CPU核心温度
  9. 获取局域网电脑的硬件配置
  10. 综合使用公网/专网等频段!今日,工信部发布工业互联网和物联网无线电频率使用指南(附下载)...