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

例子:

  1. package com.mypackage;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5.  
  6. public class MovieRecommender {
  7.  
  8. @Autowired
  9. @Qualifier("main")
  10. private MovieCatalog movieCatalog;
  11.  
  12. }
  1. package com.mypackage;
  2.  
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4.  
  5. public class MovieRecommender {
  6.  
  7. private MovieCatalog movieCatalog;
  8.  
  9. public void prepare(@Qualifier("main")MovieCatalog movieCatalog){
  10. this.movieCatalog=movieCatalog;
  11. }
  12.  
  13. }

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

  • XML文件中使用qualifier:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  9.  
  10. <context:component-scan base-package="com.multibean">
  11. </context:component-scan>
  12.  
  13. <bean class="com.mypackage.MovieCatalog">
  14. <qualifier value="main"></qualifier>
  15. </bean>
  16.  
  17. <bean class="com.mypackage.MovieCatalog">
  18. <qualifier value="action"></qualifier>
  19. </bean>
  20. </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接口

  1. package com.multibean;
  2.  
  3. public interface BeanInterface {
  4.  
  5. }

在定义两个实现类

  1. package com.multibean;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. @Component
  6. public class BeanInterfaceImpl implements BeanInterface {
  7.  
  8. }

  

  1. package com.multibean;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. @Component
  6. public class BeanInterface2Impl implements BeanInterface {
  7.  
  8. }

定义BeanInvoker实现@Qualifier指定bean

  1. package com.multibean;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.stereotype.Component;
  6.  
  7. @Component
  8. public class BeanInvoker {
  9.  
  10. @Autowired
  11. @Qualifier("beanInterfaceImpl")
  12. private BeanInterface beanInterface;
  13.  
  14. public void say(){
  15.  
  16. if(null != beanInterface){
  17. System.out.println(beanInterface.getClass().getName());
  18. }else{
  19. System.out.println("BeanInterface is null.");
  20. }
  21.  
  22. }
  23.  
  24. }

单元测试:

  1. package com.multibean;
  2.  
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7. public class UnitTest {
  8.  
  9. @Test
  10. public void test(){
  11. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beansnnotation.xml");
  12. BeanInvoker beanInvoker = (BeanInvoker)context.getBean("beanInvoker");
  13. beanInvoker.say();
  14. }
  15. }

结果:

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

修改BeanInvoker

  1. package com.multibean;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.stereotype.Component;
  6.  
  7. @Component
  8. public class BeanInvoker {
  9.  
  10. @Autowired
  11. @Qualifier("beanInterface2Impl")
  12. private BeanInterface beanInterface;
  13.  
  14. public void say(){
  15.  
  16. if(null != beanInterface){
  17. System.out.println(beanInterface.getClass().getName());
  18. }else{
  19. System.out.println("BeanInterface is null.");
  20. }
  21.  
  22. }
  23.  
  24. }

结果:

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

Spring学习(10)--- @Qualifier注解的更多相关文章

  1. [Spring]@Autowired,@Required,@Qualifier注解

    @Required注解 @Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值 假设有个测试类,里面有name和password两个属性 我给两个属性的setter方法 ...

  2. spring学习笔记二 注解及AOP

    本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...

  3. Spring学习笔记--使用注解装配

    使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...

  4. Spring学习之-各注解的含义总结

    注解配置 @ComponentScan("spittr.web"):/在加载Spring上下文时,会扫描spittr.web包查找组件 @ComponentScan注解扫描的组件有 ...

  5. Spring学习之事务注解@Transactional

    今天学习spring中的事务注解,在学习Spring注解事务之前需要明白一些事务的基本概念: 事务:并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通 ...

  6. Spring学习之路-注解

    Spring的注解总结. 地址:https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsin ...

  7. Spring学习之常用注解(转)

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  8. Spring学习笔记5——注解方式AOP

    第一步:注解配置业务类 使用@Component("Pservice")注解ProductService 类 package com.spring.service; import ...

  9. spring学习 十四 注解AOP 通知传递参数

    我们在对切点进行增强时,不建议对切点进行任何修改,因此不加以使用@PointCut注解打在切点上,尽量只在Advice上打注解(Before,After等),如果要在通知中接受切点的参数,可以使用Jo ...

  10. Spring学习--用 ASpectJ 注解实现 AOP

    用 AspectJ 注解声明切面: 要在 Spring 中声明 AspectJ 切面 , 只需要在 IOC 容器中将切面声明为 bean 实例.当在 Spring IOC 容器中初始化 AsjectJ ...

随机推荐

  1. python——网络编程

    Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...

  2. 【内网渗透】MSF的exploit和pyload的基础使用

    1.连接MSF root@kali:~# msfconsole 2.显示所有攻击模块 msf > show exploits |more 3.寻找攻击模块 msf > search ms0 ...

  3. 【珍藏】linux 同步IO: sync、fsync与fdatasync

    传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘I/O都通过缓冲进行.当将数据写入文件时,内核通常先将该数据复制到其中一个缓冲区中,如果该缓冲区尚未写满,则并不将其排入输出队列, ...

  4. google ip地址

    http://203.208.46.146 http://203.208.46.177 http://203.208.46.178 http://209.116.186.251 http://203. ...

  5. 接口加密《二》: API权限设计总结

    来源:http://meiyitianabc.blog.163.com/blog/static/105022127201310562811897/ API权限设计总结: 最近在做API的权限设计这一块 ...

  6. 使用 Python 实现命令行词典(一)

    最近经常在服务器上开发,经常会遇到不认识的单词,然而 linux 下实在没有什么好用的词典,索性自己写一个好了. 词典 API 首先,Google 了一下可用的词典的 API,发现金山的 iciba ...

  7. 搭建本地git仓库

    使用工具:git|码云 步骤: 注册码云账号,创建项目名称等. 本地git配置 本地文件目录:git init(初始化创建分支master) 基础配置:git config --global user ...

  8. jquery.uploadifive 解决上传限制图片或文件大小

    dotNet疯狂之路No.28  今天很残酷,明天更残酷,后天很美好,但是绝大部分人是死在明天晚上,只有那些真正的英雄才能见到后天的太阳.  We're here to put a dent in t ...

  9. Java学习笔记——设计模式之三.装饰模式

    函数应该做一件事,做好这件事,只做这一件事. --Clean Code 装饰模式,上代码: 先定义零件类: package cn.no3.decorator.template; public abst ...

  10. 自己写的书《深入理解Android虚拟机内存管理》,不出版只是写着玩

    百度网盘地址:https://pan.baidu.com/s/1jI4xZgE 我给起的书名叫做<深入理解Android虚拟机内存管理>.本书分为两个部分,前半部分主要是我对Linux0. ...