日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 2.0(七):Spring Boot 如何解决项目启动时初始化资源

發(fā)布時間:2025/3/21 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 2.0(七):Spring Boot 如何解决项目启动时初始化资源 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

這個神器就是?CommandLineRunner,CommandLineRunner?接口的?Component?會在所有?Spring Beans都初始化之后,SpringApplication.run()之前執(zhí)行,非常適合在應(yīng)用程序啟動之初進(jìn)行一些數(shù)據(jù)初始化的工作。

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

@SpringBootApplication public 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.");} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

接下來我們直接創(chuàng)建一個類繼承?CommandLineRunner?,并實(shí)現(xiàn)它的?run()?方法。

@Component public class Runner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("The Runner start to initialize ...");} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

我們在?run()?方法中打印了一些參數(shù)來看出它的執(zhí)行時機(jī)。完成之后啟動項(xiàng)目進(jìn)行測試:

... 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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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

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

我們創(chuàng)建兩個?CommandLineRunner?的實(shí)現(xiàn)類來進(jìn)行測試:

第一個實(shí)現(xiàn)類:

@Component @Order(1) public class OrderRunner1 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("The OrderRunner1 start to initialize ...");} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

第二個實(shí)現(xiàn)類:

@Component @Order(2) public class OrderRunner2 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("The OrderRunner2 start to initialize ...");} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

添加完成之后重新啟動,觀察執(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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

在實(shí)踐中,使用ApplicationRunner也可以達(dá)到相同的目的,兩著差別不大。看來使用 Spring Boot 解決初始化資源的問題非常簡單。

示例代碼-github

示例代碼-碼云

總結(jié)

以上是生活随笔為你收集整理的Spring Boot 2.0(七):Spring Boot 如何解决项目启动时初始化资源的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。