SpringBoot啟動后運(yùn)行代碼的幾種方法
Spring Boot啟動后運(yùn)行代碼的方法
實(shí)現(xiàn)步驟
1. 使用ApplicationReadyEvent
ApplicationReadyEvent
會在應(yīng)用刷新并處理完所有相關(guān)回調(diào)后觸發(fā),表明應(yīng)用已準(zhǔn)備好處理請求??梢酝ㄟ^@EventListener
注解監(jiān)聽該事件。
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class StartupRunner { @EventListener(ApplicationReadyEvent.class) public void doSomethingAfterStartup() { System.out.println("hello world, I have just started up"); } }
2. 實(shí)現(xiàn)ApplicationListener<ApplicationReadyEvent>接口
創(chuàng)建一個(gè)類實(shí)現(xiàn)ApplicationListener<ApplicationReadyEvent>
接口,并重寫onApplicationEvent
方法。
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent> { @Override public void onApplicationEvent(final ApplicationReadyEvent event) { // 這里編寫你的代碼 } }
3. 使用CommandLineRunner或ApplicationRunner
CommandLineRunner
和ApplicationRunner
是Spring Boot提供的接口,實(shí)現(xiàn)這些接口并重寫run
方法,Spring Boot會在應(yīng)用啟動過程的末尾執(zhí)行這些方法。
import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class CommandLineAppStartupRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Application started with command-line arguments: " + String.join(", ", args)); } }
4. 使用@PostConstruct注解
在一個(gè)@Component
類中使用@PostConstruct
注解的方法會在Bean初始化完成后執(zhí)行。
import javax.annotation.PostConstruct; import org.springframework.stereotype.Component; @Component public class Monitor { @PostConstruct public void init() { // 在這里啟動監(jiān)控 } }
5. 使用SmartInitializingSingleton
在Spring 4.1及以上版本中,可以使用SmartInitializingSingleton
,它會在所有單例Bean初始化完成后回調(diào)。
import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public SmartInitializingSingleton importProcessor() { return () -> { // 執(zhí)行任務(wù) }; } }
核心代碼
以下是幾種方法的核心代碼示例:
使用ApplicationReadyEvent
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class StartupExample { @EventListener(ApplicationReadyEvent.class) public void executeAfterStartup() { System.out.println("執(zhí)行啟動后任務(wù)"); } }
使用CommandLineRunner
import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class CommandLineExample implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("使用CommandLineRunner執(zhí)行任務(wù)"); } }
使用@PostConstruct
import javax.annotation.PostConstruct; import org.springframework.stereotype.Component; @Component public class PostConstructExample { @PostConstruct public void init() { System.out.println("使用@PostConstruct執(zhí)行任務(wù)"); } }
最佳實(shí)踐
- 如果代碼需要在應(yīng)用準(zhǔn)備好處理請求后執(zhí)行,推薦使用ApplicationReadyEvent。
- 如果需要處理命令行參數(shù),可以使用CommandLineRunner或ApplicationRunner。
- 如果代碼是與Bean初始化相關(guān)的,可以使用@PostConstruct注解。
- 如果需要在所有單例Bean初始化完成后執(zhí)行代碼,可以使用SmartInitializingSingleton。
常見問題
- @Autowired服務(wù)未初始化:使用ApplicationReadyEvent、CommandLineRunner、ApplicationRunner或SmartInitializingSingleton可以確保@Autowired服務(wù)已初始化。
- @PostConstruct方法執(zhí)行過早:@PostConstruct方法在Bean初始化時(shí)執(zhí)行,可能會在應(yīng)用整體準(zhǔn)備好之前執(zhí)行。如果需要在應(yīng)用準(zhǔn)備好后執(zhí)行,不建議使用該注解。
- ContextRefreshedEvent多次觸發(fā):ContextRefreshedEvent可能會多次觸發(fā),需要在代碼中添加判斷邏輯,避免重復(fù)執(zhí)行。
以上就是SpringBoot啟動后運(yùn)行代碼的幾種方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot啟動后運(yùn)行代碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼
這篇文章主要介紹了mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03解決IDEA 左側(cè)Project中沒有out文件夾的問題
這篇文章主要介紹了解決IDEA 左側(cè)Project中沒有out文件夾的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02