springboot項目啟動后執(zhí)行方法的三種方式
springboot項目啟動后執(zhí)行方法,有三種實現(xiàn)方式。
1 方法
- ApplicationListener< ContextRefreshedEvent> 不推薦
- ApplicationListener 推薦
- CommandLineRunner 推薦
方法1:spring的ApplicationListener< ContextRefreshedEvent>接口
實現(xiàn)ApplicationListener接口,并實現(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>
實現(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接口
實現(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í)行順序
當項目中同時實現(xiàn)了ApplicationRunner和CommondLineRunner接口時,可使用Order注解或?qū)崿F(xiàn)Ordered接口來指定執(zhí)行順序,值越小越先執(zhí)行。
3 原理
SpringApplication 的run方法會執(zhí)行afterRefresh方法。
afterRefresh方法會執(zhí)行callRunners方法。
callRunners方法會調(diào)用所有實現(xiàn)ApplicationRunner和CommondLineRunner接口的方法。
到此這篇關(guān)于springboot項目啟動后執(zhí)行方法的三種方式的文章就介紹到這了,更多相關(guān)springboot啟動后執(zhí)行方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析Java的JNI編程中的對象引用與內(nèi)存泄漏問題
這篇文章主要介紹了Java的JNI編程中的對象引用與內(nèi)存泄漏問題,重點講述了局部和全局引用時一些值得注意的地方,需要的朋友可以參考下2015-11-11Java實現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法
這篇文章主要介紹了Java實現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01Mybatis調(diào)用SQL?Server存儲過程的實現(xiàn)示例
在軟件開發(fā)過程中,經(jīng)常會使用到存儲過程,本文就來介紹一下Mybatis調(diào)用SQL?Server存儲過程的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-01-01SpringBoot整合mybatis通用Mapper+自定義通用Mapper方法解析
這篇文章主要介紹了SpringBoot整合mybatis通用Mapper+自定義通用Mapper方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Spring的循環(huán)依賴、三級緩存解決方案源碼詳細解析
這篇文章主要介紹了Spring的循環(huán)依賴、三級緩存解決方案源碼詳細解析,在Spring中,由于IOC的控制反轉(zhuǎn),創(chuàng)建對象不再是簡單的new出來,而是交給Spring去創(chuàng)建,會經(jīng)歷一系列Bean的生命周期才創(chuàng)建出相應(yīng)的對象,需要的朋友可以參考下2024-01-01Java使用System.currentTimeMillis()方法計算程序運行時間的示例代碼
System.currentTimeMillis() 方法的返回類型為 long ,表示毫秒為單位的當前時間,文中通過示例代碼介紹了計算 String 類型與 StringBuilder 類型拼接字符串的耗時情況,對Java計算程序運行時間相關(guān)知識感興趣的朋友一起看看吧2022-03-03