Spring Boot 初始化運(yùn)行特定方法解析
Spring Boot提供了兩種 “開(kāi)機(jī)自啟動(dòng)” 的方式,ApplicationRunner和CommandLineRunner
這兩種方式的目的是為了滿足,在容器啟動(dòng)時(shí)like執(zhí)行某些方法。我們可以通過(guò)實(shí)現(xiàn)ApplicationRunner或者CommandLineRunner來(lái)實(shí)現(xiàn),他們都是在SpringAppliaction執(zhí)行之后開(kāi)始執(zhí)行的。這個(gè)特性可以讓我們自定義一些在容器啟動(dòng)時(shí)需要初始化的邏輯
ApplicationRunner接口:
官方doc:
Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple ApplicationRunner beans can be defined within the same application context and can be ordered using the Ordered
當(dāng)該接口包含在SpringApplication中時(shí)執(zhí)行。多個(gè)ApplicationRunner通過(guò)Order直接進(jìn)行排序:
/** * 初始化類 */ @Order(1) // @Order注解可以改變執(zhí)行順序,越小越先執(zhí)行 @Component public class MyApplicationRunner1 implements ApplicationRunner { /** * 會(huì)在服務(wù)啟動(dòng)完成后立即執(zhí)行 */ @Override public void run(ApplicationArguments arg0) throws Exception { System.out.println("MyApplicationRunner1----" + arg0); } }
/** * 初始化類 */ @Order(2) @Component public class MyApplicationRunner2 implements ApplicationRunner { /** * 會(huì)在服務(wù)啟動(dòng)完成后立即執(zhí)行 */ @Override public void run(ApplicationArguments arg0) throws Exception { System.out.println("MyApplicationRunner2----" + arg0); } }
容器啟動(dòng)后的運(yùn)行結(jié)果:
可以看到多個(gè)ApplicationRunner執(zhí)行順序是按照Order中的值執(zhí)行的,并且每個(gè)的入?yún)⒍际峭粋€(gè)ApplicationArguments實(shí)例(具體原因后面分析)
CommandLineRunner接口:
二者的官方doc基本一樣,區(qū)別在于接收的參數(shù)不一樣
/** * 初始化類 */ @Order(1) // @Order注解可以改變執(zhí)行順序,越小越先執(zhí)行 @Component public class MyCommandLineRunner1 implements CommandLineRunner { /** * 會(huì)在服務(wù)啟動(dòng)完成后立即執(zhí)行 */ @Override public void run(String... args) throws Exception { System.out.println("MyCommandLineRunner1----" + args); } }
/** * 初始化類 */ @Order(2) @Component public class MyCommandLineRunner2 implements CommandLineRunner { /** * 會(huì)在服務(wù)啟動(dòng)完成后立即執(zhí)行 */ @Override public void run(String... args) throws Exception { System.out.println("MyCommandLineRunner2----" + args); } }
容器啟動(dòng)后的運(yùn)行結(jié)果:
可以看到多個(gè)CommandLineRunner的執(zhí)行效果跟ApplicationRunner一模一樣
最后看下源碼:
SpringApplication啟動(dòng)時(shí),會(huì)執(zhí)行其run方法中的afterRefresh方法:
在afterRefresh中可以看到這兩個(gè)接口被執(zhí)行,并且每個(gè)ApplicationRunner或CommandLineRunner實(shí)例都是用的同一個(gè)入?yún)ⅲ?/p>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 高并發(fā)十: JDK8對(duì)并發(fā)的新支持詳解
本文主要介紹Java 高并發(fā)JDK8的支持,這里整理了詳細(xì)的資料及1. LongAdder 2. CompletableFuture 3. StampedLock的介紹,有興趣的小伙伴可以參考下2016-09-09Redisson分布式閉鎖RCountDownLatch的使用詳細(xì)講解
分布式鎖和我們java基礎(chǔ)中學(xué)習(xí)到的synchronized略有不同,synchronized中我們的鎖是個(gè)對(duì)象,當(dāng)前系統(tǒng)部署在不同的服務(wù)實(shí)例上,單純使用synchronized或者lock已經(jīng)無(wú)法滿足對(duì)庫(kù)存一致性的判斷。本次主要講解基于rediss實(shí)現(xiàn)的分布式鎖2023-02-02ThreadLocal原理介紹及應(yīng)用場(chǎng)景
本文詳細(xì)講解了ThreadLocal原理介紹及應(yīng)用場(chǎng)景,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12netty?pipeline中的inbound和outbound事件傳播分析
這篇文章主要為大家介紹了netty?pipeline中的inbound和outbound事件傳播分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Mybatis 動(dòng)態(tài)SQL的幾種實(shí)現(xiàn)方法
這篇文章主要介紹了Mybatis 動(dòng)態(tài)SQL的幾種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11