- 按类型自动装配可能多个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