色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作

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

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

今天就給大家介紹一個(gè) Spring Boot 神器,專門幫助大家解決項(xiàng)目啟動(dòng)初始化資源操作。

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

正文:

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

@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)建一個(gè)類繼承 CommandLineRunner ,并實(shí)現(xiàn)它的 run() 方法。

@Componentpublic 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)類來進(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

通過控制臺(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)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 午夜影院免费入口 | 国产亚洲视频在线播放大全 | 国产精品成aⅴ人片在线观看 | 国产高颜值露脸在线观看 | 国产精品视频自拍 | 亚洲性综合 | 亚洲精品在线视频 | 亚洲一区二区三区精品影院 | 国产成人刺激视频在线观看 | 一级毛片播放 | 美女张开腿 | 国产一级毛片卡 | 国产成人精品免费视频网页大全 | 欧美日韩一级片在线观看 | 欧美一级α片毛片免费观看 | 亚洲日韩精品欧美一区二区一 | 日本乱人伦片中文字幕三区 | 九九精品免费 | 免费在线一级毛片 | 美女黄视频网站 | 亚洲午夜精品在线 | avav在线看 | 日韩在线二区全免费 | 成年女人aaaaa毛片 | 久草国产视频 | 怡红院精品视频 | 成年人免费毛片 | 亚洲a免费| 欧美性xxxx18 | 波多野结衣在线观看高清免费资源 | 亚洲精品天堂一区在线观看 | 欧美性猛交xxx免费看人妖 | 国产黄色三级三级三级 | 免费a级毛片大学生免费观看 | 成人欧美一区二区三区黑人 | 精品国产亚洲人成在线 | 荡女妇边被c边呻吟久久 | 看全黄男人和女人视频 | 亚洲2020天天堂在线观看 | 午夜精品成人毛片 | 在线精品国产三级 |