SpringBoot啟動(dòng)后初始化的幾種方式匯總
在 Spring Boot 項(xiàng)目中,程序啟動(dòng)后需要做一些初始化的操作,如需要將一些原始數(shù)據(jù)寫(xiě)入緩存、或者一些資源的加載等。
一、靜態(tài)代碼塊
當(dāng)我們將某個(gè)類(lèi)交給Spring管理的時(shí)候,靜態(tài)代碼塊是優(yōu)先執(zhí)行的,此時(shí)可以在代碼塊中做一些初始化操作,這個(gè)無(wú)需過(guò)多解釋。
@Component
@Slf4j
public class TestDemo {
@Value("${netty.port}")
private Integer nettyPort;
static {
log.info("靜態(tài)代碼塊執(zhí)行======");
}
}二、構(gòu)造方法
構(gòu)造方法是靜態(tài)代碼塊之后執(zhí)行的,這種方式也無(wú)需過(guò)多解釋。
@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)記的方法會(huì)在依賴(lài)注入完成后立即被調(diào)用。它適用于簡(jiǎn)單的初始化邏輯,執(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 接口并重寫(xiě) afterPropertiesSet 方法,它比 @PostConstruct 更具可讀性,適合復(fù)雜的初始化邏輯。它也是在依賴(lài)注入完成后調(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 配置類(lèi)中的 @Bean 注解,可以指定 initMethod 屬性來(lái)定義初始化方法。需要指定初始化方法,它在 @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 接口的類(lèi)會(huì)在 SpringBoot 應(yīng)用啟動(dòng)完成后執(zhí)行。它可以接收啟動(dòng)參數(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 類(lèi)似。
@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)用啟動(dòng)的某個(gè)生命周期階段執(zhí)行初始化邏輯。比如監(jiān)聽(tīng) ContextRefreshedEvent 事件。它適用于需要在特定生命周期事件發(fā)生時(shí)執(zhí)行的初始化邏輯,可以監(jiān)聽(tīng)各種生命周期事件。
@Component
@Slf4j
public class MyContextRefreshedListener {
@Value("${netty.port}")
private Integer nettyPort;
@EventListener
public void handleContextRefreshed(ContextRefreshedEvent event) {
log.info("@EventListener監(jiān)聽(tīng)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)聽(tīng)特定的 Spring 事件,如 ApplicationReadyEvent,用于在應(yīng)用完全啟動(dòng)后執(zhí)行邏輯。監(jiān)聽(tīng)各種 Spring 事件,提供靈活的初始化時(shí)機(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)聽(tīng)ApplicationReadyEvent事件,ApplicationListener接口的onApplicationEvent方法執(zhí)行======配置文件讀取:{}", nettyPort);
}
}十一、執(zhí)行順序

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot集成es插入和查詢(xún)的簡(jiǎn)單使用示例詳解
這篇文章主要介紹了springboot集成es 插入和查詢(xún)的簡(jiǎn)單使用,本文分步驟結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
SpringCloud Alibaba Nacos 整合SpringBoot A
這篇文章主要介紹了SpringCloud Alibaba Nacos 整合SpringBoot Admin實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot RESTful風(fēng)格入門(mén)講解
RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個(gè)資源定位(url)及資源操作的風(fēng)格,這篇文章主要介紹了SpringBoot使用RESTful接口2022-11-11
Java8深入學(xué)習(xí)系列(三)你可能忽略了的新特性
一提到Java 8就只能聽(tīng)到lambda,但這不過(guò)是其中的一個(gè)而已,Java 8還有許多新的特性,有一些功能強(qiáng)大的新類(lèi)或者新的用法,還有一些功能則是早就應(yīng)該加到Java里了,所以下面這篇文章主要給大家介紹了關(guān)于Java8中大家可能忽略了的一些新特性,需要的朋友可以參考下。2017-08-08
Spring boot項(xiàng)目整合WebSocket方法
這篇文章主要介紹了WebSocket使用Spring boot整合方法,需要繼承webSocketHandler類(lèi),重寫(xiě)幾個(gè)方法就可以了,具體實(shí)例代碼跟隨小編一起看看吧2021-09-09
java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處
java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處,需要的朋友可以參考一下2013-03-03
解決Mybatis-plus找不到對(duì)應(yīng)表及默認(rèn)表名命名規(guī)則的問(wèn)題
這篇文章主要介紹了解決Mybatis-plus找不到對(duì)應(yīng)表及默認(rèn)表名命名規(guī)則的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11

