springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作
前言:
在我們實(shí)際工作中,總會(huì)遇到這樣需求,在項(xiàng)目啟動(dòng)的時(shí)候需要做一些初始化的操作,比如初始化線程池,提前加載好加密證書(shū)等。
今天就給大家介紹一個(gè) Spring Boot 神器,專門(mén)幫助大家解決項(xiàng)目啟動(dòng)初始化資源操作。
這個(gè)神器就是 CommandLineRunner,CommandLineRunner 接口的 Component 會(huì)在所有 Spring Beans 都初始化之后,SpringApplication.run() 之前執(zhí)行,非常適合在應(yīng)用程序啟動(dòng)之初進(jìn)行一些數(shù)據(jù)初始化的工作。
正文:
接下來(lái)我們就運(yùn)用案例測(cè)試它如何使用,在測(cè)試之前在啟動(dòng)類加兩行打印提示,方便我們識(shí)別 CommandLineRunner 的執(zhí)行時(shí)機(jī)。
@SpringBootApplication
public class SpringbootRabbitmqApplication {
public static void main(String[] args) {
System.out.println("The service to start");
SpringApplication.run(SpringbootRabbitmqApplication.class, args);
System.out.println("The service to started");
}
}
接下來(lái)我們直接創(chuàng)建一個(gè)類繼承 CommandLineRunner ,并實(shí)現(xiàn)它的 run() 方法。
@Component
public class Runner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("The Runner start to initialize ...");
}
}
啟動(dòng)項(xiàng)目進(jìn)行測(cè)試:
... The service to start. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.2.RELEASE) ... 2021-02-01 11:38:31.314 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8078 (http) with context path '' 2021-02-01 11:38:31.317 [main] INFO com.cn.SpringbootRabbitmqApplication - Started SpringbootRabbitmqApplication in 4.124 seconds (JVM running for 6.226) The Runner start to initialize ... The service to started
根據(jù)控制臺(tái)的打印信息我們可以看出 CommandLineRunner 中的方法會(huì)在 Spring Boot 容器加載之后執(zhí)行,執(zhí)行完成后項(xiàng)目啟動(dòng)完成。
如果我們?cè)趩?dòng)容器的時(shí)候需要初始化很多資源,并且初始化資源相互之間有序,那如何保證不同的 CommandLineRunner 的執(zhí)行順序呢?Spring Boot 也給出了解決方案。那就是使用 @Order 注解。
我們創(chuàng)建兩個(gè) CommandLineRunner 的實(shí)現(xiàn)類來(lái)進(jìn)行測(cè)試:
第一個(gè)實(shí)現(xiàn)類:
@Component
@Order(1)
public class OrderRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("The OrderRunner1 start to initialize ...");
}
}
第二個(gè)實(shí)現(xiàn)類:
@Component
@Order(2)
public class OrderRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("The OrderRunner2 start to initialize ...");
}
}
添加完成之后重新啟動(dòng),觀察執(zhí)行順序:
... The service to start. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.2.RELEASE) ... 2021-02-01 11:42:05.724 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8078 (http) with context path '' 2021-02-01 11:42:05.728 [main] INFO com.cn.SpringbootRabbitmqApplication - Started SpringbootRabbitmqApplication in 3.472 seconds (JVM running for 5.473) The OrderRunner1 start to initialize ... The OrderRunner2 start to initialize ... The Runner start to initialize ... The service to started
通過(guò)控制臺(tái)的輸出我們發(fā)現(xiàn),添加 @Order 注解的實(shí)現(xiàn)類最先執(zhí)行,并且@Order()里面的值越小啟動(dòng)越早。
在實(shí)踐中,使用ApplicationRunner也可以達(dá)到相同的目的,兩著差別不大。
以上就是springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作的詳細(xì)內(nèi)容,更多關(guān)于springboot 解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring中一個(gè)少見(jiàn)的引介增強(qiáng)IntroductionAdvisor
這篇文章主要為大家介紹了Spring中一個(gè)少見(jiàn)的引介增強(qiáng)IntroductionAdvisor實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Springboot配置security basic path無(wú)效解決方案
這篇文章主要介紹了Springboot配置security basic path無(wú)效解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java.toCharArray()和charAt()的效率對(duì)比分析
這篇文章主要介紹了Java.toCharArray()和charAt()的效率對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
java針對(duì)于時(shí)間轉(zhuǎn)換的DateUtils工具類
這篇文章主要為大家詳細(xì)介紹了java針對(duì)于時(shí)間轉(zhuǎn)換的DateUtils工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Java基礎(chǔ)之詳解基本數(shù)據(jù)類型的使用
今天給大家?guī)?lái)的是關(guān)于Java基礎(chǔ)的相關(guān)知識(shí),文章圍繞著基本數(shù)據(jù)類型的使用展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
SpringBoot項(xiàng)目啟動(dòng)報(bào)錯(cuò)踩坑實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目啟動(dòng)報(bào)錯(cuò)踩坑的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02
Spring Data JPA 如何使用QueryDsl查詢并分頁(yè)
這篇文章主要介紹了Spring Data JPA 如何使用QueryDsl查詢并分頁(yè),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot?緩存預(yù)熱的實(shí)現(xiàn)
本文主要介紹了SpringBoot?緩存預(yù)熱的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2007-11-11

