Spring中的ApplicationRunner接口的使用詳解
ApplicationRunner
使用起來(lái)很簡(jiǎn)單,只需要實(shí)現(xiàn)CommandLineRunner或者ApplicationRunner接口,重寫(xiě)run方法就行。
在springboot完全初始化完畢后,會(huì)執(zhí)行CommandLineRunner和ApplicationRunner,兩者唯一的區(qū)別是參數(shù)不同,但是不會(huì)影響,都可以獲取到執(zhí)行參數(shù)
代碼實(shí)例
package com.wideth.config; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; /*** * 在springboot完全初始化完畢后, * 會(huì)執(zhí)行CommandLineRunner和ApplicationRunner, * 兩者唯一的區(qū)別是參數(shù)不同,但是不會(huì)影響,都可以獲取到執(zhí)行參數(shù)。 */ @Slf4j @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args){ log.info("====>>> MyApplicationRunner.run()正在執(zhí)行========="); } }
程序結(jié)果
CommandLineRunner
代碼實(shí)例
package com.wideth.config; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /*** * 在springboot完全初始化完畢后, * 會(huì)執(zhí)行CommandLineRunner和ApplicationRunner, * 兩者唯一的區(qū)別是參數(shù)不同,但是不會(huì)影響,都可以獲取到執(zhí)行參數(shù)。 */ @Slf4j @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args){ log.info("====>>> MyCommandLineRunner.run()正在執(zhí)行========="); } }
程序結(jié)果
ApplicationListener
通過(guò)事件監(jiān)聽(tīng)我們也可以實(shí)現(xiàn)springboot啟動(dòng)執(zhí)行方法。實(shí)現(xiàn)ApplicationListener,重寫(xiě)onApplicationEvent方法,便可在所有的bean加載完畢后執(zhí)行。
觸發(fā)時(shí)機(jī)
在IOC的容器的啟動(dòng)過(guò)程,當(dāng)所有的bean都已經(jīng)處理完成之后,spring ioc容器會(huì)有一個(gè)發(fā)布ContextRefreshedEvent事件的動(dòng)作。
使用實(shí)例
package com.wideth.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; /*** * 在IOC的容器的啟動(dòng)過(guò)程, * 當(dāng)所有的bean都已經(jīng)處理完成之后, * spring ioc容器會(huì)有一個(gè)發(fā)布 * ContextRefreshedEvent事件的動(dòng)作。 */ @Slf4j @Component public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { log.info("====>>> MyApplicationListener.onApplicationEvent()正在執(zhí)行========="); } }
程序結(jié)果
注意問(wèn)題
系統(tǒng)會(huì)存在兩個(gè)容器,一個(gè)是root application context ,另一個(gè)就是我們自己的 projectName-servlet context(作為root application context的子容器)
這種情況下,就會(huì)造成onApplicationEvent方法被執(zhí)行兩次。為了避免上面提到的問(wèn)題,我們可以只在root application context初始化完成后調(diào)用邏輯代碼,其他的容器的初始化完成,則不做任何處理
//root application context 沒(méi)有parent if (event.getApplicationContext().getParent() == null) { //邏輯代碼 }
到此這篇關(guān)于Spring中的ApplicationRunner接口的使用詳解的文章就介紹到這了,更多相關(guān)ApplicationRunner接口的使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過(guò)JDBC連接oracle數(shù)據(jù)庫(kù)的十大技巧
通過(guò)JDBC連接oracle數(shù)據(jù)庫(kù)的十大技巧...2006-12-12java實(shí)現(xiàn)簡(jiǎn)單解析XML文件功能示例
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單解析XML文件功能,結(jié)合實(shí)例形式分析了java針對(duì)xml文件的讀取、遍歷節(jié)點(diǎn)及輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Java中向文件寫(xiě)入數(shù)據(jù)的幾種常見(jiàn)方式分享
在日常開(kāi)發(fā)中,肯定離不開(kāi)要和文件打交道,今天就簡(jiǎn)單羅列一下平時(shí)比較常用的創(chuàng)建文件并向文件中寫(xiě)入數(shù)據(jù)的幾種方式,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10JAVA代碼設(shè)置selector不同狀態(tài)下的背景顏色
這篇文章主要介紹了JAVA代碼設(shè)置selector不同狀態(tài)下的背景顏色,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05Spring Security實(shí)現(xiàn)多次登錄失敗后賬戶(hù)鎖定功能
當(dāng)用戶(hù)多次登錄失敗的時(shí)候,我們應(yīng)該將賬戶(hù)鎖定,等待一定的時(shí)間之后才能再次進(jìn)行登錄操作。今天小編給大家分享Spring Security實(shí)現(xiàn)多次登錄失敗后賬戶(hù)鎖定功能,感興趣的朋友一起看看吧2019-11-11MyBatis找不到mapper文件的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis找不到mapper文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
對(duì)比原生Mybatis, Mybatis Plus或者其他框架,F(xiàn)luentMybatis提供了哪些便利呢?很多朋友對(duì)這一問(wèn)題不是很清楚,今天小編給大家?guī)?lái)一篇教程關(guān)于Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一,一起看看吧2021-08-08