SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行指定方法的幾種實(shí)現(xiàn)方式
在Spring Boot應(yīng)用程序中,要實(shí)現(xiàn)在應(yīng)用啟動(dòng)時(shí)自動(dòng)執(zhí)行某些代碼,可以采用以下幾種方式:
1. 使用@PostConstruct注解
@PostConstruct注解用于標(biāo)記一個(gè)方法,該方法將在依賴(lài)注入完成后、構(gòu)造方法之后自動(dòng)執(zhí)行。這適用于需要在對(duì)象創(chuàng)建后立即執(zhí)行的初始化邏輯。
import javax.annotation.PostConstruct;
@Component
public class UsePostConstruct {
@PostConstruct
public void init() {
// 啟動(dòng)時(shí)自動(dòng)執(zhí)行的代碼
}
}
2. 實(shí)現(xiàn)CommandLineRunner或ApplicationRunner接口
這兩個(gè)接口都包含了一個(gè)run方法,該方法會(huì)在Spring應(yīng)用上下文準(zhǔn)備就緒后被調(diào)用。ApplicationRunner是CommandLineRunner的增強(qiáng)版,它提供了對(duì)命令行參數(shù)的訪(fǎng)問(wèn)能力。
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class UseCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 啟動(dòng)時(shí)自動(dòng)執(zhí)行的代碼
}
}
3. 使用@EventListener注解
@EventListener注解可以用來(lái)監(jiān)聽(tīng)Spring框架的事件。如果你想在Spring容器完全啟動(dòng)后執(zhí)行某些操作,可以監(jiān)聽(tīng)ContextRefreshedEvent。
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class UseEventListener {
@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
// 應(yīng)用上下文初始化完畢后自動(dòng)執(zhí)行的代碼
}
}
4. 使用InitializingBean接口
InitializingBean接口提供了一個(gè)afterPropertiesSet方法,該方法會(huì)在所有屬性設(shè)置完成后自動(dòng)執(zhí)行。
import org.springframework.beans.factory.InitializingBean;
public class UseInitializingBean implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
// 啟動(dòng)時(shí)自動(dòng)執(zhí)行的代碼
}
}
5. 使用ServletContextListener接口
ServletContextListener是一個(gè)在Servlet規(guī)范中定義的監(jiān)聽(tīng)器接口,這個(gè)接口有個(gè)contextInitialized(ServletContextEvent sce)方法是在Web應(yīng)用被Servlet容器(如Tomcat)加載并初始化時(shí)調(diào)用。
@Component
public class UseServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
// 啟動(dòng)時(shí)自動(dòng)執(zhí)行的代碼
ServletContextListener.super.contextInitialized(sce);
}
}
6. 使用ApplicationContextAware接口
ApplicationContextAware是Spring框架中的一個(gè)接口,它允許Bean獲取到Spring的ApplicationContext。這個(gè)接口中只有一個(gè)方法setApplicationContext(ApplicationContext applicationContext)在創(chuàng)建這個(gè)Bean的實(shí)例之后會(huì)自動(dòng)調(diào)。
@Component
@Slf4j
public class UseApplicationContextAware implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// 啟動(dòng)時(shí)自動(dòng)執(zhí)行的代碼
}
}
7. 使用靜態(tài)代碼塊
在類(lèi)中添加靜態(tài)代碼塊,這樣在Spring在掃描這類(lèi)時(shí)候就會(huì)自動(dòng)執(zhí)行靜態(tài)代碼,從而達(dá)到代碼自動(dòng)運(yùn)行的效果。
@Component
public class UseStatic {
static{
// 啟動(dòng)時(shí)自動(dòng)執(zhí)行的代碼
}
}
到此這篇關(guān)于SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行指定方法的幾種方式的文章就介紹到這了,更多相關(guān)SpringBoot自動(dòng)執(zhí)行指定方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot工程啟動(dòng)時(shí)自動(dòng)執(zhí)行任務(wù)實(shí)現(xiàn)方式
- SpringBoot啟動(dòng)后自動(dòng)執(zhí)行方法的各種方式對(duì)比
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行特定代碼的完整指南
- SpringBoot啟動(dòng)后自動(dòng)執(zhí)行初始化任務(wù)的五種方法
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式
- springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟
- springBoot啟動(dòng)時(shí)讓方法自動(dòng)執(zhí)行的幾種實(shí)現(xiàn)方式
- SpringBoot 啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種方式講解
相關(guān)文章
spring?boot入門(mén)之誕生背景及優(yōu)勢(shì)影響
這篇文章主要為大家描述說(shuō)明了介紹了spring?boot誕生的背景以及其產(chǎn)生的優(yōu)勢(shì)影響,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Java中CompletableFuture?的詳細(xì)介紹
這篇文章主要介紹了Java中的CompletableFuture,通過(guò)創(chuàng)建?CompletableFuture?的對(duì)象的工廠方法展開(kāi)詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-05-05
java 二維數(shù)組矩陣乘法的實(shí)現(xiàn)方法
java 二維數(shù)組矩陣乘法的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-03-03
Spring Boot 聲明式調(diào)用 Feign 從入門(mén)到精通實(shí)例詳解
這篇文章給大家介紹Spring Boot聲明式調(diào)用Feign從入門(mén)到精通實(shí)例詳解,本文全面介紹了Feign的使用和配置,涵蓋基礎(chǔ)和高級(jí)特性,以及在復(fù)雜場(chǎng)景中的應(yīng)用,感興趣的朋友跟隨小編一起看看吧2026-04-04
上傳自己的jar包到maven中央倉(cāng)庫(kù)的快速操作方法
網(wǎng)絡(luò)上可以搜索到很多jar包到中央倉(cāng)庫(kù),但是都不是多適合自己的項(xiàng)目,于是自己動(dòng)手寫(xiě)個(gè),本文檔通過(guò)sonatype上傳jar包至maven中央倉(cāng)庫(kù),Sonatype通過(guò)JIRA來(lái)管理OSSRH倉(cāng)庫(kù),具體實(shí)例代碼跟隨小編一起看看吧2021-08-08
Java代理模式及動(dòng)態(tài)代理使用及說(shuō)明
文章解釋了代理模式的概念及其應(yīng)用場(chǎng)景,介紹了靜態(tài)代理和動(dòng)態(tài)代理的特點(diǎn)和應(yīng)用場(chǎng)景,并詳細(xì)講解了基于JDK原生動(dòng)態(tài)代理實(shí)現(xiàn)日志記錄功能的方法2026-04-04

