Spring Boot解決項(xiàng)目啟動(dòng)時(shí)初始化資源的方法
前言
在我們實(shí)際工作中,總會(huì)遇到這樣需求,在項(xiàng)目啟動(dòng)的時(shí)候需要做一些初始化的操作,比如初始化線程池,提前加載好加密證書(shū)等。今天就給大家介紹一個(gè) Spring Boot 神器,專(zhuān)門(mén)幫助大家解決項(xiàng)目啟動(dòng)初始化資源操作。
這個(gè)神器就是 CommandLineRunner, CommandLineRunner 接口的 Component 會(huì)在所有 SpringBeans都初始化之后, SpringApplication.run()之前執(zhí)行,非常適合在應(yīng)用程序啟動(dòng)之初進(jìn)行一些數(shù)據(jù)初始化的工作。
接下來(lái)我們就運(yùn)用案例測(cè)試它如何使用,在測(cè)試之前在啟動(dòng)類(lèi)加兩行打印提示,方便我們識(shí)別 CommandLineRunner 的執(zhí)行時(shí)機(jī)。
@SpringBootApplicationpublic class CommandLineRunnerApplication {
public static void main(String[] args) {
System.out.println("The service to start.");
SpringApplication.run(CommandLineRunnerApplication.class, args);
System.out.println("The service has started.");
}
}
接下來(lái)我們直接創(chuàng)建一個(gè)類(lèi)繼承 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 ...");
}
}
我們?cè)?run() 方法中打印了一些參數(shù)來(lái)看出它的執(zhí)行時(shí)機(jī)。完成之后啟動(dòng)項(xiàng)目進(jìn)行測(cè)試:
... The service to start. ____ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.RELEASE) ... 2018-04-21 22:21:34.706 INFO 27016 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-04-21 22:21:34.710 INFO 27016 --- [ main] com.neo.CommandLineRunnerApplication : Started CommandLineRunnerApplication in 3.796 seconds (JVM running for 5.128) The Runner start to initialize ... The service has 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)來(lái)進(jìn)行測(cè)試:
第一個(gè)實(shí)現(xiàn)類(lèi):
@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)類(lèi):
@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.0.RELEASE) ... 2018-04-21 22:21:34.706 INFO 27016 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-04-21 22:21:34.710 INFO 27016 --- [ main] com.neo.CommandLineRunnerApplication : Started CommandLineRunnerApplication in 3.796 seconds (JVM running for 5.128) The OrderRunner1 start to initialize ... The OrderRunner2 start to initialize ... The Runner start to initialize ... The service has started.
通過(guò)控制臺(tái)的輸出我們發(fā)現(xiàn),添加 @Order 注解的實(shí)現(xiàn)類(lèi)最先執(zhí)行,并且 @Order()里面的值越小啟動(dòng)越早。
在實(shí)踐中,使用 ApplicationRunner也可以達(dá)到相同的目的,兩著差別不大。
示例代碼:https://github.com/ityouknow/spring-cloud-examples (本地下載)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java進(jìn)階之Object類(lèi)及常用方法詳解
Object?類(lèi)是?Java?默認(rèn)提供的一個(gè)類(lèi),是所有?Java?類(lèi)的祖先類(lèi),每個(gè)類(lèi)都使用?Object?作為父類(lèi)。本文就來(lái)和大家聊聊Object類(lèi)的常用方法,希望對(duì)大家有所幫助2023-01-01
Java中使用json與前臺(tái)Ajax數(shù)據(jù)交互的方法
這篇文章主要為大家詳細(xì)介紹了Java中使用json與前臺(tái)Ajax數(shù)據(jù)交互的方法,分享Ajax獲取顯示Json數(shù)據(jù)的一種方法,感興趣的小伙伴們可以參考一下2016-06-06
springboot掃描引入jar包的service等組件方式
這篇文章主要介紹了springboot掃描引入jar包的service等組件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
PowerShell用戶認(rèn)證Function實(shí)例代碼
這篇文章主要介紹了PowerShell用戶認(rèn)證Function的資料,并附實(shí)例代碼,幫助大家學(xué)習(xí)理解,有需要的小伙伴可以參考下2016-09-09
SpringBoot自定義線程池,執(zhí)行定時(shí)任務(wù)方式
這篇文章主要介紹了SpringBoot自定義線程池,執(zhí)行定時(shí)任務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
SpringBoot 如何添加容器啟動(dòng)的初始化邏輯的操作方法
這篇文章主要介紹了SpringBoot 如何添加容器啟動(dòng)的初始化邏輯,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
spring boot項(xiàng)目快速構(gòu)建的全步驟
這篇文章主要給大家介紹了關(guān)于spring boot項(xiàng)目快速構(gòu)建的全步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

