午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

springboot使用CommandLineRunner解決項目啟動時初始化資源的操作

瀏覽:71日期:2023-03-26 09:15:59
前言:

在我們實際工作中,總會遇到這樣需求,在項目啟動的時候需要做一些初始化的操作,比如初始化線程池,提前加載好加密證書等。

今天就給大家介紹一個 Spring Boot 神器,專門幫助大家解決項目啟動初始化資源操作。

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

正文:

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

@SpringBootApplicationpublic 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');}}

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

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

啟動項目進(jìn)行測試:

...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ù)控制臺的打印信息我們可以看出 CommandLineRunner 中的方法會在 Spring Boot 容器加載之后執(zhí)行,執(zhí)行完成后項目啟動完成。

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

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

第一個實現(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 ...'); }}

第二個實現(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 ...'); }}

添加完成之后重新啟動,觀察執(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

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

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

以上就是springboot使用CommandLineRunner解決項目啟動時初始化資源的操作的詳細(xì)內(nèi)容,更多關(guān)于springboot 解決項目啟動時初始化資源的操作的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 在线观看色网站 | 亚洲欧美日韩国产一区 | 在线成人免费 | 观看av在线 | 日韩不卡一区二区三区 | 正在播放一区二区 | 激情自拍偷拍 | 三级色网 | 亚洲精品91 | 日本午夜在线 | 国产第一页在线 | 中文无码日韩欧 | 香蕉911 | 久久99精品久久久久久国产越南 | 日韩久久久久久久久 | 久久伊人婷婷 | aaa亚洲精品 | 99re这里只有精品6 | 午夜久久久久久久久久影院 | 亚欧成人精品一区二区 | 亚洲四区| 久草视频手机在线 | 婷婷激情久久 | 综合色av| 久久久久久高清 | 日本少妇做爰全过程毛片 | 国产精品第十页 | 国产午夜一区 | 成人做爰www免费看视频网站 | 日本视频www| 国产调教视频 | 天天色影网 | 日本不卡中文字幕 | 在线成人小视频 | 手机av免费在线观看 | 日韩中文字幕av | 性做久久久久久久久 | wwwwww日本 | 亚洲精品久久久久久久久久 | 国产成人精品白浆久久69 | 亚洲动漫在线 |