springboot項目啟動后執(zhí)行方法的三種方式
springboot項目啟動后執(zhí)行方法,有三種實(shí)現(xiàn)方式。
1 方法
- ApplicationListener< ContextRefreshedEvent> 不推薦
- ApplicationListener 推薦
- CommandLineRunner 推薦
方法1:spring的ApplicationListener< ContextRefreshedEvent>接口
實(shí)現(xiàn)ApplicationListener接口,并實(shí)現(xiàn) onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent)方法
@Service
public class SearchReceive implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保證只執(zhí)行一次
//需要執(zhí)行的方法
}
}
}
方法2:springboot的ApplicationRunner接口
ApplicationListener和CommandLineRunner兩個接口是springBoot提供用來在spring容器加載完成后執(zhí)行指定方法。兩個接口區(qū)別主要是入?yún)⒉煌?/p>
實(shí)現(xiàn)ApplicationRunner接口
@Component
@Order(value = 1)
public class AfterRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("執(zhí)行方法");
}
}
方法3:springboot的CommandLineRunner接口
實(shí)現(xiàn)CommandLineRunner接口
@Component
@Order(value = 2)
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("執(zhí)行方法");
}
}
注:如果同時implements ApplicationListener和CommandLineRunner兩個接口,ApplicationRunner接口的方法先執(zhí)行,CommandLineRunner后執(zhí)行;
@Slf4j
@Component
public class RunnerTest implements ApplicationRunner, CommandLineRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("服務(wù)啟動RunnerTest ApplicationRunner執(zhí)行啟動加載任務(wù)...");
}
@Override
public void run(String... args) throws Exception {
System.out.println("服務(wù)啟動RunnerTest CommandLineRunner 執(zhí)行啟動加載任務(wù)...");
}
}
}
2 指定執(zhí)行順序
當(dāng)項目中同時實(shí)現(xiàn)了ApplicationRunner和CommondLineRunner接口時,可使用Order注解或?qū)崿F(xiàn)Ordered接口來指定執(zhí)行順序,值越小越先執(zhí)行。
3 原理
SpringApplication 的run方法會執(zhí)行afterRefresh方法。
afterRefresh方法會執(zhí)行callRunners方法。
callRunners方法會調(diào)用所有實(shí)現(xiàn)ApplicationRunner和CommondLineRunner接口的方法。
到此這篇關(guān)于springboot項目啟動后執(zhí)行方法的三種方式的文章就介紹到這了,更多相關(guān)springboot啟動后執(zhí)行方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javax.validation.constraints注解使用
這篇文章主要介紹了javax.validation.constraints注解使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
SpringBoot前后端傳輸加密設(shè)計實(shí)現(xiàn)方案
這篇文章主要給大家介紹了關(guān)于SpringBoot前后端傳輸加密設(shè)計實(shí)現(xiàn)方案的相關(guān)資料,包括數(shù)據(jù)加密方案、解密傳輸數(shù)據(jù)實(shí)現(xiàn)方案和響應(yīng)數(shù)據(jù)加密實(shí)現(xiàn)方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11
java實(shí)現(xiàn)酷狗音樂臨時緩存文件轉(zhuǎn)換為MP3文件的方法
這篇文章主要介紹了java實(shí)現(xiàn)酷狗音樂臨時緩存文件轉(zhuǎn)換為MP3文件的方法,涉及java針對文件操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08
SpringBoot整合PageHelper分頁無效的常見原因分析
這篇文章主要介紹了SpringBoot整合PageHelper分頁無效的常見原因分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Spring集成Web環(huán)境與SpringMVC組件的擴(kuò)展使用詳解
這篇文章主要介紹了Spring集成Web環(huán)境與SpringMVC組件,它是一個MVC架構(gòu),用來簡化基于MVC架構(gòu)的Web應(yīng)用開發(fā)。SpringMVC最重要的就是五大組件2022-08-08
IDEA類與方法注釋模板設(shè)置圖文教程(非常詳細(xì))
IDEA自帶的注釋模板不是太好用,我本人到網(wǎng)上搜集了很多資料系統(tǒng)的整理了一下制作了一份比較完整的模板來分享給大家,下面這篇文章主要給大家介紹了關(guān)于IDEA類與方法注釋模板設(shè)置的相關(guān)資料,需要的朋友可以參考下2022-09-09

