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

