SpringBoot啟動(dòng)后運(yùn)行代碼的幾種方法
Spring Boot啟動(dòng)后運(yùn)行代碼的方法
實(shí)現(xiàn)步驟
1. 使用ApplicationReadyEvent
ApplicationReadyEvent會(huì)在應(yīng)用刷新并處理完所有相關(guān)回調(diào)后觸發(fā),表明應(yīng)用已準(zhǔn)備好處理請(qǐng)求??梢酝ㄟ^(guò)@EventListener注解監(jiān)聽(tīng)該事件。
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會(huì)在應(yīng)用啟動(dòng)過(guò)程的末尾執(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注解的方法會(huì)在Bean初始化完成后執(zhí)行。
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
@Component
public class Monitor {
@PostConstruct
public void init() {
// 在這里啟動(dòng)監(jiān)控
}
}
5. 使用SmartInitializingSingleton
在Spring 4.1及以上版本中,可以使用SmartInitializingSingleton,它會(huì)在所有單例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í)行啟動(dòng)后任務(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)備好處理請(qǐng)求后執(zhí)行,推薦使用ApplicationReadyEvent。
- 如果需要處理命令行參數(shù),可以使用CommandLineRunner或ApplicationRunner。
- 如果代碼是與Bean初始化相關(guān)的,可以使用@PostConstruct注解。
- 如果需要在所有單例Bean初始化完成后執(zhí)行代碼,可以使用SmartInitializingSingleton。
常見(jiàn)問(wèn)題
- @Autowired服務(wù)未初始化:使用ApplicationReadyEvent、CommandLineRunner、ApplicationRunner或SmartInitializingSingleton可以確保@Autowired服務(wù)已初始化。
- @PostConstruct方法執(zhí)行過(guò)早:@PostConstruct方法在Bean初始化時(shí)執(zhí)行,可能會(huì)在應(yīng)用整體準(zhǔn)備好之前執(zhí)行。如果需要在應(yīng)用準(zhǔn)備好后執(zhí)行,不建議使用該注解。
- ContextRefreshedEvent多次觸發(fā):ContextRefreshedEvent可能會(huì)多次觸發(fā),需要在代碼中添加判斷邏輯,避免重復(fù)執(zhí)行。
以上就是SpringBoot啟動(dòng)后運(yùn)行代碼的幾種方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot啟動(dòng)后運(yùn)行代碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
IntelliJ IDEA查看方法說(shuō)明文檔的圖解
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA查看方法說(shuō)明文檔的圖解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼
這篇文章主要介紹了mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
解決IDEA 左側(cè)Project中沒(méi)有out文件夾的問(wèn)題
這篇文章主要介紹了解決IDEA 左側(cè)Project中沒(méi)有out文件夾的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
SpringBoot集成Redis,并自定義對(duì)象序列化操作
這篇文章主要介紹了SpringBoot集成Redis,并自定義對(duì)象序列化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

