1. SpringBoot啟動后自動執行方法的各種方式
1.1?@PostConstruct
?注解
作用:在依賴注入完成后執行初始化方法。
適用場景:需要在Bean初始化時執行某些操作(如配置、預加載數據)。
注意:該方法在Bean初始化階段執行,此時應用可能未完全啟動(如數據庫連接未就緒)。
示例代碼:
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;@Component
public class MyPostConstructBean {@PostConstructpublic void init() {System.out.println("PostConstruct 方法執行");// 執行初始化邏輯}
}
1.2.?實現?InitializingBean
?接口
作用:通過重寫?afterPropertiesSet
?方法實現初始化。
適用場景:需要在屬性注入后執行操作,與?@PostConstruct
?類似但需實現接口。
示例代碼:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;@Component
public class MyInitializingBean implements InitializingBean {@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("InitializingBean 的 afterPropertiesSet 方法執行");// 執行初始化邏輯}
}
1.3.?實現?ApplicationRunner
?接口
作用:在Spring應用上下文刷新后執行,支持接收命令行參數。
適用場景:需要在應用啟動完成后執行操作,此時Bean已初始化完畢。
示例代碼:
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;@Component
public class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("ApplicationRunner 執行");// 執行邏輯,可訪問命令行參數 args}
}
1.4.?實現?CommandLineRunner
?接口
作用:與?ApplicationRunner
?類似,但參數為?String[]
。
適用場景:需要接收簡單命令行參數時使用。
示例代碼:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("CommandLineRunner 執行");// 執行邏輯,可訪問 args 參數}
}
1.5. 實現?ApplicationListener
?接口
作用:通過?ApplicationListener
?監聽應用完全啟動事件。
適用場景:需要在應用完全就緒后執行操作(如啟動后發送通知)。
示例代碼:
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;@Component
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {@Overridepublic void onApplicationEvent(ApplicationReadyEvent event) {System.out.println("ApplicationReadyEvent 觸發,應用已啟動");// 執行邏輯,此時可安全訪問所有Bean}
}
1.6. 使用?@Bean
?的?initMethod
?屬性
作用:在Spring配置類中定義Bean時,通過?initMethod
?指定初始化方法。
示例代碼:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Bean(initMethod = "init")public MyBean myBean() {return new MyBean();}
}public class MyBean {public void init() {System.out.println("應用啟動后執行: initMethod");// 在這里添加初始化邏輯}
}
?或使用 init-method
?屬性(XML 配置):
<bean id="myBean" class="com.example.MyBean" init-method="init"/>
2.各方式執行時機對比
方法 | 執行時機 |
---|---|
@PostConstruct | Bean 初始化完成后(屬性注入后)。 |
InitializingBean | 與?@PostConstruct ?類似,屬性注入后。 |
ApplicationRunner | 應用上下文刷新后,ApplicationReadyEvent ?觸發前。 |
CommandLineRunner | 同上。 |
ApplicationListener | 應用完全就緒(所有Bean初始化完成,所有啟動任務執行完畢)。 |
根據需求選擇合適的方式:
- 需要訪問數據庫或資源時,建議使用?
ApplicationListener
?或?CommandLineRunner
。 - 簡單的Bean初始化邏輯可用?
@PostConstruct
?或?InitializingBean
。
注意事項
- 線程問題:默認在主線程執行,避免長時間阻塞操作,可考慮異步執行(如結合?
@Async
)。 - 依賴注入:在?
@PostConstruct
?和?InitializingBean
?中無法直接訪問其他Bean(需等待初始化完成)。 - 順序控制:通過?
@Order
?或實現?Ordered
?接口控制多個啟動任務的順序。
相關文檔:InitializingBean接口和@PostConstruct-筆記-CSDN博客
3.使用?@Order
?控制執行順序
作用:通過?@Order
?或實現?Ordered
?接口控制多個啟動任務的執行順序。
示例:在?ApplicationRunner
?中使用?@Order
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@Component
@Order(1) // 數值越小優先級越高
public class FirstRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) {System.out.println("第一個執行的 Runner");}
}@Component
@Order(2)
public class SecondRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) {System.out.println("第二個執行的 Runner");}
}