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提供用來在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接口來指定執(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)文章
解析Java的JNI編程中的對(duì)象引用與內(nèi)存泄漏問題
這篇文章主要介紹了Java的JNI編程中的對(duì)象引用與內(nèi)存泄漏問題,重點(diǎn)講述了局部和全局引用時(shí)一些值得注意的地方,需要的朋友可以參考下2015-11-11IDEA SSM整合Redis項(xiàng)目實(shí)例 附源碼
今天給大家普及IDEA SSM整合Redis項(xiàng)目實(shí)例,包括pom.xml 配置和spring-redis.xml 配置代碼,代碼也很簡(jiǎn)單,通過項(xiàng)目實(shí)際案例能更好的幫助大家理解,需要的朋友可以參考下2021-06-06IDEA配置SpringBoot熱啟動(dòng),以及熱啟動(dòng)失效問題
這篇文章主要介紹了IDEA配置SpringBoot熱啟動(dòng),以及熱啟動(dòng)失效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Java實(shí)現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法
這篇文章主要介紹了Java實(shí)現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Mybatis調(diào)用SQL?Server存儲(chǔ)過程的實(shí)現(xiàn)示例
在軟件開發(fā)過程中,經(jīng)常會(huì)使用到存儲(chǔ)過程,本文就來介紹一下Mybatis調(diào)用SQL?Server存儲(chǔ)過程的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01SpringBoot整合mybatis通用Mapper+自定義通用Mapper方法解析
這篇文章主要介紹了SpringBoot整合mybatis通用Mapper+自定義通用Mapper方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Spring的循環(huán)依賴、三級(jí)緩存解決方案源碼詳細(xì)解析
這篇文章主要介紹了Spring的循環(huán)依賴、三級(jí)緩存解決方案源碼詳細(xì)解析,在Spring中,由于IOC的控制反轉(zhuǎn),創(chuàng)建對(duì)象不再是簡(jiǎn)單的new出來,而是交給Spring去創(chuàng)建,會(huì)經(jīng)歷一系列Bean的生命周期才創(chuàng)建出相應(yīng)的對(duì)象,需要的朋友可以參考下2024-01-01Java使用System.currentTimeMillis()方法計(jì)算程序運(yùn)行時(shí)間的示例代碼
System.currentTimeMillis() 方法的返回類型為 long ,表示毫秒為單位的當(dāng)前時(shí)間,文中通過示例代碼介紹了計(jì)算 String 類型與 StringBuilder 類型拼接字符串的耗時(shí)情況,對(duì)Java計(jì)算程序運(yùn)行時(shí)間相關(guān)知識(shí)感興趣的朋友一起看看吧2022-03-03