Spring bean銷毀的過程

2021-09-26 20:48:04 字數 3186 閱讀 1689

一:defaultsingletonbeanregistry之destroysingleton(string beanname)

銷毀單例bean

public

void

destroysingleton

(string beanname)

destroybean

(beanname, disposablebean)

;}

方法簡析:

移除快取的單例bean之removesingleton(beanname)

protected

void

removesingleton

(string beanname)

}

方法簡析:

銷毀bean之destroybean(string beanname, disposablebean bean)

protected

void

destroybean

(string beanname, disposablebean bean)

for(string dependentbeanname : dependencies)

}// actually destroy the bean now...

if(bean != null)

catch

(throwable ex)

}// trigger destruction of contained beans...

setcontainedbeans =

this

.containedbeanmap.

remove

(beanname);if

(containedbeans != null)

}// remove destroyed bean from other beans' dependencies.

synchronized

(this

.dependentbeanmap)}}

// remove destroyed bean's prepared dependency information.

this

.dependenciesforbeanmap.

remove

(beanname)

;}

方法簡析:

二:disposablebeanadapter

上面講到從disposablebeans列表中移除disposablebean物件,現在來看一下spring是如何註冊disposablebean物件的:

public

void

registerdisposablebean

(string beanname, disposablebean bean)

} beanname, object bean, rootbeandefinition mbd)

protected

void

registerdisposablebeanifnecessary

(string beanname, object bean, rootbeandefinition mbd)

else

scope.

registerdestructioncallback

(beanname,

newdisposablebeanadapter

(bean, beanname, mbd,

getbeanpostprocessors()

, acc));

}}}//檢查是否需要銷毀

bean, rootbeandefinition mbd)

protected

boolean

requiresdestruction

(object bean, rootbeandefinition mbd)

方法簡析:

disposablebeanadapter中重要方法簡析

1.構造方法

public

disposablebeanadapter

(object bean, string beanname, rootbeandefinition beandefinition,

list

postprocessors, accesscontrolcontext acc)

}else

else

if(paramtypes.length ==1&&

boolean

.class

!= paramtypes[0]

)}}this

.beanpostprocessors =

filterpostprocessors

(postprocessors, bean)

;}

方法簡析:

2.尋找銷毀方法

private string inferdestroymethodifnecessary

(object bean, rootbeandefinition beandefinition)

catch

(nosuchmethodexception ex)

catch

(nosuchmethodexception ex2)}}

return null;

}return

(stringutils.

haslength

(destroymethodname)

? destroymethodname : null)

;}

方法簡析:

3.bean的銷毀行為之destroy()

public

void

destroy()

}if(this

.invokedisposablebean)

try}

, acc);}

else

}catch

(throwable ex)

else}}

if(this

.destroymethod != null)

elseif(

this

.destroymethodname != null)

}}

方法簡析:

spring bean的初始化與銷毀

spring bean的初始與銷毀 這裡只介紹bean的初始與銷毀時呼叫的方法 不配置就不呼叫 這是要測試的bean public class testbean public void destroy 1.初始化的兩種方式 1 實現initializingbean介面的 public class b...

spring bean載入過程

spring通過資源載入器載入相應的xml檔案,使用讀取器讀取資源載入器中的檔案到讀取器中,在讀取過程中,解析相應的xml檔案元素,轉化為spring定義的資料結beandefinition,把相應的beandefinition註冊到登錄檔中。登錄檔中包含的beandefinition的資料結構,沒...

spring Bean建立過程詳解

強烈建議大家從git上拉取spring原始碼來學習spring原始碼。因為裡面相較於idea生成的會有注釋,裡面有的方法會有注釋看起來會省力一點。以下都是用5.0.2版本來做闡述。寫在前面 建議大家一定要自己用例項跑一遍,做好記錄。如果只是看看會非常抽象。此流程圖作為梗概,便於加強記憶和理解,新手或...