国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Spring Boot解決項目啟動時初始化資源的方法

2024-07-14 08:40:35
字體:
來源:轉載
供稿:網友

前言

在我們實際工作中,總會遇到這樣需求,在項目啟動的時候需要做一些初始化的操作,比如初始化線程池,提前加載好加密證書等。今天就給大家介紹一個 Spring Boot 神器,專門幫助大家解決項目啟動初始化資源操作。

這個神器就是 CommandLineRunner, CommandLineRunner 接口的 Component 會在所有 SpringBeans都初始化之后, SpringApplication.run()之前執行,非常適合在應用程序啟動之初進行一些數據初始化的工作。

接下來我們就運用案例測試它如何使用,在測試之前在啟動類加兩行打印提示,方便我們識別 CommandLineRunner 的執行時機。

@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.");  }}

接下來我們直接創建一個類繼承 CommandLineRunner ,并實現它的 run() 方法。

@Componentpublic class Runner implements CommandLineRunner {  @Override public void run(String... args) throws Exception { System.out.println("The Runner start to initialize ..."); }}

我們在 run() 方法中打印了一些參數來看出它的執行時機。完成之后啟動項目進行測試:

...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.

根據控制臺的打印信息我們可以看出 CommandLineRunner 中的方法會在 Spring Boot 容器加載之后執行,執行完成后項目啟動完成。

如果我們在啟動容器的時候需要初始化很多資源,并且初始化資源相互之間有序,那如何保證不同的 CommandLineRunner 的執行順序呢?Spring Boot 也給出了解決方案。那就是使用 @Order 注解。

我們創建兩個 CommandLineRunner 的實現類來進行測試:

第一個實現類:

@Component@Order(1)public class OrderRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception {  System.out.println("The OrderRunner1 start to initialize ..."); }}

第二個實現類:

@Component@Order(2)public class OrderRunner2 implements CommandLineRunner { @Override public void run(String... args) throws Exception {  System.out.println("The OrderRunner2 start to initialize ..."); }}

添加完成之后重新啟動,觀察執行順序:

...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.

通過控制臺的輸出我們發現,添加 @Order 注解的實現類最先執行,并且 @Order()里面的值越小啟動越早。

在實踐中,使用 ApplicationRunner也可以達到相同的目的,兩著差別不大。

示例代碼:https://github.com/ityouknow/spring-cloud-examples

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 呈贡县| 黎城县| 巴林右旗| 台湾省| 拜城县| 望都县| 萨迦县| 丰镇市| 纳雍县| 方山县| 余江县| 安徽省| 天等县| 周宁县| 彭州市| 锦屏县| 株洲市| 绥化市| 满城县| 崇礼县| 防城港市| 玛纳斯县| 浦县| 安溪县| 杂多县| 磴口县| 云和县| 漳浦县| 博乐市| 西藏| 屯昌县| 社旗县| 汕头市| 原阳县| 正镶白旗| 阳泉市| 龙江县| 鹤庆县| 内丘县| 盘锦市| 图们市|