springboot啟動(dòng)后和停止前執(zhí)行方法示例詳解
springboot啟動(dòng)后即執(zhí)行的方法
1)實(shí)現(xiàn)ApplicationRunner接口
@Configuration
public class ApplicationService implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
iForwardQueuesService.create();
}
}2)實(shí)現(xiàn)CommandLineRunner接口
@Configuration
public class ApplicationService implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("執(zhí)行平臺(tái)登出");
}
}注意:如果ApplicationListener和CommandLineRunner同時(shí)存在,則ApplicationRunner接口先執(zhí)行,CommandLineRunner后執(zhí)行;
也可以使用執(zhí)行執(zhí)行順序
@Configuration
@Order(1)
public class ApplicationService implements CommandLineRunner {
}原理:
SpringApplication 的run方法會(huì)執(zhí)行afterRefresh方法。
afterRefresh方法會(huì)執(zhí)行callRunners方法。
callRunners方法會(huì)調(diào)用所有實(shí)現(xiàn)ApplicationRunner和CommondLineRunner接口的方法。
springboot停止前執(zhí)行的方法
1)實(shí)現(xiàn)DisposableBean接口并實(shí)現(xiàn)destroy方法
springboot銷毀時(shí)執(zhí)行
@Configuration
public class ApplicationService implements DisposableBean,{
@Override
public void destroy() throws Exception {
log.info("執(zhí)行平臺(tái)登出");
platformService.PlatformLogout();
}
}2)使用ShutdownHook關(guān)閉鉤子
JAVA虛擬機(jī)關(guān)閉鉤子(Shutdown Hook)在下面場(chǎng)景下被調(diào)用:
- 程序正常退出;
- 使用System.exit();
- 終端使用Ctrl+C觸發(fā)的中斷;
4)系統(tǒng)關(guān)閉;
5)OutOfMemory宕機(jī);使用Kill pid命令干掉進(jìn)程(注:在使用kill -9 pid時(shí),是不會(huì)被調(diào)用的);
@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í)行的一個(gè)線程.當(dāng)然這個(gè)線程依然要自己寫.
到此這篇關(guān)于springboot啟動(dòng)后和停止前執(zhí)行方法的文章就介紹到這了,更多相關(guān)springboot啟動(dòng)執(zhí)行方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
圖解Java經(jīng)典算法冒泡排序的原理與實(shí)現(xiàn)
冒泡排序是一種簡(jiǎn)單的排序算法,它也是一種穩(wěn)定排序算法。其實(shí)現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對(duì)相鄰的元素,當(dāng)該對(duì)元素順序不正確時(shí)進(jìn)行交換。一直重復(fù)這個(gè)過(guò)程,直到?jīng)]有任何兩個(gè)相鄰元素可以交換,就表明完成了排序2022-09-09
JAVA解決在@autowired,@Resource注入為null的情況
這篇文章主要介紹了JAVA解決在@autowired,@Resource注入為null的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
java實(shí)現(xiàn)識(shí)別二維碼圖片功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)識(shí)別二維碼圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Java使用Redisson分布式鎖實(shí)現(xiàn)原理
Redisson分布式鎖 之前的基于注解的鎖有一種鎖是基本redis的分布式鎖,這篇文章主要介紹了Java使用Redisson分布式鎖實(shí)現(xiàn)原理,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10
SpringBoot Controller接收參數(shù)的幾種常用方式
這篇文章主要介紹了SpringBoot Controller接收參數(shù)的幾種常用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java+ElasticSearch+Pytorch實(shí)現(xiàn)以圖搜圖功能
這篇文章主要為大家詳細(xì)介紹了Java如何利用ElasticSearch和Pytorch實(shí)現(xiàn)以圖搜圖功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-06-06

