SpringBoot啟動后初始化的幾種方式匯總
在 Spring Boot 項目中,程序啟動后需要做一些初始化的操作,如需要將一些原始數(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 接口
實現(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 接口
實現(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接口
實現(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接口
實現(xiàn) ApplicationListener 接口,可以監(jiān)聽特定的 Spring 事件,如 ApplicationReadyEvent,用于在應(yīng)用完全啟動后執(zhí)行邏輯。監(jiān)聽各種 Spring 事件,提供靈活的初始化時機。
@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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud Alibaba Nacos 整合SpringBoot A
這篇文章主要介紹了SpringCloud Alibaba Nacos 整合SpringBoot Admin實戰(zhàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot RESTful風(fēng)格入門講解
RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個資源定位(url)及資源操作的風(fēng)格,這篇文章主要介紹了SpringBoot使用RESTful接口2022-11-11
Java8深入學(xué)習(xí)系列(三)你可能忽略了的新特性
一提到Java 8就只能聽到lambda,但這不過是其中的一個而已,Java 8還有許多新的特性,有一些功能強大的新類或者新的用法,還有一些功能則是早就應(yīng)該加到Java里了,所以下面這篇文章主要給大家介紹了關(guān)于Java8中大家可能忽略了的一些新特性,需要的朋友可以參考下。2017-08-08
java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處
java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處,需要的朋友可以參考一下2013-03-03
解決Mybatis-plus找不到對應(yīng)表及默認表名命名規(guī)則的問題
這篇文章主要介紹了解決Mybatis-plus找不到對應(yīng)表及默認表名命名規(guī)則的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

