Spring学习(10)--- @Qualifier注解
- 按类型自动装配可能多个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;@Componentpublic class BeanInterfaceImpl implements BeanInterface {}
package com.multibean;import org.springframework.stereotype.Component;@Componentpublic 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;@Componentpublic 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 prepareRefreshINFO: 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 loadBeanDefinitionsINFO: 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;@Componentpublic 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 prepareRefreshINFO: 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 loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]com.multibean.BeanInterface2Impl
作者:Json_wangqiang
来源链接:https://www.cnblogs.com/JsonShare/p/4625753.html
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。