詳解Spring中Lookup注解的使用
我們知道在spring容器中單獨的一個抽象類是不能成為一個bean的,那么有沒有辦法呢?這個時候我們可以使用Lookup注解,我們可以看下spring的掃描bean部分邏輯。我們知道在spring中要想成為一個bean,必須先生成BeanDefinition對象,如果一個抽象類中沒有含有Lookup注解的方法,在spring掃描時就會被排除掉。
/**
* 1、判斷是不是獨立的類,非靜態(tài)內部類則無法生成bean,
* 2、判斷是不是接口或者抽象類(有一種特殊情況),是則無法生成
* 3、判斷如果是抽象類,但是里面有某個方法上面加油@lookup注解,則也可以生成bean
* Determine whether the given bean definition qualifies as candidate.
* <p>The default implementation checks whether the class is not an interface
* and not dependent on an enclosing class.
* <p>Can be overridden in subclasses.
* @param beanDefinition the bean definition to check
* @return whether the bean definition qualifies as a candidate component
*/
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
AnnotationMetadata metadata = beanDefinition.getMetadata();
return (metadata.isIndependent() && (metadata.isConcrete() ||
(metadata.isAbstract() && metadata.hasAnnotatedMethods(Lookup.class.getName()))));
}
下面我們就驗證下,不使用Lookup注解的情況
@Component
public abstract class C1 {
}
運行結果報錯

在抽象類中隨便寫個方法,然后方法上面增加Lookup注解
@Component
public abstract class C1 {
@Lookup
public void a(){}
}
運行結果,正常輸出,通過cglib代理生成新的類

但是一般很少這樣用,另外一種場景可能會用到。在某個單例bean中使用另外一個bean對象,但是每次又想返回的對象不同。但是spring在容器中注入bean的時候,scope默認的是單例模式,也就是說在整個應用中只能創(chuàng)建一個實例。當scope為PROTOTYPE類型的時候,在每次注入的時候會自動創(chuàng)建一個新的bean實例。但是當一個單例模式的bean去引用PROTOTYPE類型的bean的時候,PROTOTYPE類型的bean也會變成單例。
@Component
public class D3 {
@Autowired
private E4 e4;
public void a(){
System.out.println(this.e4);
}
}
@Component
public class E4 {
}
輸出結果,可以看到每次打印出來的對象是同一個

使用Lookup注解
@Component
public class D3 {
public void a(){
System.out.println(this.a1());
}
@Lookup
public E4 a1(){
return null;
}
}
運行輸出結果,每次輸出的結果已經(jīng)不相同了,已經(jīng)達到了我們的需求

這是什么原因導致的呢?還有就是我們a1方法返回的是空,但是輸出的結果為啥也有值呢?
因為spring在遇到這種標有Lookup注解的方法時,會重寫該方法,然后返回結果,所以我們自己定義的方法不管有沒有返回值已經(jīng)沒關系了。
到此這篇關于詳解Spring中Lookup注解的使用的文章就介紹到這了,更多相關Spring Lookup注解的使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
netty?pipeline中的inbound和outbound事件傳播分析
這篇文章主要為大家介紹了netty?pipeline中的inbound和outbound事件傳播分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
解決springboot 獲取form-data里的file文件的問題
這篇文章主要介紹了解決springboot 獲取form-data里的file文件的問題的相關資料,這里提供了詳細的解決步驟,需要的朋友可以參考下2017-07-07

