——?dú)g迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術(shù),交流工作經(jīng)驗(yàn),分享JAVA帶來的快樂!本網(wǎng)站部分轉(zhuǎn)載文章,如果有版權(quán)問題請(qǐng)與我聯(lián)系。
對(duì)于singleton bean,Spring容器知道bean何時(shí)實(shí)例化結(jié)束,何時(shí)銷毀,Spring可以管理實(shí)例化結(jié)束之后,和銷毀之前的行為,管理bean的生命周期行為主要未如下兩個(gè)時(shí)機(jī):
Bean全部依賴注入之后 Bean即將銷毀之前
(1)依賴關(guān)系注入后的行為實(shí)現(xiàn): 有兩種方法:A.編寫init方法 B.實(shí)現(xiàn)InitializingBean接口
afterPropertiesSet和init同時(shí)出現(xiàn),前者先于后者執(zhí)行,使用init方法,需要對(duì)配置文件加入init-method屬性
(2)bean銷毀之前的行為
有兩種方法:A.編寫close方法 B.實(shí)現(xiàn)DisposableBean接口
destroy和close同時(shí)出現(xiàn),前者先于后者執(zhí)行,使用close方法,需要對(duì)配置文件加入destroy-method屬性
如果需要在bean創(chuàng)建之時(shí)和創(chuàng)建之后進(jìn)行監(jiān)控,則需要實(shí)現(xiàn)BeanPostProcessor接口
其中有兩個(gè)方法:postProcessBeforeInitialization和postProcessAfterInitialization
這兩個(gè)方法和init方法的順序是:postProcessBeforeInitialization-->init-->postProcessAfterInitialization
Powered by: BlogJava Copyright © rogerfan