spring原始碼分析 一 bean的註冊

2021-08-29 20:52:49 字數 4005 閱讀 5309

a、xml註冊獲取

system.out.println(bean);

b、註解方式註冊的bean

system.out.println(bean);

@configuration  //告訴spring這是乙個配置類

public class mainconfig

}

@configuration 

@componentscans(value = )

public class mainconfig

componentscans指定掃瞄的包,然後獲取出來的就是這個包下,所有標註有@component、@controller、@service、@repository等註解的類。

@controller、@service、@repository都是@component的派生類

for (string name : definitionnames)

@configuration  

@componentscans(

value = ),

@filter(type=filtertype.assignable_type,classes=),*/

@filter(type=filtertype.custom,classes=)

},usedefaultfilters = false) }

)//@componentscan value:指定要掃瞄的包

//excludefilters = filter 指定掃瞄的時候,按照什麼規則,排除哪些元件 排除有controller註解的元件

//includefilters = filter 指定掃瞄的時候,按照什麼規則,只需要哪些元件

按照註解方式掃瞄,上面是只掃瞄標註controller註解的元件

按照給定型別 只掃瞄bookservice或其子類或實現類

使用aspectj指定

使用正則指定

自定義

//usedefaultfilters = false 禁用預設的規則,只有設定為false,只包含才生效

public class mainconfig

自定義過濾規則,必須實現typefilter介面

public class mytypefilter implements typefilter 

return false;

}}

//預設是單例項的

/*** @scope:調整作用域

* prototype:多例項的:ioc容器啟動並不會去呼叫方法建立物件放在容器中。

* 每次獲取的時候才會呼叫方法建立物件;

* singleton:單例項的(預設值):ioc容器啟動會呼叫方法建立物件放到ioc容器中。

* 以後每次獲取就是直接從容器(map.get())中拿,

* request:同一次請求建立乙個例項

* session:同乙個session建立乙個例項

*/@scope("prototype")

@bean("person")

public person person()

懶載入:

單例項bean:預設在容器啟動的時候建立物件;懶載入:容器啟動不建立物件。第一次使用(獲取)bean建立物件,並初始化;

按照一定的條件進行判斷,滿足條件給容器中註冊bean。

首先要自己寫乙個類,繼承condition,並實現match方法

//判斷是否linux系統

public class linuxcondition implements condition

return false;

}}

然後在註冊bean的時候,在方法上加上@conditional註解,並制定linuxcondition的類。

如果標在類上面,則滿足條件的時候,這個配置類裡面的bean才會生效。

//類中元件統一設定。滿足當前條件,這個類中配置的所有bean註冊才能生效;

@conditional()

@configuration

public class mainconfig2 ) : 按照一定的條件進行判斷,滿足條件給容器中註冊bean

* * 如果系統是windows,給容器中註冊("bill")

* 如果是linux系統,給容器中註冊("linus")

*/@conditional()

@bean("bill")

public person person01()

}

給容器中註冊元件;

1)、包掃瞄+元件標註註解(@controller/@service/@repository/@component)[自己寫的類]

2)、@bean[匯入的第三方包裡面的元件]

3)、@import[快速給容器中匯入乙個元件]

1)、@import(要匯入到容器中的元件);容器中就會自動註冊這個元件,id預設是全類名

2)、importselector:返回需要匯入的元件的全類名陣列; 

3)、importbeandefinitionregistrar:手動註冊bean到容器中

4)、使用spring提供的 factorybean(工廠bean);

1)、預設獲取到的是工廠bean呼叫getobject建立的物件

2)、要獲取工廠bean本身,我們需要給id前面加乙個&

&colo***ctorybean

@configuration

@import()

//@import匯入元件,id預設是元件的全類名

public class mainconfig2

importselector可以自己定義匯入哪些類,返回乙個陣列,返回要匯入的類的全類名,需要實現importselector介面,
//自定義邏輯返回需要匯入的元件

public class myimportselector implements importselector ;

}}

然後在配置類上,需要匯入自己寫的匯入選擇器

@import()
需要自己寫乙個註冊的規則類,實現importbeandefinitionregistrar介面,並在配置類上匯入這個類

public class myimportbeandefinitionregistrar implements importbeandefinitionregistrar 

}}

//建立乙個spring定義的factorybean

public class colo***ctorybean implements factorybean

@override

public class<?> getobjecttype()

//是單例?

//true:這個bean是單例項,在容器中儲存乙份

//false:多例項,每次獲取都會建立乙個新的bean;

@override

public boolean issingleton()

}

在配置類進行註冊

/* 使用spring提供的 factorybean(工廠bean);

* 預設獲取到的是工廠bean呼叫getobject建立的物件

*/@bean

public colo***ctorybean colo***ctorybean()

獲取已經註冊的bean

要獲取工廠bean本身,我們需要給id前面加乙個&       &colo***ctorybean

spring原始碼分析 堆記憶體中建立bean物件

bean的生命週期中首先是在記憶體中開闢儲存空間,然後反射建立物件,建立物件後,依賴注入,設定bean的屬性,初始化bean,最後 放入快取中,返回bean物件,這裡我們分析一下第一步,bean的建立 準備工作 建立乙個student類,新增註解component component student...

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

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

Spring 原始碼分析

public throws bean ception 到這裡最終的方法就是refresh super只是做一些初始化工作,可以忽略。以下是初始化發放做的事情 debug main adding systemproperties propertysource with lowest search pr...