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è)方法,該方法將在依賴注入完成后、構(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ù)的訪問(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)代碼塊
在類中添加靜態(tài)代碼塊,這樣在Spring在掃描這類時(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)文章希望大家以后多多支持腳本之家!
- IntelliJ IDEA下SpringBoot如何指定某一個(gè)配置文件啟動(dòng)項(xiàng)目
- springBoot?啟動(dòng)指定配置文件環(huán)境多種方案(最新推薦)
- IDEA下SpringBoot指定環(huán)境、配置文件啟動(dòng)操作過(guò)程
- SpringBoot啟動(dòng)時(shí)加載指定方法的方式小結(jié)
- SpringBoot啟動(dòng)時(shí)如何通過(guò)啟動(dòng)參數(shù)指定logback的位置
- springboot指定profiles啟動(dòng)失敗問(wèn)題及解決
- springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法
- springboot項(xiàng)目啟動(dòng)指定對(duì)應(yīng)環(huán)境的方法
- SpringBoot項(xiàng)目啟動(dòng)時(shí)執(zhí)行指定的方法
相關(guān)文章
SpringBoot整合RestTemplate用法的實(shí)現(xiàn)
本篇主要介紹了RestTemplate中的GET,POST,PUT,DELETE、文件上傳和文件下載6大常用的功能,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
Java那些鮮為人知的關(guān)鍵字volatile詳析
這篇文章主要給大家介紹了關(guān)于Java那些鮮為人知的關(guān)鍵字volatile的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
詳解Java接口簽名(Signature)實(shí)現(xiàn)方案
這篇文章主要介紹了Java接口簽名(Signature)實(shí)現(xiàn)方案?,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
Eclipse創(chuàng)建java程序可執(zhí)行jar包教程
這篇文章主要為大家分享了Eclipse創(chuàng)建java程序可執(zhí)行jar包教程,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
如何用Dos命令運(yùn)行Java版HelloWorld你知道嗎
這篇文章主要介紹了在dos窗口中編譯和運(yùn)行java文件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Spring Boot使用yml格式進(jìn)行配置的方法
很多springboot項(xiàng)目使用的是yml格式,主要目的是方便對(duì)讀懂其他人的項(xiàng)目,下面小編通過(guò)本文給大家分享Spring Boot使用yml格式進(jìn)行配置的方法,需要的朋友參考下吧2018-04-04

