Spring学习(10)--- @Qualifier注解
2015-07-06 23:44
603 查看
按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以指定单独的构造器参数或方法参数
可用于注解集合类型变量
例子:
PS:应用于构造器的方法比较常用
XML文件中使用qualifier:
如果通过名字进行注解注入,主要使用的不是@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接口
在定义两个实现类
定义BeanInvoker实现@Qualifier指定bean
单元测试:
结果:
修改BeanInvoker
结果:
可用于注解集合类型变量
例子:
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/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://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 { @Test public 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
相关文章推荐
- SpringMVC 入门 -- Handler编写
- java中compareTo和compare方法之比较
- java线程阻塞中断与LockSupport使用介绍(转)
- GIT: 第一步:Myeclipse安装GIT客户端
- java学习11--语句:for循环嵌套、转义字符的使用
- Java IO:面向缓冲区、同步、非阻塞式IO(NIO)
- Java - Thinking in Java 第2章 练习
- Java 基础第一天
- Johnnny_yan_lan的Java学习笔记
- Java語言
- JRE与JDK
- Java中的枚举类型
- Java IO:面向流、同步、阻塞式IO(BIO)
- JDK源码分析之concurrent包(三) -- Future方式的实现
- java基础知识--接口语法细节
- Spring学习(9)--- @Autowired注解(二)
- Java IO:阻塞/非阻塞式IO、同步/异步IO
- JAVA中关于set()和get()方法的理解及使用
- JAVA 向上转型有什么用,List 和ArrayList的区别
- Java面向对象之重载和重写