目錄
SpringBean的生命周期
整體介紹
詳細(xì)介紹
1.實(shí)例化Bean
2.設(shè)置屬性值
3.檢查Aware
4.調(diào)用BeanPostProcessor的前置處理方法
5.調(diào)用InitializingBean的afterPropertiesSet方法
6.調(diào)用自定義init-method方法
7.調(diào)用BeanPostProcessor的后置處理方法
8.注冊Destruction回調(diào)
9.Bean準(zhǔn)備就緒
10.調(diào)用DisposableBean的destroy方法
11.調(diào)用自定義的destory-method
核心部分源碼
SpringBean的生命周期
整體介紹
一個(gè)SpringBean從出生到銷毀的全過程就是它的整個(gè)生命周期,需要經(jīng)歷以下幾個(gè)階段:
整個(gè)生命周期可以大致分為三個(gè)大階段,分別是:創(chuàng)建、使用、銷毀。還可以進(jìn)一步分為5個(gè)小的階段:實(shí)例化、初始化(前置處理、初始化、后置處理)、注冊Destruction回調(diào)、正常使用、銷毀。
詳細(xì)介紹
SpringBean的生命周期詳情
1.實(shí)例化Bean
Spring容器首先創(chuàng)建Bean實(shí)例
在AbstractAutowireCapableBeanFactory類中的createBeanInstance方法中實(shí)現(xiàn)
2.設(shè)置屬性值
Spring容器注入必要的屬性到Bean中。
在AbstractAutowireCapableBeanFactory類中的populateBean方法中處理
3.檢查Aware
如果Bean實(shí)現(xiàn)了BeanNameAware、BeanClassLoaderAware等這些Aware接口,Spring容器會調(diào)用它們。
?在AbstractAutowireCapableBeanFactory類中的initializeBean方法中調(diào)用
4.調(diào)用BeanPostProcessor的前置處理方法
在Bean初始化之前,允許自定義的BeanPostProcessor對Bean實(shí)例進(jìn)行處理,如修改Bea的狀態(tài)
BeanPostProcessor的postProcessBeforeInitialization方法會在此時(shí)被調(diào)用。
由AbstractAutowireCapableBeanFactory的applyBeanPostProcessorsBeforeInitialization方法執(zhí)行
5.調(diào)用InitializingBean的afterPropertiesSet方法
提供一個(gè)機(jī)會,在所有Bean屬性設(shè)置完成后進(jìn)行初始化操作。如果Bean實(shí)現(xiàn)了InitializingBean接口afterPropertiesSet方法會被調(diào)用。
在AbstractAutowireCapableBeanFactory的invokelnitMethods方法中調(diào)用
6.調(diào)用自定義init-method方法
提供一種配置方式,在XML配置中指定Bean的初始化方法。如果Bean在配置文件中定義了初始化方法那么該方法會被調(diào)用。
在AbstractAutowireCapableBeanFactory的invokelnitMethods方法中調(diào)用
7.調(diào)用BeanPostProcessor的后置處理方法
在Bean初始化之后,再次允許BeanPostProcessor對Bean進(jìn)行處理。BeanPostProcessor的postProcessAfterlnitialization方法會在此時(shí)被調(diào)用。
由AbstractAutowireCapableBeanFactory的applyBeanPostProcessorsAfterlnitialization方法執(zhí)行
8.注冊Destruction回調(diào)
如果Bean實(shí)現(xiàn)了DisposableBean接口或在Bean定義中指定了自定義的銷毀方法,Spring容器會為這些Bean注冊一個(gè)銷毀回調(diào),確保在容器關(guān)閉時(shí)能夠正確地清理資源。
在AbstractAutowireCapableBeanFactory類中的registerDisposableBeanlfNecessary方法中實(shí)現(xiàn)
9.Bean準(zhǔn)備就緒
此時(shí),Bean已完全初始化,可以開始處理應(yīng)用程序的請求了
10.調(diào)用DisposableBean的destroy方法
當(dāng)容器關(guān)閉時(shí),如果Bean實(shí)現(xiàn)了DisposableBean接口,destroy方法會被調(diào)用。
在DisposableBeanAdapter的destroy方法中實(shí)現(xiàn)
11.調(diào)用自定義的destory-method
如果Bean在配置文件中定義了銷毀方法,那么該方法會被調(diào)用
在DisposableBeanAdapter的destroy方法中實(shí)現(xiàn)
核心部分源碼
可以看到,整Bean的創(chuàng)建的過程都依賴于AbstractAutowireCapableBeanFactory這個(gè)類,而銷毀主要依賴DisposableBeanAdapter這個(gè)類
AbstractAutowireCapableBeanFactory 的入口處,doCreateBean的核心代碼如下,其中包含了實(shí)例化、設(shè)置屬性值、初始化Bean以及注冊銷毀回調(diào)的幾個(gè)核心方法。文章來源:http://www.zghlxwxcb.cn/news/detail-770083.html
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
//實(shí)例化 Bean。
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
Object bean = instanceWrapper.getWrappedInstance();
Class<?> beanType = instanceWrapper.getWrappedClass();
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
}
//..............................................
//初始化 Bean 實(shí)例。
Object exposedObject = bean;
try {
//設(shè)置屬性值
populateBean(beanName, mbd, instanceWrapper);
//初始化Bean
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
//......................................
//注冊Bean的銷毀回調(diào)。
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
return exposedObject;
}
??而DisposableBeanAdapter的destory方法的核心內(nèi)容如下文章來源地址http://www.zghlxwxcb.cn/news/detail-770083.html
@Override
public void destroy() {
//...........................
if (this.invokeDisposableBean) {
//........................
try {
((DisposableBean) this.bean).destroy();
}
//.............................
//判斷是否自定義了銷毀方法
else if (this.destroyMethods != null) {
//遍歷每個(gè)銷毀方法并調(diào)用
for (Method destroyMethod : this.destroyMethods) {
invokeCustomDestroyMethod(destroyMethod);
}
}
else if (this.destroyMethodNames != null) {
//遍歷每個(gè)銷毀方法名稱并通過方法名稱調(diào)用
for (String destroyMethodName : this.destroyMethodNames) {
Method destroyMethod = determineDestroyMethod(destroyMethodName);
if (destroyMethod != null) {
invokeCustomDestroyMethod(
ClassUtils.getInterfaceMethodIfPossible(destroyMethod, this.bean.getClass()));
}
}
}
}
到了這里,關(guān)于Spring高手之路-SpringBean的生命周期的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!