一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - springboot使用CommandLineRunner解決項目啟動時初始化資源的操作

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

2021-08-02 11:08maomaojava Java教程

這篇文章主要介紹了springboot使用CommandLineRunner解決項目啟動時初始化資源的操作,幫助大家更好的理解和學習使用springboot框架,感興趣的朋友可以了解下

前言:

 

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

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

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

正文:

 

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

?
1
2
3
4
5
6
7
8
9
10
@springbootapplication
public 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");
    }
 
}

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

?
1
2
3
4
5
6
7
8
9
@component
public class runner implements commandlinerunner {
  
  @override
  public void run(string... args) throws exception {
    system.out.println("the runner start to initialize ...");
  }
  
}

啟動項目進行測試:

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

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

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

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

第一個實現類:

?
1
2
3
4
5
6
7
8
@component
@order(1)
public class orderrunner1 implements commandlinerunner {
  @override
  public void run(string... args) throws exception {
    system.out.println("the orderrunner1 start to initialize ...");
  }
}

第二個實現類:

?
1
2
3
4
5
6
7
8
@component
@order(2)
public class orderrunner2 implements commandlinerunner {
  @override
  public void run(string... args) throws exception {
    system.out.println("the orderrunner2 start to initialize ...");
  }
}

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

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

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

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

以上就是springboot使用commandlinerunner解決項目啟動時初始化資源的操作的詳細內容,更多關于springboot 解決項目啟動時初始化資源的操作的資料請關注服務器之家其它相關文章!

原文鏈接:https://juejin.cn/post/6924146777675268109

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产视频福利 | 精品日韩二区三区精品视频 | 美女靠逼免费视频 | 风间由美m3u8在线 | 国产激情视频网站 | 国产成人精品视频午夜 | 国产日韩欧美综合一区二区三区 | 欧美不卡一区二区三区 | 色综合久久丁香婷婷 | 国产亚洲综合精品一区二区三区 | 18欧美同性videos可播放 | 国产三级跑 | 福利一区三区 | 青草青视频 | 欧美日韩视频在线成人 | 果冻传媒林予曦图片 | 51国产午夜精品免费视频 | 全色黄大色黄大片爽一次 | 精品国产国产精2020久久日 | 高清不卡免费一区二区三区 | 视频大全在线观看免费 | 亚洲国产免费 | 草莓视频榴莲视频 | 亚洲无限| 国产高清专区 | 久久久久久久99精品免费观看 | 男同桌扒开女同桌胸罩喝奶 | sxx免费看视频在线播放 | 狠狠色伊人亚洲综合网站色 | 四虎在线永久免费视频网站 | 嫩草影院久久99 | 日韩国产成人精品视频 | 爽好舒服快想要免费看 | 成人涩涩屋福利视频 | 沉沦艳妇杨幂肉体小说 | 久久精品视频免费 | 午夜福利合集1000在线 | 久久无码AV亚洲精品色午夜麻豆 | 欧美一区二区三区免费高 | 喜欢老头吃我奶躁我的动图 | 17岁韩国在线观看免费1 |