Spring中的ApplicationRunner接口的使用詳解
ApplicationRunner
使用起來很簡單,只需要實現(xiàn)CommandLineRunner或者ApplicationRunner接口,重寫run方法就行。
在springboot完全初始化完畢后,會執(zhí)行CommandLineRunner和ApplicationRunner,兩者唯一的區(qū)別是參數(shù)不同,但是不會影響,都可以獲取到執(zhí)行參數(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完全初始化完畢后,
* 會執(zhí)行CommandLineRunner和ApplicationRunner,
* 兩者唯一的區(qū)別是參數(shù)不同,但是不會影響,都可以獲取到執(zhí)行參數(shù)。
*/
@Slf4j
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args){
log.info("====>>> MyApplicationRunner.run()正在執(zhí)行=========");
}
}
程序結果

CommandLineRunner
代碼實例
package com.wideth.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/***
* 在springboot完全初始化完畢后,
* 會執(zhí)行CommandLineRunner和ApplicationRunner,
* 兩者唯一的區(qū)別是參數(shù)不同,但是不會影響,都可以獲取到執(zhí)行參數(shù)。
*/
@Slf4j
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args){
log.info("====>>> MyCommandLineRunner.run()正在執(zhí)行=========");
}
}
程序結果

ApplicationListener
通過事件監(jiān)聽我們也可以實現(xiàn)springboot啟動執(zhí)行方法。實現(xiàn)ApplicationListener,重寫onApplicationEvent方法,便可在所有的bean加載完畢后執(zhí)行。
觸發(fā)時機
在IOC的容器的啟動過程,當所有的bean都已經處理完成之后,spring ioc容器會有一個發(fā)布ContextRefreshedEvent事件的動作。
使用實例
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的容器的啟動過程,
* 當所有的bean都已經處理完成之后,
* spring ioc容器會有一個發(fā)布
* ContextRefreshedEvent事件的動作。
*/
@Slf4j
@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
log.info("====>>> MyApplicationListener.onApplicationEvent()正在執(zhí)行=========");
}
}
程序結果

注意問題
系統(tǒng)會存在兩個容器,一個是root application context ,另一個就是我們自己的 projectName-servlet context(作為root application context的子容器)
這種情況下,就會造成onApplicationEvent方法被執(zhí)行兩次。為了避免上面提到的問題,我們可以只在root application context初始化完成后調用邏輯代碼,其他的容器的初始化完成,則不做任何處理
//root application context 沒有parent
if (event.getApplicationContext().getParent() == null) {
//邏輯代碼
}
到此這篇關于Spring中的ApplicationRunner接口的使用詳解的文章就介紹到這了,更多相關ApplicationRunner接口的使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
深入了解Java核心類庫--BigDecimal和System類
這篇文章主要為大家詳細介紹了javaBigDecimal和System類定義與使用的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助2021-07-07
spring boot + mybatis實現(xiàn)動態(tài)切換數(shù)據(jù)源實例代碼
這篇文章主要給大家介紹了關于spring boot + mybatis實現(xiàn)動態(tài)切換數(shù)據(jù)源的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-10-10
java HttpClient傳輸json格式的參數(shù)實例講解
這篇文章主要介紹了java HttpClient傳輸json格式的參數(shù)實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
java多線程編程之從線程返回數(shù)據(jù)的兩種方法
從線程中返回數(shù)據(jù)和向線程傳遞數(shù)據(jù)類似。也可以通過類成員以及回調函數(shù)來返回數(shù)據(jù)。但類成員在返回數(shù)據(jù)和傳遞數(shù)據(jù)時有一些區(qū)別,下面讓我們來看看它們區(qū)別在哪2014-01-01

