在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

    SpringBoot 項目啟動后執(zhí)行代碼

    在開發(fā)的過程中,有時需要在應用啟動后自動進行一些操作,比如:項目啟動前初始化資源文件、初始化線程池、提前加載加密證書等等。下邊介紹兩個接口CommandLineRunner 和 ApplicationRunner 來滿足我們的需求,它們會在spring Bean初始化之后SpringApplication run方法執(zhí)行之前調用,如果需要指定執(zhí)行順序,可以使用@Order注解,值越小越先執(zhí)行。

    執(zhí)行順序:

  • 當@Order注解的值相等時,先執(zhí)行ApplicationRunner,然后執(zhí)行CommandLineRunner
  • 當@Order注解的值不相等時,誰小誰先執(zhí)行
  • 當@Order注解的值為空或者未設置@Order注解,放到最后執(zhí)行。先執(zhí)行ApplicationRunner,然后執(zhí)行CommandLineRunner
  • ApplicationRunner

    @Component@Order(1)public class MyApplicationRunner1 implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println(“MyApplicationRunner1”); }}@Component@Order(2)public class MyApplicationRunner2 implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println(“MyApplicationRunner2”); }}@Componentpublic class MyApplicationRunner3 implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println(“MyApplicationRunner3”); }}

    CommandLineRunner

    @Component@Order(1)public class MyCommandLineRunner1 implements CommandLineRunner { @Override public void run(String… args) throws Exception { System.out.println(“MyCommandLineRunner1”); }}@Component@Order(2)public class MyCommandLineRunner2 implements CommandLineRunner { @Override public void run(String… args) throws Exception { System.out.println(“MyCommandLineRunner2”); }}@Componentpublic class MyCommandLineRunner3 implements CommandLineRunner { @Override public void run(String… args) throws Exception { System.out.println(“MyCommandLineRunner3”); }}

    執(zhí)行結果

    MyApplicationRunner1MyCommandLineRunner1MyApplicationRunner2MyCommandLineRunner2MyApplicationRunner3MyCommandLineRunner3

    鄭重聲明:本文內容及圖片均整理自互聯(lián)網,不代表本站立場,版權歸原作者所有,如有侵權請聯(lián)系管理員(admin#wlmqw.com)刪除。
    用戶投稿
    上一篇 2022年6月27日 06:02
    下一篇 2022年6月27日 06:02

    相關推薦

    聯(lián)系我們

    聯(lián)系郵箱:admin#wlmqw.com
    工作時間:周一至周五,10:30-18:30,節(jié)假日休息