詳解SpringBoot啟動(dòng)項(xiàng)目后執(zhí)行方法的幾種方式
在項(xiàng)目開發(fā)中某些場(chǎng)景必須要用到啟動(dòng)項(xiàng)目后立即執(zhí)行方式的功能,如我們需要去初始化數(shù)據(jù)到redis緩存、設(shè)置策略工廠,或者啟動(dòng)后讀取相應(yīng)的配置等,主要聊聊實(shí)現(xiàn)立即執(zhí)行的幾種方法。
一、CommandLineRunner
實(shí)現(xiàn)CommandLineRunner接口 然后在run方法里面調(diào)用需要調(diào)用的方法即可,好處是方法執(zhí)行時(shí),項(xiàng)目已經(jīng)初始化完畢,是可以正常提供服務(wù)的。
同時(shí)該方法也可以接受參數(shù),可以根據(jù)項(xiàng)目啟動(dòng)時(shí): java -jar demo.jar arg1 arg2 arg3 傳入的參數(shù)進(jìn)行一些處理。
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(Arrays.toString(args));
}
}二、 ApplicationRunner
這兩者的實(shí)現(xiàn)方法一樣,都是去繼承相應(yīng)的接口然后重寫run方法即可,也都是SpringBoot框架所提供給我們的接口,也是項(xiàng)目中最常用的,比較靈活,有多個(gè)CommandLineRunner或ApplicationRunner實(shí)現(xiàn)類時(shí)可以通過(guò)@Order注解或?qū)崿F(xiàn)Ordered接口重寫getOrder方法實(shí)現(xiàn)按指定順序執(zhí)行。
@Order(1) //通過(guò)order注解指定執(zhí)行順序
@Component
public class CommandLineRunnerInit implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner 啟動(dòng)后執(zhí)行方法...");
}
}ApplicationRunner 通過(guò)order注解指定執(zhí)行順序
@Order(2) //通過(guò)order注解指定執(zhí)行順序
@Component
public class ApplicationRunnerInit implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner 啟動(dòng)后執(zhí)行方法...");
}
}ApplicationRunner 通過(guò)實(shí)現(xiàn) Ordered 指定執(zhí)行順序
@Component
// 通過(guò)實(shí)現(xiàn) Ordered 指定執(zhí)行順序
public class ApplicationRunnerInit implements ApplicationRunner, Ordered {
? ? @Override
? ? public void run(ApplicationArguments args) throws Exception {
? ? ? ? System.out.println("ApplicationRunner 啟動(dòng)后執(zhí)行方法...");
? ? }
? ? @Override
? ? public int getOrder() {
? ? ? ?return 0;
? ? }
}這兩者的不同其實(shí)就是run方法中所接收的參數(shù)類型不同,CommandLineRunner是對(duì)我們啟動(dòng)jar包時(shí)所傳參數(shù)不進(jìn)行處理,原樣返回String類型給你,ApplicationRunner是封裝成了ApplicationArguments參數(shù),通過(guò)這個(gè)類型可以更方便的判斷某些參數(shù)是否存在之類的。
三、JDK提供的@PostConstruct
@PostConstruct是JDK所提供的注解,使用該注解的方法會(huì)在服務(wù)器加載Servlet的時(shí)候運(yùn)行。也可以在一個(gè)類中寫多個(gè)方法并添加這個(gè)注解。
@Component
public class PostConstructTest {
? ? @PostConstruct
? ? public void inti1() {
? ? ? ? System.out.println("@PostConstruct 方法1執(zhí)行...");
? ? }
? ? @PostConstruct
? ? public void inti2() {
? ? ? ? System.out.println("@PostConstruct 方法2執(zhí)行...");
? ? }
}三、其他方法
3.1 ApplicationContextAware
ApplicationContextAware 一般被用來(lái)獲取applicationContext上下文信息,并且也可以啟動(dòng)時(shí)被執(zhí)行
@Component
public class ApplicationContextAwareTest implements ApplicationContextAware {
? ? private ApplicationContext applicationContext;
? ? @Override
? ? public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
? ? ? ? this.applicationContext = applicationContext;
? ? ? ? System.out.println("ApplicationContextAware 方法執(zhí)行...");
? ? }
}3.2 ApplicationListener
ApplicationListener 事件監(jiān)聽,啟動(dòng)會(huì)執(zhí)行多個(gè)監(jiān)聽器【會(huì)多次執(zhí)行】,具體可了解Springboot服務(wù)啟動(dòng)過(guò)程。
@Component
public class ApplicationListenerTest implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("ApplicationListener 方法執(zhí)行...");
}
}3.3 InitializingBean
InitializingBean 初始化啟動(dòng)后方法執(zhí)行
@Component
public class InitializingBeanInit implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean 方法執(zhí)行...");
}
}四、總結(jié)
以上多種初始執(zhí)行方法日志輸出為:
ApplicationContextAware 方法執(zhí)行...
InitializingBean 方法執(zhí)行...
@PostConstruct 方法1執(zhí)行...
@PostConstruct 方法2執(zhí)行...ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
ApplicationRunner 啟動(dòng)后執(zhí)行方法...
CommandLineRunner 啟動(dòng)后執(zhí)行方法...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
存在多個(gè)ApplicationRunner、CommandLineRunner可以通過(guò)order指定其執(zhí)行順序,但其執(zhí)行順序沒(méi)有ApplicationContextAware等高。但一般生產(chǎn)中使用ApplicationRunner、CommandLineRunner,因?yàn)槠涓屿`活。
到此這篇關(guān)于詳解SpringBoot啟動(dòng)項(xiàng)目后執(zhí)行方法的幾種方式的文章就介紹到這了,更多相關(guān)SpringBoot啟動(dòng)后執(zhí)行方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中@Scheduled實(shí)現(xiàn)服務(wù)啟動(dòng)時(shí)執(zhí)行一次
- SpringBoot啟動(dòng)時(shí)執(zhí)行初始化操作的幾種方式
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行指定方法的幾種實(shí)現(xiàn)方式
- Springboot項(xiàng)目啟動(dòng)成功后可通過(guò)五種方式繼續(xù)執(zhí)行
- SpringBoot項(xiàng)目啟動(dòng)執(zhí)行任務(wù)的多種方法小結(jié)
- SpringBoot啟動(dòng)后執(zhí)行方法的五種實(shí)現(xiàn)方式
相關(guān)文章
SpringBoot業(yè)務(wù)邏輯異常的處理方法介紹
本篇文章為大家展示了如何在SpringBoot中統(tǒng)一處理邏輯異常,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲2022-09-09
基于SpringBoot+AOP實(shí)現(xiàn)操作日志記錄
本文介紹了使用日志記錄的實(shí)現(xiàn)方法,步驟包括明確記錄信息、搭建基礎(chǔ)環(huán)境、創(chuàng)建操作日志實(shí)體類和自定義注解、創(chuàng)建AOP切面,重點(diǎn)在于精準(zhǔn)控制需要記錄日志的接口,通過(guò)自定義注解和使用、切點(diǎn)表達(dá)式、環(huán)繞通知實(shí)現(xiàn)實(shí)現(xiàn)日志記錄邏輯,需要的朋友可以參考下2026-04-04
SpringBoot+JavaMailSender實(shí)現(xiàn)騰訊企業(yè)郵箱配置
這篇文章主要介紹了SpringBoot+JavaMailSender實(shí)現(xiàn)騰訊企業(yè)郵箱配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java 實(shí)現(xiàn)Excel文檔添加超鏈接的代碼
超鏈接即內(nèi)容鏈接,通過(guò)給特定對(duì)象設(shè)置超鏈接,可實(shí)現(xiàn)載體與特定網(wǎng)頁(yè)、文件、郵件、網(wǎng)絡(luò)等的鏈接,點(diǎn)擊鏈接載體可打開鏈接目標(biāo),在文檔處理中是一種比較常用的功能,本文將介紹通過(guò)Java程序給Excel文檔添加超鏈接的方法,感興趣的朋友一起看看吧2020-02-02
解決JavaWeb讀取本地json文件以及亂碼的問(wèn)題
今天小編就為大家分享一篇解決JavaWeb讀取本地json文件以及亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
IntelliJ IDEA下自動(dòng)生成Hibernate映射文件以及實(shí)體類
這篇文章主要介紹了IntelliJ IDEA下自動(dòng)生成Hibernate映射文件以及實(shí)體類,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

