詳解SpringBoot啟動項目后執(zhí)行方法的幾種方式
在項目開發(fā)中某些場景必須要用到啟動項目后立即執(zhí)行方式的功能,如我們需要去初始化數(shù)據(jù)到redis緩存、設(shè)置策略工廠,或者啟動后讀取相應(yīng)的配置等,主要聊聊實現(xiàn)立即執(zhí)行的幾種方法。
一、CommandLineRunner
實現(xiàn)CommandLineRunner接口 然后在run方法里面調(diào)用需要調(diào)用的方法即可,好處是方法執(zhí)行時,項目已經(jīng)初始化完畢,是可以正常提供服務(wù)的。
同時該方法也可以接受參數(shù),可以根據(jù)項目啟動時: java -jar demo.jar arg1 arg2 arg3 傳入的參數(shù)進行一些處理。
@Component public class CommandLineRunnerImpl implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println(Arrays.toString(args)); } }
二、 ApplicationRunner
這兩者的實現(xiàn)方法一樣,都是去繼承相應(yīng)的接口然后重寫run方法即可,也都是SpringBoot框架所提供給我們的接口,也是項目中最常用的,比較靈活,有多個CommandLineRunner或ApplicationRunner實現(xiàn)類時可以通過@Order注解或?qū)崿F(xiàn)Ordered接口重寫getOrder方法實現(xiàn)按指定順序執(zhí)行。
@Order(1) //通過order注解指定執(zhí)行順序 @Component public class CommandLineRunnerInit implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunner 啟動后執(zhí)行方法..."); } }
ApplicationRunner 通過order注解指定執(zhí)行順序
@Order(2) //通過order注解指定執(zhí)行順序 @Component public class ApplicationRunnerInit implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner 啟動后執(zhí)行方法..."); } }
ApplicationRunner 通過實現(xiàn) Ordered 指定執(zhí)行順序
@Component // 通過實現(xiàn) Ordered 指定執(zhí)行順序 public class ApplicationRunnerInit implements ApplicationRunner, Ordered { ? ? @Override ? ? public void run(ApplicationArguments args) throws Exception { ? ? ? ? System.out.println("ApplicationRunner 啟動后執(zhí)行方法..."); ? ? } ? ? @Override ? ? public int getOrder() { ? ? ? ?return 0; ? ? } }
這兩者的不同其實就是run方法中所接收的參數(shù)類型不同,CommandLineRunner是對我們啟動jar包時所傳參數(shù)不進行處理,原樣返回String類型給你,ApplicationRunner是封裝成了ApplicationArguments參數(shù),通過這個類型可以更方便的判斷某些參數(shù)是否存在之類的。
三、JDK提供的@PostConstruct
@PostConstruct是JDK所提供的注解,使用該注解的方法會在服務(wù)器加載Servlet的時候運行。也可以在一個類中寫多個方法并添加這個注解。
@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 一般被用來獲取applicationContext上下文信息,并且也可以啟動時被執(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)聽,啟動會執(zhí)行多個監(jiān)聽器【會多次執(zhí)行】,具體可了解Springboot服務(wù)啟動過程。
@Component public class ApplicationListenerTest implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { System.out.println("ApplicationListener 方法執(zhí)行..."); } }
3.3 InitializingBean
InitializingBean 初始化啟動后方法執(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 啟動后執(zhí)行方法...
CommandLineRunner 啟動后執(zhí)行方法...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
存在多個ApplicationRunner、CommandLineRunner可以通過order指定其執(zhí)行順序,但其執(zhí)行順序沒有ApplicationContextAware等高。但一般生產(chǎn)中使用ApplicationRunner、CommandLineRunner,因為其更加靈活。
到此這篇關(guān)于詳解SpringBoot啟動項目后執(zhí)行方法的幾種方式的文章就介紹到這了,更多相關(guān)SpringBoot啟動后執(zhí)行方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot項目中運用vue+ElementUI+echarts前后端交互實現(xiàn)動態(tài)圓環(huán)圖(推薦)
今天給大家?guī)硪黄坛剃P(guān)于Springboot項目中運用vue+ElementUI+echarts前后端交互實現(xiàn)動態(tài)圓環(huán)圖的技能,包括環(huán)境配置及圓環(huán)圖前端后端實現(xiàn)代碼,感興趣的朋友一起看看吧2021-06-06springboot打包不同環(huán)境配置以及shell腳本部署的方法
這篇文章主要給大家介紹了關(guān)于springboot打包不同環(huán)境配置以及shell腳本部署的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用springboot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Java數(shù)據(jù)結(jié)構(gòu)及算法實例:選擇排序 Selection Sort
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:選擇排序 Selection Sort,本文直接給出實現(xiàn)代碼,代碼中包含詳細注釋,需要的朋友可以參考下2015-06-06SpringBoot解析LocalDateTime失?。篣niapp傳輸時間變1970的原因與解決方案
這篇文章主要介紹了SpringBoot解析LocalDateTime失???Uniapp傳輸時間變1970的原因與解決方案,文中通過代碼示例給大家講解的非常詳細,需要的朋友可以參考下2025-03-03用Rational Rose逆向工程(java)生成類圖(教程和錯誤解決)
Rational Rose有個很方便的功能,將項目中的JAVA代碼自動轉(zhuǎn)換成UML類圖2013-02-02基于Springboot實現(xiàn)JWT認證的示例代碼
本文主要介紹了基于Springboot實現(xiàn)JWT認證,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11