Spring之@Lookup注解詳細解析
1、簡介
假設一個單例的Bean A需要引用一個非單例模式的Bean B,那么在每次引用B的時候都想拿到一個新的B,該怎么做?
要知道,Bean A是單例模式的,只會被創(chuàng)建一次,注入一次屬性,也就是說,即使B是property模式,那也是只會一個相同的B,因為A只會被注入一次。
2、解決辦法
- 讓bean A通過實現(xiàn)ApplicationContextAware來感知applicationContext(即可以獲得容器上下文),從而能在運行時通過ApplicationContext.getBean(String beanName)的方法來獲取最新的bean B
- 使用Spring的Lookup注解
3、案例
原型B
@Component @Scope(value = "prototype") public class LookupTestPrototype { }
單例A
@Component public class LookupTestSingleton { @Autowired LookupTestPrototype lookupTestPrototype; public void getObjectId() { System.out.println("Singleton Object Id:" + this + ", Prototype Object Id : " + lookupTestPrototype); } }
Spock測試代碼:
@ContextConfiguration( classes = [LookupTestSingleton.class, LookupTestPrototype.class] ) class Spock4SpringTest extends Specification { @Resource LookupTestSingleton singleton; def "測試單例中使用多例的注解" () { setup: for (int i = 0; i < 10; i++) { println "第"+i+"次調用的結果:" singleton.getObjectId(); } } }
Spock測試結果:
第0次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
第1次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
第2次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
第3次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
第4次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
第5次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
第6次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
第7次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
第8次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
第9次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton@b8e246c, Prototype Object Id : com.talframework.spring.LookupTestPrototype@1f387978
使用@Lookup改動的單例A代碼如下:
@Component public abstract class LookupTestSingleton { @Lookup public abstract LookupTestPrototype lookupTestPrototype (); public void getObjectId() { System.out.println("Singleton Object Id:" + this + ", Prototype Object Id : " + lookupTestPrototype()); } }
調用的結果如下:
第0次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton$$EnhancerBySpringCGLIB$$15adeab8@3be4fcc0, Prototype Object Id : com.talframework.spring.LookupTestPrototype@661c46bc
第1次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton$$EnhancerBySpringCGLIB$$15adeab8@3be4fcc0, Prototype Object Id : com.talframework.spring.LookupTestPrototype@37864b77
第2次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton$$EnhancerBySpringCGLIB$$15adeab8@3be4fcc0, Prototype Object Id : com.talframework.spring.LookupTestPrototype@2b98b3bb
第3次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton$$EnhancerBySpringCGLIB$$15adeab8@3be4fcc0, Prototype Object Id : com.talframework.spring.LookupTestPrototype@540b0448
第4次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton$$EnhancerBySpringCGLIB$$15adeab8@3be4fcc0, Prototype Object Id : com.talframework.spring.LookupTestPrototype@50a691d3
第5次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton$$EnhancerBySpringCGLIB$$15adeab8@3be4fcc0, Prototype Object Id : com.talframework.spring.LookupTestPrototype@557eb543
第6次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton$$EnhancerBySpringCGLIB$$15adeab8@3be4fcc0, Prototype Object Id : com.talframework.spring.LookupTestPrototype@3b95d13c
第7次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton$$EnhancerBySpringCGLIB$$15adeab8@3be4fcc0, Prototype Object Id : com.talframework.spring.LookupTestPrototype@3730ab42
第8次調用的結果:
Singleton Object Id:com.talframework.spring.LookupTestSingleton$$EnhancerBySpringCGLIB$$15adeab8@3be4fcc0, Prototype Object Id : com.talframework.spring.LookupTestPrototype@537c8c7e
第9次調用的結果:
需要注意的是@Lookup只能注解在方法上,這個時候可以注解在一個抽象方法上。
到此這篇關于Spring之@Lookup注解詳細解析的文章就介紹到這了,更多相關@Lookup注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot jar可執(zhí)行原理的徹底分析
這篇文章主要給大家介紹了關于Spring Boot jar可執(zhí)行原理的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-07-07Java 中Timer和TimerTask 定時器和定時任務使用的例子
這篇文章主要介紹了Java 中Timer和TimerTask 定時器和定時任務使用的例子,非常具有實用價值,需要的朋友可以參考下2017-05-05談談為JAXB和response設置編碼,解決wechat4j中文亂碼的問題
中文亂碼是每個程序員都會遇到的問題,本篇文章主要介紹了談談為JAXB和response設置編碼,解決wechat4j中文亂碼的問題,具有一定的參考價值,有興趣的可以了解一下。2016-12-12