Springboot項(xiàng)目啟動(dòng)成功后可通過五種方式繼續(xù)執(zhí)行
- 實(shí)現(xiàn)CommandLineRunner接口
項(xiàng)目初始化完畢后,才會(huì)調(diào)用方法,提供服務(wù)
@Component public class StartRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunner===================="); } }
- 實(shí)現(xiàn)ApplicationRunner接口
同 CommandLineRunner。只是傳參格式不一樣。CommandLineRunner:沒有任何限制;ApplicationRunner:key-value
@Component public class StartRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) { System.out.println("ApplicationRunner================="); } }
- 實(shí)現(xiàn)ApplicationListener接口
項(xiàng)目初始化完畢后,才會(huì)調(diào)用方法,提供服務(wù)。注意監(jiān)聽的事件,通常是 ApplicationStartedEvent 或者 ApplicationReadyEvent,其他的事件可能無法注入 bean。
@Component public class StartListener implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { System.out.println("ApplicationListener================ApplicationStartedEvent"); } }
如果監(jiān)聽的是 ApplicationStartedEvent 事件,則 ApplicationListener 一定會(huì)在 CommandLineRunner 和 ApplicationRunner 之前執(zhí)行;
如果監(jiān)聽的是 ApplicationReadyEvent 事件,則 ApplicationListener 一定會(huì)在 CommandLineRunner 和 ApplicationRunner 之后執(zhí)行;
順序:
默認(rèn)是 ApplicationRunner 先執(zhí)行,如果雙方指定了@Order 則按照 @Order的大小順序執(zhí)行,小的先執(zhí)行
- @PostConstruct注解
在項(xiàng)目初始化過程中,就會(huì)調(diào)用此方法。如果業(yè)務(wù)邏輯執(zhí)行很耗時(shí),可能會(huì)導(dǎo)致項(xiàng)目啟動(dòng)失敗。
@Component public class StartInit { @PostConstruct public void init() { System.out.println("@PostConstruct==============================="); } }
- 實(shí)現(xiàn)InitalizingBean接口
項(xiàng)目啟動(dòng)時(shí),調(diào)用此方法
@Component public class StartSet implements InitializingBean { @Override public void afterPropertiesSet() { System.out.println("InitializingBean===================="); } }
到此這篇關(guān)于Springboot項(xiàng)目啟動(dòng)成功后可通過五種方式繼續(xù)執(zhí)行的文章就介紹到這了,更多相關(guān)Springboot啟動(dòng)成功后繼續(xù)執(zhí)行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中@Scheduled實(shí)現(xiàn)服務(wù)啟動(dòng)時(shí)執(zhí)行一次
- SpringBoot啟動(dòng)時(shí)執(zhí)行初始化操作的幾種方式
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行指定方法的幾種實(shí)現(xiàn)方式
- 詳解SpringBoot啟動(dòng)項(xiàng)目后執(zhí)行方法的幾種方式
- SpringBoot項(xiàng)目啟動(dòng)執(zhí)行任務(wù)的多種方法小結(jié)
- SpringBoot啟動(dòng)后執(zhí)行方法的五種實(shí)現(xiàn)方式
相關(guān)文章
java實(shí)現(xiàn)學(xué)生成績(jī)錄入系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生成績(jī)錄入系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Java IText異常NoClassDefFoundError: org/bouncycastle
在使用Java進(jìn)行PDF文檔操作時(shí),iText是一個(gè)非常強(qiáng)大的庫,然而,在實(shí)際開發(fā)過程中,可能會(huì)遇到一些異常情況,其中之一就是??NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable??,本文將探討這個(gè)錯(cuò)誤的原因及其解決方案,需要的朋友可以參考下2025-02-02