Spring原始碼分析 beanFactory

2021-08-06 03:49:58 字數 4679 閱讀 8572

aliasregistry:定義了對別名的簡單增刪改操作

public inte***ce aliasregistry
******aliasregistry:使用concurrenthashmap實現了aliasregistry介面:

public

class

******aliasregistry

implements

aliasregistry

...}

singletonbeanregistry :定義對單例的註冊及獲取

public

inte***ce singletonbeanregistry

defaultsingletonbeanregistry:對singletonbeanregistry 介面的實現,同時繼承了******aliasregistry 類。

public

class

defaultsingletonbeanregistry

extends

******aliasregistry

implements

singletonbeanregistry else }}

//往singletonobjects裡面增加單例

protected

void

addsingleton(string beanname, object singletonobject)

}//增加物件工廠(工廠bean)

protected

void

addsingletonfactory(string beanname, objectfactory<?> singletonfactory) }}

//獲取單例,

public object getsingleton(string beanname)

//先從儲存bean對map中獲取,沒有獲取到,則從快取objectfactory獲取的bean的map中獲取,沒有獲取到,則從工廠bean獲取

protected object getsingleton(string beanname, boolean allowearlyreference) }}

}return singletonobject != null_object?singletonobject:null;

}}

總結bean的增加和獲取:

把bean增加到singletonobjects中,從singletonfactories中移除,從earlysingletonobjects中移除,再儲存到registeredsingletons中。

beanfactory:bean的獲取和bean的各種屬性

public

inte***ce

beanfactory

hierarchicalbeanfactory :在beanfactory 的基礎上增加工廠層次關係,

public

inte***ce

hierarchicalbeanfactory

extends

beanfactory

beandefinitionregistry :對beandefinition的增刪改操作

public

inte***ce

beandefinitionregistry

extends

aliasregistry

factorybeanregistrysupport :增加對factorybean的操作

public

abstract

class

factorybeanregistrysupport

extends

defaultsingletonbeanregistry

configurablebeanfactory :提供配置factory的各種方法

public

inte***ce

configurablebeanfactory

extends

hierarchicalbeanfactory, singletonbeanregistry

listablebeanfactory :根據各種條件獲取bean

public

inte***ce

listablebeanfactory

extends

beanfactory

abstractbeanfactory :繼承了factorybeanregistrysupport ,同時實現configurablebeanfactory 介面的方法

public

abstract

class

abstractbeanfactory

extends

factorybeanregistrysupport

implements

configurablebeanfactory

public object getbean(string name) throws bean***ception

public

t getbean(string name, classrequiredtype) throws bean***ception

//一系列最核心的getbean方法。非常重要!

public object getbean(string name, object... args) throws bean***ception

public

t getbean(string name, classrequiredtype, object... args) throws bean***ception

//獲取bean

protected

t dogetbean(string name, classrequiredtype, final object args, boolean typecheckonly) throws bean***ception else

}bean = this.getobjectforbeaninstance(sharedinstance, name, beanname, (rootbeandefinition)null);//bean的例項化,如果是工廠bean,則獲取工廠bean的裡面的bean

} else

beanfactory ex = this.getparentbeanfactory();

if(ex != null && !this.containsbeandefinition(beanname))

return ex.getbean(var24, requiredtype);

}if(!typecheckonly)

try

this.registerdependentbean(dependsonbean, beanname);

this.getbean(dependsonbean);}}

if(ex1.issingleton()) catch (bean***ception var2)

}});

bean = this.getobjectforbeaninstance(sharedinstance, name, beanname, ex1);

} else

if(ex1.isprototype()) finally

bean = this.getobjectforbeaninstance(var25, name, beanname, ex1);

} else

try finally

return var1;

}});

bean = this.getobjectforbeaninstance(var28, name, beanname, ex1);

} catch (illegalstateexception var21)

}} catch (bean***ception var23)

}}

autowirecapablebeanfactory:提供建立bean,自動注入,初始化以及應用bean的後處理器

public

inte***ce

autowirecapablebeanfactory

extends

beanfactory

abstractautowirecapablebeanfactory :繼承abstractbeanfactory ,實現autowirecapablebeanfactory 介面

//建立bean的實現

public

abstract

class

abstractautowirecapablebeanfactory

extends

abstractbeanfactory

implements

autowirecapablebeanfactory

}

defaultlistablebeanfactory:綜合上面所有功能,主要是對bean註冊後的處理

spring原始碼分析 一 bean的註冊

a xml註冊獲取 system.out.println bean b 註解方式註冊的bean system.out.println bean configuration 告訴spring這是乙個配置類 public class mainconfig configuration components...

spring原始碼分析 bean的載入初探

我已經參透了符文,你輸了 瑞茲 網上有許多對bean載入的講解文章,條例非常清晰,看完後會不想再自己寫,就像我看了張愛玲的 不再想寫 因為我覺得我寫人物的心理永遠也不會寫的比她真實,深刻。就像高曉松說,他看了 百年孤獨 再也不會想寫 一樣。可是別人寫的再好,畢竟是別人的,我還是要寫出來。本文抄襲了部...

spring原始碼分析 spring原始碼分析

1.spring 執行原理 spring 啟動時讀取應用程式提供的 bean 配置資訊,並在 spring 容器中生成乙份相應的 bean 配置登錄檔,然後根據這張登錄檔例項化 bean,裝配好 bean 之間的依賴關係,為上 層應用提供準備就緒的執行環境。二 spring 原始碼分析 1.1spr...