詳解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項(xiàng)目中運(yùn)用vue+ElementUI+echarts前后端交互實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)圖(推薦)
今天給大家?guī)?lái)一篇教程關(guān)于Springboot項(xiàng)目中運(yùn)用vue+ElementUI+echarts前后端交互實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)圖的技能,包括環(huán)境配置及圓環(huán)圖前端后端實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2021-06-06springboot打包不同環(huán)境配置以及shell腳本部署的方法
這篇文章主要給大家介紹了關(guān)于springboot打包不同環(huán)境配置以及shell腳本部署的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用springboot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:選擇排序 Selection Sort
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:選擇排序 Selection Sort,本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-06-06SpringBoot解析LocalDateTime失敗:Uniapp傳輸時(shí)間變1970的原因與解決方案
這篇文章主要介紹了SpringBoot解析LocalDateTime失?。縐niapp傳輸時(shí)間變1970的原因與解決方案,文中通過(guò)代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下2025-03-03用Rational Rose逆向工程(java)生成類圖(教程和錯(cuò)誤解決)
Rational Rose有個(gè)很方便的功能,將項(xiàng)目中的JAVA代碼自動(dòng)轉(zhuǎn)換成UML類圖2013-02-02基于Springboot實(shí)現(xiàn)JWT認(rèn)證的示例代碼
本文主要介紹了基于Springboot實(shí)現(xiàn)JWT認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11