Spring DestorySingleton娴佺▼
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring DestorySingleton娴佺▼相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/RoCE' title='RoCE'>RoCE ons 鍐呭 try 瀹炰緥 鍏紬 before nmap tap
?绗竴鑺備粙缁峉pring鍚姩(閾炬帴)鏃讹紝浠嬬粛AbstractApplicationContext鐨勮繃閿€姣佽繃绋嬶紝涓昏鏄皟鐢ㄤ簡鍐呴儴鐨刣estroyBeans鏂规硶锛岃繖鑺備究鏉ヤ粙缁峛ean鐨勯攢姣佽繃绋嬨€?/p>
?鍓嶉潰浠嬬粛杩囷紝DefaultSingletonBeanRegistry鐢ㄤ竴涓狹ap缂撳瓨鐫€鎵€鏈夌殑鍗曚緥瀹炰緥锛屽浜庡璞$殑閿€姣侊紝鍙绠€鍗曠殑灏嗗叾浠嶮ap涓Щ闄ゅ氨琛屻€備富瑕佸鐞嗙殑鏄緷璧栧叧绯讳笅鐨刡ean閿€姣侀『搴忎互鍙婂洖璋冩帴鍙g殑澶勭悊銆備笂闈㈠厛瀵筪isposableBeans涓墍娑夊強鍒扮殑bean閫愪釜杩涜閿€姣侊紝鐒跺悗鍐嶆竻鐞嗘墍鏈夌殑渚濊禆鍏崇郴鍜岀紦瀛樼殑鍗曚緥瀹炰緥銆?/p>
?涓婁竴鑺傛彁鍒帮紝disposableBeans涓瓨鏀剧殑鏄瓨鍦ㄩ攢姣佹椂闇€瑕佽繘琛屾柟娉曞洖璋冪殑DisposableBeanAdapter瀵硅薄锛宬ey涓哄搴旂殑bean name锛屼細鍦╞ean鍒濆鍖栧悗杩涜鍒ゆ柇骞朵笖瀛樺叆銆傝繖閲屼細浼樺厛澶勭悊杩欎簺bean鐨勯攢姣侊紝涓嬮潰閲嶇偣浠嬬粛涓媎estroySingleton鏂规硶銆?/p>
浜?destroySingleton
?濡備笂鎻忚堪浜嗚鏂规硶鐨勫ぇ鑷磋繃绋嬶紝鍒嗗埆涓?/p>
灏咲efaultSingletonBeanRegistry涓紦瀛樼殑鍚勭鍗曚緥瀹炰緥娓呴櫎鎺?/p>
鎵цdisposableBeans.remove鏂规硶锛岀Щ闄よbeanName锛岃繖閲屼細杩斿洖涓€涓狣isposableBean瀵硅薄
- 鍒犻櫎dependentBeanMap涓璪eanName鐨勫唴瀹癸紝涔熸槸鎵цremove鐨勬柟娉曪紝浼氳繑鍥炰緷璧栦簬beanName鐨勫叾浠朾ean name鍒楄〃锛屽啀渚濇璋冪敤destorySingleton閿€姣佽繖浜沚ean
瀵瑰綋鍓嶇殑DisposableBean鎵цdestory
鍒犻櫎containedBeanMap涓璪eanName鐨勫唴瀹癸紝鎵ц鐨剅emove鏂规硶锛宧i杩斿洖beanName渚濊禆鐨勫叾浠朾ean name鍒楄〃锛屽啀渚濇璋冪敤destorySingleton閿€姣佽繖浜沚ean
浠庤€屽叿浣撶殑鍥炶皟鍙戠敓鍦ㄧ4姝ワ紝寮€澶存彁鍒帮紝bean鍦ㄥ疄渚嬪寲鍚庝細浠isposableBeanAdapter瀛樻斁杩沝isposableBeans涓紝浠庤€岃绫诲疄鐜颁簡鍏蜂綋鐨勫洖璋冭繃绋嬨€?/p>
涓?DisposableBeanAdapter
?DisposableBeanAdapter鐨勭粨鏋勫涓嬶紝鍏跺疄鐜颁簡DisposableBean鎺ュ彛锛屽洜鑰屽彲浠ュ涓婇潰璇寸殑锛屽湪destroySingleton鏂规硶杩涜鍥炶皟銆傚畠鍦ㄦ瀯閫犳柟娉曚腑瀵瑰嚑涓噸瑕佺殑灞炴€ц繘琛屼簡璧嬪€硷紝鐢ㄤ簬瀛樺偍閿€姣佸姩浣滅浉鍏崇殑淇℃伅锛屽寘鎷細
Bean锛氬緟閿€姣乥ean瀵硅薄
beanName锛氬緟閿€姣乥ean鐨刡eanName
destroyMethodName锛氬緟閿€姣佽璋冪敤鐨勬柟娉曪紝璇ユ柟娉曟潵婧愪簬destory-method閰嶇疆椤癸紝濡傛灉涓?inferred)锛屽垯浼氬皢鍥炶皟鏂规硶璧嬪€间负close鎴栬€卻hutdown
destoryMethod锛歞estoryMethodName瀵瑰簲鐨凪ethod瀵硅薄
beanPostProcessors锛氶渶瑕佸鐞嗚bean鐨凞estructionAwareBeanPostProcessor鍥炶皟鍒楄〃銆傚湪瀹炰緥鍖朌isposableBeanAdapter瀵硅薄鏃讹紝浼氳繃婊ょ郴缁熶腑鐨凚eanPostProcessor鍒楄〃锛屾壘鍑哄疄鐜癉estructionAwareBeanPostProcessor鎺ュ彛锛屼笖requiresDestruction鏂规硶杩斿洖true鐨勫疄渚嬶紝鐢ㄤ簬鍚庣画杩涜鍥炶皟銆傦紙InitDestroyAnnotationBeanPostProcessor绫诲疄鐜颁簡璇ユ柟娉曪紝鐢ㄤ互鍥炶皟@PreDestory娉ㄨВ鐨勬柟娉曪紝CommonAnnotationBeanPostProcessor缁ф壙鑷绫伙紝璁剧疆浜哾estroyAnnotationType涓篜reDestroy.class锛?/p>
?褰撴寜鐓х锛堜簩锛夐儴鍒嗙4姝ユ墽琛屾椂锛岃绫讳細鎸夌収濡備笅椤哄簭鎵ц閿€姣佸姩浣滐細
濡傛灉beanPostProcessors鍒楄〃涓嶄负绌猴紝鍒欏洖璋僁estructionAwareBeanPostProcessor鐨刾ostProcessBeforeDestruction鏂规硶锛屽嵆鎵ц@PreDestory娉ㄨВ鏂规硶
濡傛灉瀹炵幇浜咲isposableBean锛屽垯鍥炶皟DisposableBean鐨刣estroy鏂规硶锛屽嵆鎵цDisposableBean鎺ュ彛鏂规硶
濡傛灉閰嶇疆浜哾estroy-method锛屽垯鎵ц閰嶇疆鐨勬柟娉曪紝鍗虫墽琛宒estory-method閰嶇疆鏂规硶
涓汉鍏紬鍙凤細鍟婇┘
以上是关于Spring DestorySingleton娴佺▼的主要内容,如果未能解决你的问题,请参考以下文章