當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring学习(10)--- @Qualifier注解
生活随笔
收集整理的這篇文章主要介紹了
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:
- 如果通過名字進行注解注入,主要使用的不是@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?
轉載于:https://www.cnblogs.com/JsonShare/p/4625753.html
總結
以上是生活随笔為你收集整理的Spring学习(10)--- @Qualifier注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用java开发简单的mis系统所需的技
- 下一篇: javascript高级程序设计 学习