SpringBoot啟動時自動執(zhí)行指定方法的幾種實(shí)現(xiàn)方式
在Spring Boot應(yīng)用程序中,要實(shí)現(xiàn)在應(yīng)用啟動時自動執(zhí)行某些代碼,可以采用以下幾種方式:
1. 使用@PostConstruct注解
@PostConstruct注解用于標(biāo)記一個方法,該方法將在依賴注入完成后、構(gòu)造方法之后自動執(zhí)行。這適用于需要在對象創(chuàng)建后立即執(zhí)行的初始化邏輯。
import javax.annotation.PostConstruct;
@Component
public class UsePostConstruct {
@PostConstruct
public void init() {
// 啟動時自動執(zhí)行的代碼
}
}
2. 實(shí)現(xiàn)CommandLineRunner或ApplicationRunner接口
這兩個接口都包含了一個run方法,該方法會在Spring應(yīng)用上下文準(zhǔn)備就緒后被調(diào)用。ApplicationRunner是CommandLineRunner的增強(qiáng)版,它提供了對命令行參數(shù)的訪問能力。
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class UseCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 啟動時自動執(zhí)行的代碼
}
}
3. 使用@EventListener注解
@EventListener注解可以用來監(jiān)聽Spring框架的事件。如果你想在Spring容器完全啟動后執(zhí)行某些操作,可以監(jiān)聽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)用上下文初始化完畢后自動執(zhí)行的代碼
}
}
4. 使用InitializingBean接口
InitializingBean接口提供了一個afterPropertiesSet方法,該方法會在所有屬性設(shè)置完成后自動執(zhí)行。
import org.springframework.beans.factory.InitializingBean;
public class UseInitializingBean implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
// 啟動時自動執(zhí)行的代碼
}
}
5. 使用ServletContextListener接口
ServletContextListener是一個在Servlet規(guī)范中定義的監(jiān)聽器接口,這個接口有個contextInitialized(ServletContextEvent sce)方法是在Web應(yīng)用被Servlet容器(如Tomcat)加載并初始化時調(diào)用。
@Component
public class UseServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
// 啟動時自動執(zhí)行的代碼
ServletContextListener.super.contextInitialized(sce);
}
}
6. 使用ApplicationContextAware接口
ApplicationContextAware是Spring框架中的一個接口,它允許Bean獲取到Spring的ApplicationContext。這個接口中只有一個方法setApplicationContext(ApplicationContext applicationContext)在創(chuàng)建這個Bean的實(shí)例之后會自動調(diào)。
@Component
@Slf4j
public class UseApplicationContextAware implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// 啟動時自動執(zhí)行的代碼
}
}
7. 使用靜態(tài)代碼塊
在類中添加靜態(tài)代碼塊,這樣在Spring在掃描這類時候就會自動執(zhí)行靜態(tài)代碼,從而達(dá)到代碼自動運(yùn)行的效果。
@Component
public class UseStatic {
static{
// 啟動時自動執(zhí)行的代碼
}
}
到此這篇關(guān)于SpringBoot啟動時自動執(zhí)行指定方法的幾種方式的文章就介紹到這了,更多相關(guān)SpringBoot自動執(zhí)行指定方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot工程啟動時自動執(zhí)行任務(wù)實(shí)現(xiàn)方式
- SpringBoot啟動后自動執(zhí)行方法的各種方式對比
- SpringBoot啟動時自動執(zhí)行特定代碼的完整指南
- SpringBoot啟動后自動執(zhí)行初始化任務(wù)的五種方法
- SpringBoot啟動時自動執(zhí)行代碼的幾種實(shí)現(xiàn)方式
- springboot 項(xiàng)目容器啟動后如何自動執(zhí)行指定方法
- SpringBoot啟動時自動執(zhí)行sql腳本的方法步驟
- springBoot啟動時讓方法自動執(zhí)行的幾種實(shí)現(xiàn)方式
- SpringBoot 啟動時自動執(zhí)行代碼的幾種方式講解
相關(guān)文章
Spring Boot 的創(chuàng)建和運(yùn)行示例代碼詳解
Spring Boot 的誕生是為了簡化Spring程序的開發(fā),今天給大家介紹下Spring Boot 的創(chuàng)建和運(yùn)行,主要包括Spring Boot基本概念和springboot優(yōu)點(diǎn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-07-07
java內(nèi)存泄漏與內(nèi)存溢出關(guān)系解析
這篇文章主要介紹了java內(nèi)存泄漏與內(nèi)存溢出關(guān)系解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
教您如何3分鐘快速搞定EasyExcel導(dǎo)入與導(dǎo)出功能
對于EasyExcel庫,我們可以使用它來實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入和導(dǎo)出,下面這篇文章主要給大家介紹了關(guān)于如何3分鐘快速搞定EasyExcel導(dǎo)入與導(dǎo)出功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Mybatis-Plus Wrapper條件構(gòu)造器超詳細(xì)使用教程
接口方法的參數(shù)中,會出現(xiàn)各種 Wrapper,比如 queryWrapper、updateWrapper 等。Wrapper 的作用就是用于定義各種各樣的條件(where)。所以不管是查詢、更新、刪除都會用到Wrapper2022-03-03

