Spring中Bean的生命周期
在spring中,從BeanFactory或ApplicationContext取得的實(shí)例為Singleton,也就是預(yù)設(shè)為每一個(gè)Bean的別名只能維持一個(gè)實(shí)例,而不是每次都產(chǎn)生
一個(gè)新的對(duì)象
使用Singleton模式產(chǎn)生單一實(shí)例,對(duì)單線程的程序說并不會(huì)有什么問題,但對(duì)于多線程的程序,就必須注意安全(Thread-safe)的議題,防止多個(gè)線程
同時(shí)存取共享資源所引發(fā)的數(shù)據(jù)不同步問題。
然而在spring中 可以設(shè)定每次從BeanFactory或ApplicationContext指定別名并取得Bean時(shí)都產(chǎn)生一個(gè)新的實(shí)例:例如:
<bean singleton="false">
在spring中,singleton屬性默認(rèn)是true,只有設(shè)定為false,則每次指定別名取得的Bean時(shí)都會(huì)產(chǎn)生一個(gè)新的實(shí)例
一個(gè)Bean從創(chuàng)建到銷毀,如果是用BeanFactory來生成,管理Bean的話,會(huì)經(jīng)歷幾個(gè)執(zhí)行階段:
1:Bean的建立:
有BeanFactory讀取Bean定義文件,并生成各個(gè)Bean實(shí)例
2:屬性注入:
執(zhí)行相關(guān)的Bean屬性依賴注入
3:BeanNameAware的setBeanName():
如果Bean類有實(shí)現(xiàn)org.springframework.beans.BeanNameAware接口,則執(zhí)行它的setBeanName()方法
4:BeanFactoryAware的setBeanFactory():
如果Bean類有實(shí)現(xiàn)org.springframework.beans.factory.BeanFactoryAware接口,則執(zhí)行它的setBeanFactory()方法
5:BeanPostProcessors的ProcessBeforeInitialization()
如果任何的org.springframework.beans.factory.config.BeanPostProcessors實(shí)例與Bean實(shí)例相關(guān)。則執(zhí)行BeanPostProcessors實(shí)例
的processBeforeInitialization()方法
6:initializingBean的afterPropertiesSet():
如果Bean類已實(shí)現(xiàn)org.springframework.beans.factory.InitializingBean接口,則執(zhí)行他的afterProPertiesSet()方法
7:Bean定義文件中定義init-method:
可以在Bean定義文件中使用"init-method"屬性設(shè)定方法名稱例如:
<bean calss="onlyfun.caterpillar.HelloBean" init-method="initBean">
如果有以上設(shè)置的話,則執(zhí)行到這個(gè)階段,就會(huì)執(zhí)行initBean()方法
8:BeanPostProcessors的ProcessaAfterInitialization()
如果有任何的BeanPostProcessors實(shí)例與Bean實(shí)例關(guān)聯(lián),則執(zhí)行BeanPostProcessors實(shí)例的ProcessaAfterInitialization()方法
9:DisposableBean的destroy()
在容器關(guān)閉時(shí),如果Bean類有實(shí)現(xiàn)org.springframework.beans.factory.DisposableBean接口,則執(zhí)行他的destroy()方法
10:Bean定義文件中定義destroy-method
在容器關(guān)閉時(shí),可以在Bean定義文件中使用"destroy-method"屬性設(shè)定方法名稱,例如:
<bean destroy-method="destroyBean">
如果有以上設(shè)定的話,則進(jìn)行至這個(gè)階段時(shí),就會(huì)執(zhí)行destroyBean()方法,如果是使用ApplicationContext來生成并管理Bean的話
則稍有不同,使用ApplicationContext來生成及管理Bean實(shí)例的話,在執(zhí)行BeanFactoryAware的setBeanFactory()階段后,若Bean
類上有實(shí)現(xiàn)org.springframework.context.ApplicationContextAware接口,則執(zhí)行其setApplicationContext()方法,接著才執(zhí)行
BeanPostProcessors的ProcessBeforeInitialization()及之后的流程
在spring中,從BeanFactory或ApplicationContext取得的實(shí)例為Singleton,也就是預(yù)設(shè)為每一個(gè)Bean的別名只能維持一個(gè)實(shí)例,而不是每次都產(chǎn)生
一個(gè)新的對(duì)象
使用Singleton模式產(chǎn)生單一實(shí)例,對(duì)單線程的程序說并不會(huì)有什么問題,但對(duì)于多線程的程序,就必須注意安全(Thread-safe)的議題,防止多個(gè)線程
同時(shí)存取共享資源所引發(fā)的數(shù)據(jù)不同步問題。
然而在spring中 可以設(shè)定每次從BeanFactory或ApplicationContext指定別名并取得Bean時(shí)都產(chǎn)生一個(gè)新的實(shí)例:例如:
<bean singleton="false">
在spring中,singleton屬性默認(rèn)是true,只有設(shè)定為false,則每次指定別名取得的Bean時(shí)都會(huì)產(chǎn)生一個(gè)新的實(shí)例
一個(gè)Bean從創(chuàng)建到銷毀,如果是用BeanFactory來生成,管理Bean的話,會(huì)經(jīng)歷幾個(gè)執(zhí)行階段:
1:Bean的建立:
有BeanFactory讀取Bean定義文件,并生成各個(gè)Bean實(shí)例
2:屬性注入:
執(zhí)行相關(guān)的Bean屬性依賴注入
3:BeanNameAware的setBeanName():
如果Bean類有實(shí)現(xiàn)org.springframework.beans.BeanNameAware接口,則執(zhí)行它的setBeanName()方法
4:BeanFactoryAware的setBeanFactory():
如果Bean類有實(shí)現(xiàn)org.springframework.beans.factory.BeanFactoryAware接口,則執(zhí)行它的setBeanFactory()方法
5:BeanPostProcessors的ProcessBeforeInitialization()
如果任何的org.springframework.beans.factory.config.BeanPostProcessors實(shí)例與Bean實(shí)例相關(guān)。則執(zhí)行BeanPostProcessors實(shí)例
的processBeforeInitialization()方法
6:initializingBean的afterPropertiesSet():
如果Bean類已實(shí)現(xiàn)org.springframework.beans.factory.InitializingBean接口,則執(zhí)行他的afterProPertiesSet()方法
7:Bean定義文件中定義init-method:
可以在Bean定義文件中使用"init-method"屬性設(shè)定方法名稱例如:
<bean calss="onlyfun.caterpillar.HelloBean" init-method="initBean">
如果有以上設(shè)置的話,則執(zhí)行到這個(gè)階段,就會(huì)執(zhí)行initBean()方法
8:BeanPostProcessors的ProcessaAfterInitialization()
如果有任何的BeanPostProcessors實(shí)例與Bean實(shí)例關(guān)聯(lián),則執(zhí)行BeanPostProcessors實(shí)例的ProcessaAfterInitialization()方法
9:DisposableBean的destroy()
在容器關(guān)閉時(shí),如果Bean類有實(shí)現(xiàn)org.springframework.beans.factory.DisposableBean接口,則執(zhí)行他的destroy()方法
10:Bean定義文件中定義destroy-method
在容器關(guān)閉時(shí),可以在Bean定義文件中使用"destroy-method"屬性設(shè)定方法名稱,例如:
<bean destroy-method="destroyBean">
如果有以上設(shè)定的話,則進(jìn)行至這個(gè)階段時(shí),就會(huì)執(zhí)行destroyBean()方法,如果是使用ApplicationContext來生成并管理Bean的話
則稍有不同,使用ApplicationContext來生成及管理Bean實(shí)例的話,在執(zhí)行BeanFactoryAware的setBeanFactory()階段后,若Bean
類上有實(shí)現(xiàn)org.springframework.context.ApplicationContextAware接口,則執(zhí)行其setApplicationContext()方法,接著才執(zhí)行
BeanPostProcessors的ProcessBeforeInitialization()及之后的流程