• 按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以指定单独的构造器参数或方法参数
  • 可用于注解集合类型变量

例子:

package com.mypackage;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;public class MovieRecommender {@Autowired@Qualifier("main")private MovieCatalog movieCatalog;}

package com.mypackage;import org.springframework.beans.factory.annotation.Qualifier;public class MovieRecommender {private MovieCatalog movieCatalog;public void prepare(@Qualifier("main")MovieCatalog movieCatalog){this.movieCatalog=movieCatalog;}}

PS:应用于构造器的方法比较常用

  • XML文件中使用qualifier:
<?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-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><context:component-scan base-package="com.multibean"></context:component-scan>   <bean class="com.mypackage.MovieCatalog"><qualifier value="main"></qualifier></bean><bean class="com.mypackage.MovieCatalog"><qualifier value="action"></qualifier></bean>
</beans>

  • 如果通过名字进行注解注入,主要使用的不是@Autowired(即使在技术上能够通过@Qualifier指定bean的名称),替代方式是使用JSR-250@Resource注解,它通过其独特的名称来定义来识别特定的目标(这是一个与所声明的类型是无关的匹配过程)
  • 因语义差异,集合或Map类型的bean无法通过@Autowired来注入,因为没有类型匹配到这样的bean,为这些bean使用@Resource注解,通过唯一名称引用集合或Map的bean
  • @Autowired适用于fields,constructors,multi-argument method这些允许在参数级别使用@Qualifier注解缩小范围的情况
  • @Resource适用于成员变量,只有一个参数的setter方法,所以在目标是构造器或者一个多参数方法时,最好的方式是使用@Qualifier

例子:

先定义一个BeanInterface接口

package com.multibean;public interface BeanInterface {}

在定义两个实现类

package com.multibean;import org.springframework.stereotype.Component;@Component
public class BeanInterfaceImpl implements BeanInterface {}

  

package com.multibean;import org.springframework.stereotype.Component;@Component
public class BeanInterface2Impl implements BeanInterface {}

定义BeanInvoker实现@Qualifier指定bean

package com.multibean;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;@Component
public class BeanInvoker {@Autowired@Qualifier("beanInterfaceImpl")private BeanInterface beanInterface;public void say(){if(null != beanInterface){System.out.println(beanInterface.getClass().getName());}else{System.out.println("BeanInterface is null.");}}}

单元测试:

package com.multibean;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class UnitTest {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beansnnotation.xml");BeanInvoker beanInvoker = (BeanInvoker)context.getBean("beanInvoker");beanInvoker.say();}
}

结果:

七月 06, 2015 11:41:38 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e397bcb: startup date [Mon Jul 06 23:41:38 CST 2015]; root of context hierarchy
七月 06, 2015 11:41:38 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
com.multibean.BeanInterfaceImpl

修改BeanInvoker

package com.multibean;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;@Component
public class BeanInvoker {@Autowired@Qualifier("beanInterface2Impl")private BeanInterface beanInterface;public void say(){if(null != beanInterface){System.out.println(beanInterface.getClass().getName());}else{System.out.println("BeanInterface is null.");}}}

结果:

七月 06, 2015 11:43:38 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e397bcb: startup date [Mon Jul 06 23:43:38 CST 2015]; root of context hierarchy
七月 06, 2015 11:43:38 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
com.multibean.BeanInterface2Impl

转载于:https://www.cnblogs.com/JsonShare/p/4625753.html

Spring学习(10)--- @Qualifier注解相关推荐

  1. Spring学习10之动态代理

    前言 优点 可以使真实角色的操作更加纯粹!不用去关注一些公共的业务 公共业务交给了代理,实现了业务的分工 公共业务发生拓展时,方便集中管理 缺点: 一个真实的角色就会产生一个代理,代码量翻倍,开发效率 ...

  2. spring学习12 -Spring 框架模块以及面试常见问题注解等

    以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式-在AOP和remoting中被用的比较 ...

  3. Spring学习总结(2)——Spring的常用注解

    2019独角兽企业重金招聘Python工程师标准>>> 本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-packag ...

  4. Spring学习(七)bean装配详解之 【通过注解装配 Bean】【自动装配的歧义解决】...

    本文借鉴:Spring学习,@Bean 的用法(特此感谢!) 自动装配 1.歧义性 我们知道用@Autowired可以对bean进行注入(按照type注入),但如果有两个相同类型的bean在IOC容器 ...

  5. @qualifier注解_常见的 Spring 注解概览

    点击上方 Java后端,选择 设为星标 优质文章,及时送达 从Java5.0开始,Java开始支持注解.Spring做为Java生态中的领军框架,从2.5版本后也开始支持注解.相比起之前使用xml来配 ...

  6. Spring学习第6篇: 基于注解使用IOC

    大家家好,我是一名网络怪咖,北漂五年.相信大家和我一样,都有一个大厂梦,作为一名资深Java选手,深知Spring重要性,现在普遍都使用SpringBoot来开发,面试的时候SpringBoot原理也 ...

  7. Spring学习day02-通过全注解模式实现CRUD

    前言 1.为什么要学习纯注解开发? 2.纯注解开发的优势? 3.使用纯注解开发达到的目标 4.实现纯注解开发的步骤 一.纯注解开发 1.为什么要学习纯注解开发? 因为后续将要学习的SpringBoot ...

  8. springmvc学习笔记(10)-springmvc注解开发之商品改动功能

    springmvc学习笔记(10)-springmvc注解开发之商品改动功能 springmvc学习笔记(10)-springmvc注解开发之商品改动功能 标签: springmvc springmv ...

  9. Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】

    本文借鉴:Spring学习(特此感谢!) 通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XM ...

最新文章

  1. xgboost api
  2. ASP.NET防伪令牌与JSON有效载荷
  3. Python--简单的端口扫描脚本
  4. 计算机网络OSI模型、TCP/IP模型与5G协议
  5. 在相册查看保存的图片
  6. 【无广告】一位算法工程师从30+场秋招面试中总结出的超强面经——目标检测篇...
  7. java面试题整理(二)
  8. EXCEL图表技巧:选择合适图表最全指南,建议收藏
  9. 萝卜小姐的整车第一弹—MCU 软件烧录及升级说明
  10. 斐讯k2虚拟服务器设置,斐讯K2调配设置
  11. SpringBoot -> 自动装配初探,debug=ture判断配置类是否生效
  12. MBA面试系列之----中文面试宝典(一)
  13. 选择使用Linux的理由
  14. 动手写一个HTML5的无限循环滚动焦点图
  15. 使用MindStudio进行城市道路交通预测
  16. 阿里云国际站有什么优势吗?
  17. Java实现21点多人游戏(期末作业)
  18. Eclipse Java EE+Tomcat问题和Apache整合Tomcat
  19. 《写给大家看的设计书》《写给大家看的色彩书》《点石成金》《形式感》学习笔记
  20. 从零开始用Unity开发《坦克世界》1.创建项目,导入素材

热门文章

  1. javascript通用验证
  2. 昨天添加的clustrMaps,忘了截屏,今天补上,就作为我在园子里的奠基。
  3. Spring AOP两种实现机制是什么?
  4. GAC中的所有的Assembly都会存放在系统目录%winroot%/assembly下面
  5. Powershell-获取DHCP地址租用信息
  6. Javascript验证上传图片大小[前台处理]
  7. bash知识点:文件测试
  8. sql2008 获取表结构说明
  9. ExtJs UI框架学习六
  10. 有感于《你赔了我赚了》