SpringBoot啟動后初始化的幾種方式匯總
在 Spring Boot 項(xiàng)目中,程序啟動后需要做一些初始化的操作,如需要將一些原始數(shù)據(jù)寫入緩存、或者一些資源的加載等。
一、靜態(tài)代碼塊
當(dāng)我們將某個類交給Spring管理的時候,靜態(tài)代碼塊是優(yōu)先執(zhí)行的,此時可以在代碼塊中做一些初始化操作,這個無需過多解釋。
@Component
@Slf4j
public class TestDemo {
@Value("${netty.port}")
private Integer nettyPort;
static {
log.info("靜態(tài)代碼塊執(zhí)行======");
}
}二、構(gòu)造方法
構(gòu)造方法是靜態(tài)代碼塊之后執(zhí)行的,這種方式也無需過多解釋。
@Component
@Slf4j
public class TestDemo {
@Value("${netty.port}")
private Integer nettyPort;
public TestDemo(){
log.info("構(gòu)造方法執(zhí)行======配置文件讀?。簕}", nettyPort);
}
static {
log.info("靜態(tài)代碼塊執(zhí)行======");
}
}三、@PostConstruct
@PostConstruct 注解,它標(biāo)記的方法會在依賴注入完成后立即被調(diào)用。它適用于簡單的初始化邏輯,執(zhí)行順序較早。
@Component
@Slf4j
public class MyPostConstructBean {
@Value("${netty.port}")
private Integer nettyPort;
@PostConstruct
public void init() {
log.info("PostConstruct執(zhí)行======配置文件讀?。簕}", nettyPort);
}
}四、InitializingBean 接口
實(shí)現(xiàn) InitializingBean 接口并重寫 afterPropertiesSet 方法,它比 @PostConstruct 更具可讀性,適合復(fù)雜的初始化邏輯。它也是在依賴注入完成后調(diào)用,執(zhí)行順序與比@PostConstruct 要早一些。
@Component
@Slf4j
public class MyInitializingBean implements InitializingBean {
@Value("${netty.port}")
private Integer nettyPort;
@Override
public void afterPropertiesSet() {
log.info("InitializingBean接口的afterProperiesSet執(zhí)行======配置文件讀?。簕}", nettyPort);
}
}五、 @Bean 注解中的 initMethod
@Configuration 配置類中的 @Bean 注解,可以指定 initMethod 屬性來定義初始化方法。需要指定初始化方法,它在 @PostConstruct 和 InitializingBean 之后執(zhí)行。
@Configuration
@Slf4j
public class MyInitMethod {
@Value("${netty.port}")
private Integer nettyPort;
@Bean(initMethod = "init")
public MyTestBean myBean() {
return new MyTestBean();
}
class MyTestBean {
public void init() {
log.info("@Bean的initMethod方法執(zhí)行 ==== 配置文件讀?。簕}", nettyPort);
}
}
}六、 CommandLineRunner 接口
實(shí)現(xiàn) CommandLineRunner 接口的類會在 SpringBoot 應(yīng)用啟動完成后執(zhí)行。它可以接收啟動參數(shù)。
@Component
@Slf4j
public class MyCommandLineRunner implements CommandLineRunner {
@Value("${netty.port}")
private Integer nettyPort;
@Override
public void run(String... args) {
log.info("CommandLineRunner接口的run方法執(zhí)行======配置文件讀?。簕}", nettyPort);
}
}七、ApplicationRunner 接口
與 CommandLineRunner 類似。
@Component
@Slf4j
public class MyApplicationRunner implements ApplicationRunner {
@Value("${netty.port}")
private Integer nettyPort;
@Override
public void run(ApplicationArguments args) {
log.info("ApplicationRunner接口的run方法執(zhí)行======配置文件讀?。簕}", nettyPort);
}
}八、@EventListener事件
@EventListener 注解,可以在應(yīng)用啟動的某個生命周期階段執(zhí)行初始化邏輯。比如監(jiān)聽 ContextRefreshedEvent 事件。它適用于需要在特定生命周期事件發(fā)生時執(zhí)行的初始化邏輯,可以監(jiān)聽各種生命周期事件。
@Component
@Slf4j
public class MyContextRefreshedListener {
@Value("${netty.port}")
private Integer nettyPort;
@EventListener
public void handleContextRefreshed(ContextRefreshedEvent event) {
log.info("@EventListener監(jiān)聽ContextRefreshedEvent事件執(zhí)行======配置文件讀?。簕}", nettyPort);
}
}九、SmartInitializingSingleton接口
實(shí)現(xiàn) SmartInitializingSingleton 接口的 afterSingletonsInstantiated 方法,在所有單例 bean 都初始化完成后執(zhí)行。
@Component
@Slf4j
public class MySmartInitializingSingleton implements SmartInitializingSingleton {
@Value("${netty.port}")
private Integer nettyPort;
@Override
public void afterSingletonsInstantiated() {
log.info("SmartInitializingSingleton接口的afterSingletonsInstantiated方法執(zhí)行======配置文件讀?。簕}", nettyPort);
}
}十、ApplicationListener接口
實(shí)現(xiàn) ApplicationListener 接口,可以監(jiān)聽特定的 Spring 事件,如 ApplicationReadyEvent,用于在應(yīng)用完全啟動后執(zhí)行邏輯。監(jiān)聽各種 Spring 事件,提供靈活的初始化時機(jī)。
@Component
@Slf4j
public class MyApplicationReadyListener implements ApplicationListener<ApplicationReadyEvent> {
@Value("${netty.port}")
private Integer nettyPort;
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
log.info("監(jiān)聽ApplicationReadyEvent事件,ApplicationListener接口的onApplicationEvent方法執(zhí)行======配置文件讀?。簕}", nettyPort);
}
}十一、執(zhí)行順序

總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot優(yōu)化啟動速度的方法實(shí)現(xiàn)
本篇文章主要介紹了SpringBoot優(yōu)化啟動速度的方法實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
java開發(fā)RocketMQ之NameServer路由管理源碼分析
這篇文章主要為大家介紹了java開發(fā)中RocketMQ之NameServer路由管理源碼分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11
基于Java Gradle復(fù)制項(xiàng)目模塊過程圖解
這篇文章主要介紹了基于Java Gradle復(fù)制項(xiàng)目模塊過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Springboot集成SpringState狀態(tài)機(jī)的實(shí)現(xiàn)
本文主要介紹了Springboot集成SpringState狀態(tài)機(jī)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-06-06
淺談java調(diào)用Restful API接口的方式
這篇文章主要介紹了淺談java調(diào)用Restful API接口的方式,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12

