springboot啟動后和停止前執(zhí)行方法示例詳解
springboot啟動后即執(zhí)行的方法
1)實現(xiàn)ApplicationRunner接口
@Configuration public class ApplicationService implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { iForwardQueuesService.create(); } }
2)實現(xiàn)CommandLineRunner接口
@Configuration public class ApplicationService implements CommandLineRunner { @Override public void run(String... args) throws Exception { log.info("執(zhí)行平臺登出"); } }
注意:如果ApplicationListener和CommandLineRunner同時存在,則ApplicationRunner接口先執(zhí)行,CommandLineRunner后執(zhí)行;
也可以使用執(zhí)行執(zhí)行順序
@Configuration @Order(1) public class ApplicationService implements CommandLineRunner { }
原理:
SpringApplication 的run方法會執(zhí)行afterRefresh方法。
afterRefresh方法會執(zhí)行callRunners方法。
callRunners方法會調用所有實現(xiàn)ApplicationRunner和CommondLineRunner接口的方法。
springboot停止前執(zhí)行的方法
1)實現(xiàn)DisposableBean接口并實現(xiàn)destroy方法
springboot銷毀時執(zhí)行
@Configuration public class ApplicationService implements DisposableBean,{ @Override public void destroy() throws Exception { log.info("執(zhí)行平臺登出"); platformService.PlatformLogout(); } }
2)使用ShutdownHook關閉鉤子
JAVA虛擬機關閉鉤子(Shutdown Hook)在下面場景下被調用:
- 程序正常退出;
- 使用System.exit();
- 終端使用Ctrl+C觸發(fā)的中斷;
4)系統(tǒng)關閉;
5)OutOfMemory宕機;使用Kill pid命令干掉進程(注:在使用kill -9 pid時,是不會被調用的);
@SpringBootApplication @ComponentScan(value = "com.xxxxxx") public class ForwardGbApplication { public static void main(String[] args) { ForwardGbApplication application=new ForwardGbApplication(); Thread t = new Thread(new ShutdownHook(application), "ShutdownHook-Thread"); Runtime.getRuntime().addShutdownHook(t); SpringApplication.run(ForwardGbApplication.class, args); } static class ShutdownHook implements Runnable{ private ForwardGbApplication manager; public ShutdownHook(ForwardGbApplication serverManager){ manager = serverManager; } @Override public void run() { try { PlatformService platform = ApplicationContextHandle.getObject(PlatformService.class); platform.PlatformLogout(); } catch (Exception e) { e.printStackTrace(); } } } }
RunTime.getRunTime().addShutdownHook的作用就是在JVM銷毀前執(zhí)行的一個線程.當然這個線程依然要自己寫.
到此這篇關于springboot啟動后和停止前執(zhí)行方法的文章就介紹到這了,更多相關springboot啟動執(zhí)行方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
5分鐘快速創(chuàng)建spring boot項目的完整步驟
這篇文章主要給大家介紹了關于通過5分鐘快速創(chuàng)建spring boot項目的完整步驟,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決
這篇文章主要介紹了Java BufferWriter寫文件寫不進去或缺失數(shù)據(jù)的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07JDBC中PreparedStatement詳解以及應用場景實例介紹
PreparedStatement對象代表的是一個預編譯的SQL語句,用它提供的setter方法可以傳入查詢的變量,這篇文章主要給大家介紹了關于JDBC中PreparedStatement詳解以及應用場景實例介紹的相關資料,需要的朋友可以參考下2024-02-02Java經(jīng)典設計模式之模板方法模式定義與用法示例
這篇文章主要介紹了Java經(jīng)典設計模式之模板方法模式,簡單說明了模板方法模式的原理、定義,并結合實例形式分析了java模板方法模式的具體使用方法,需要的朋友可以參考下2017-08-08用Maven打成可執(zhí)行jar,包含maven依賴,本地依賴的操作
這篇文章主要介紹了用Maven打成可執(zhí)行jar,包含maven依賴,本地依賴的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Java并發(fā) synchronized鎖住的內容解析
這篇文章主要介紹了Java并發(fā) synchronized鎖住的內容解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09淺談Mybatis中resultType為hashmap的情況
這篇文章主要介紹了淺談Mybatis中resultType為hashmap的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12