- 按類型自動裝配可能多個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/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
