Spring框架中Bean的生命週期

2021-09-04 04:14:56 字數 523 閱讀 5984

① spring ioc容器找到關於bean的定義並例項化該bean。 

② spring ioc容器對bean進行依賴注入。

③ 如果bean實現了beannameaware介面,則將該bean的id傳給setbeanname方法。

④ 如果bean實現了beanfactoryaware介面,則將beanfactory物件傳給setbeanfactory方法。

⑤ 如果bean實現了beanpostprocessor介面,則呼叫其postprocessbeforeinitialization方法。

⑥ 如果bean實現了initializingbean介面,則呼叫其afterpropertyset方法。

⑦ 如果有和bean關聯的beanpostprocessors物件,則這些物件的postprocessafterinitialization方法被呼叫。

⑧ 當銷毀bean例項時,如果bean實現了disposablebean介面,則呼叫其destroy方法。

請介紹一下Spring框架中Bean的生命週期

一 bean的定義 spring通常通過配置檔案定義bean。如 xmlns xsi xsi schemalocation spring beans 2.0.xsd helloworld 這個配置檔案就定義了乙個標識為 helloworld 的bean。在乙個配置文件中可以定義多個bean。二 be...

Spring框架 Bean的種類

1 普通bean spring直接建立a例項,並返回。2 factorybean 是乙個特殊的bean,具有工廠生成物件能力,只能生成特定的物件。bean必須使用factorybean介面,此介面提供方法getobject 用於獲得特定bean。先建立fb例項,呼叫getobject 方法,並返回方...

Spring框架中bean的生命週期

1 spring容器從xml檔案中讀取bean的定義,並例項化bean。2 spring根據bean的定義填充所有的屬性。3 如果bean實現了beannameaware介面,spring傳遞bean的id到setbeanname方法。4 如果bean實現了beanfactoryaware介面,sp...