兩種Spring服務關閉時對象銷毀的實現(xiàn)方法
spring提供了兩種方式用于實現(xiàn)對象銷毀時去執(zhí)行操作
1.實現(xiàn)DisposableBean接口的destroy
2.在bean類的方法上增加@PreDestroy方法,那么這個方法會在DisposableBean.destory方法前觸發(fā)
3.實現(xiàn)SmartLifecycle接口的stop方法
package com.wyf.service; import org.springframework.beans.factory.DisposableBean; import org.springframework.context.Lifecycle; import org.springframework.context.SmartLifecycle; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Component; import javax.annotation.PreDestroy; @Component public class UserService implements DisposableBean, SmartLifecycle { boolean isRunning = false; @Override public void destroy() throws Exception { System.out.println(this.getClass().getSimpleName()+" is destroying....."); } @PreDestroy public void preDestory(){ System.out.println(this.getClass().getSimpleName()+" is pre destory...."); } @Override public void start() { System.out.println(this.getClass().getSimpleName()+" is start.."); isRunning=true; } @Override public void stop() { System.out.println(this.getClass().getSimpleName()+" is stop..."); isRunning=false; } @Override public boolean isRunning() { return isRunning; } }
那么這個時候我們?nèi)右粋€spring容器
package com.wyf; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); } }
這個時候其實銷毀方法是不會執(zhí)行的,我們可以通過,調(diào)用close方法觸發(fā)或者調(diào)用registerShutdownHook注冊一個鉤子來在容器關閉時觸發(fā)銷毀方法
package com.wyf; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); //添加一個關閉鉤子,用于觸發(fā)對象銷毀操作 context.registerShutdownHook(); context.close(); } }
實際上我們?nèi)ゲ榭丛创a會發(fā)現(xiàn)本質(zhì)上這兩種方式都是去調(diào)用了同一個方法org.springframework.context.support.AbstractApplicationContext#doClose
@Override public void registerShutdownHook() { if (this.shutdownHook == null) { // No shutdown hook registered yet. this.shutdownHook = new Thread(SHUTDOWN_HOOK_THREAD_NAME) { @Override public void run() { synchronized (startupShutdownMonitor) { doClose(); } } }; Runtime.getRuntime().addShutdownHook(this.shutdownHook); } }
registerShutdownHook
方法其實是創(chuàng)建了一個jvm shutdownhook(關閉鉤子),這個鉤子本質(zhì)上是一個線程,他會在jvm關閉的時候啟動并執(zhí)行線程實現(xiàn)的方法。而spring的關閉鉤子實現(xiàn)則是執(zhí)行了org.springframework.context.support.AbstractApplicationContext#doClose
這個方法去執(zhí)行一些spring的銷毀方法
@Override public void close() { synchronized (this.startupShutdownMonitor) { doClose(); // If we registered a JVM shutdown hook, we don't need it anymore now: // We've already explicitly closed the context. if (this.shutdownHook != null) { try { Runtime.getRuntime().removeShutdownHook(this.shutdownHook); } catch (IllegalStateException ex) { // ignore - VM is already shutting down } } } }
而close方法則是執(zhí)行直接執(zhí)行了doClose
方法,并且在執(zhí)行之后會判斷是否注冊了關閉鉤子,如果注冊了則注銷掉這個鉤子,因為已經(jīng)執(zhí)行過doClose了,不應該再執(zhí)行一次
@Override public void close() { synchronized (this.startupShutdownMonitor) { doClose(); // If we registered a JVM shutdown hook, we don't need it anymore now: // We've already explicitly closed the context. if (this.shutdownHook != null) { try { Runtime.getRuntime().removeShutdownHook(this.shutdownHook); } catch (IllegalStateException ex) { // ignore - VM is already shutting down } } } }
doClose方法源碼分析
@SuppressWarnings("deprecation") protected void doClose() { // Check whether an actual close attempt is necessary... //判斷是否有必要執(zhí)行關閉操作 //如果容器正在執(zhí)行中,并且以CAS的方式設置關閉標識成功,則執(zhí)行后續(xù)關閉操作,當然這個標識僅僅是標識,并沒有真正修改容器的狀態(tài) if (this.active.get() && this.closed.compareAndSet(false, true)) { if (logger.isDebugEnabled()) { logger.debug("Closing " + this); } if (!NativeDetector.inNativeImage()) { LiveBeansView.unregisterApplicationContext(this); } try { // Publish shutdown event. //發(fā)布容器關閉事件,通知所有監(jiān)聽器 publishEvent(new ContextClosedEvent(this)); } catch (Throwable ex) { logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex); } // Stop all Lifecycle beans, to avoid delays during individual destruction. //如果存在bean實現(xiàn)的Lifecycle接口,則執(zhí)行onClose(),lifecycleProcessor會對所有Lifecycle進行分組然后分批執(zhí)行stop方法 if (this.lifecycleProcessor != null) { try { this.lifecycleProcessor.onClose(); } catch (Throwable ex) { logger.warn("Exception thrown from LifecycleProcessor on context close", ex); } } // Destroy all cached singletons in the context's BeanFactory. //銷毀所有緩存的單例bean destroyBeans(); // Close the state of this context itself. //關閉bean工廠 closeBeanFactory(); // Let subclasses do some final clean-up if they wish... //為子類預留的方法允許子類去自定義一些銷毀操作 onClose(); // Reset local application listeners to pre-refresh state. //將本地應用程序偵聽器重置為預刷新狀態(tài)。 if (this.earlyApplicationListeners != null) { this.applicationListeners.clear(); this.applicationListeners.addAll(this.earlyApplicationListeners); } // Switch to inactive. //設置上下文到狀態(tài)為關閉狀態(tài) this.active.set(false); } }
- 首先判斷當前容器是否正在運行中,然后嘗試通過CAS的方式設置關閉標識為true,這相當于一個鎖,避免其他線程再去執(zhí)行關閉操作。
- 發(fā)布容器關閉事件,通知所有監(jiān)聽器,監(jiān)聽器收到事件后執(zhí)行其實現(xiàn)的監(jiān)聽方法
- 如果存在bean實現(xiàn)的Lifecycle接口,并且正在運行中,則執(zhí)行Lifecycle.stop()方法,需要注意的是如果是實現(xiàn)Lifecycle,那么start方法需要使用context.start()去顯示調(diào)用才會執(zhí)行,而實現(xiàn)SmartLifecycle則會自動執(zhí)行,而stop方法是否執(zhí)行依賴于isRunning()方法的返回,如果為true那么無論是用哪一種Lifecycle實現(xiàn),則都會執(zhí)行stop,當然,你也可以實現(xiàn)isRunning方法讓他默認返回true,那么你也就無需去關注start了。
- 銷毀所有的單例bean,這里會去執(zhí)行實現(xiàn)了
org.springframework.beans.factory.DisposableBean#destroy
方法的bean的destroy方法,以及其帶有@PreDestroy注解的方法。 - 關閉Bean工廠,這一步很簡單,就是設置當前上下文持有的bean工廠引用為null即可
- 執(zhí)行onClose()方法,這里是為子類預留的擴展,不同的ApplicationContext有不同的實現(xiàn)方式,但是本文主講的不是這個就不談了
- 將本地應用程序偵聽器重置為預刷新狀態(tài)。
- 將ApplicationContext的狀態(tài)設置為關閉狀態(tài),容器正式關閉完成。
tips:其實Lifecycle不算是bean銷毀時的操作,而是bean銷毀前操作,這個是bean生命周期管理實現(xiàn)的接口,相當于spring除了自己去對bean的生命周期管理之外,還允許你通過這個接口來在bean的不同生命周期階段去執(zhí)行各種邏輯,我個人理解和另外兩種方法的本質(zhì)上是差不多的,只是誰先執(zhí)行誰后執(zhí)行的問題,Lifecycle只不過是把這些能力集成在一個接口里面方便管理和使用。
到此這篇關于兩種Spring服務關閉時對象銷毀的實現(xiàn)方法的文章就介紹到這了,更多相關Spring對象銷毀內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
簡單易懂的java8新特性之lambda表達式知識總結(jié)
一直想針對lambda表達式作一個總結(jié),借助于這次公司安排的考試作一個入門式的總結(jié),對正在學習java的小伙伴們非常有幫助,需要的朋友可以參考下2021-05-05SpringCloud zookeeper作為注冊中心使用介紹
ZooKeeper由雅虎研究院開發(fā),是Google Chubby的開源實現(xiàn),后來托管到Apache,于2010年11月正式成為Apache的頂級項目。ZooKeeper是一個經(jīng)典的分布式數(shù)據(jù)一致性解決方案,致力于為分布式應用提供一個高性能、高可用,且具有嚴格順序訪問控制能力的分布式協(xié)調(diào)服務2022-11-11java異常處理執(zhí)行順序詳解try catch finally
try catch語句是java語言用于捕獲異常并進行處理的標準方式,對于try catch及try catch finally執(zhí)行順序必須有深入的了解2021-10-10舉例講解Java的Spring框架中AOP程序設計方式的使用
這篇文章主要介紹了Java的Spring框架中AOP程序設計方式的使用講解,文中舉的AOP下拋出異常的例子非常實用,需要的朋友可以參考下2016-04-04淺談JDK8中的Duration Period和ChronoUnit
在JDK8中,引入了三個非常有用的時間相關的API:Duration,Period和ChronoUnit。他們都是用來對時間進行統(tǒng)計的,本文將會詳細講解一下這三個API的使用2021-06-06SpringBoot設置靜態(tài)資源訪問控制和封裝集成方案
這篇文章主要介紹了SpringBoot靜態(tài)資源訪問控制和封裝集成方案,關于springboot靜態(tài)資源訪問的問題,小編是通過自定義webconfig實現(xiàn)WebMvcConfigurer,重寫addResourceHandlers方法,具體完整代碼跟隨小編一起看看吧2021-08-08