免费爱碰视频在线观看,九九精品国产屋,欧美亚洲尤物久久精品,1024在线观看视频亚洲

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

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

      執(zhí)行順序:

    1. 當(dāng)@Order注解的值相等時,先執(zhí)行ApplicationRunner,然后執(zhí)行CommandLineRunner
    2. 當(dāng)@Order注解的值不相等時,誰小誰先執(zhí)行
    3. 當(dāng)@Order注解的值為空或者未設(shè)置@Order注解,放到最后執(zhí)行。先執(zhí)行ApplicationRunner,然后執(zhí)行CommandLineRunner
    4. 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í)行結(jié)果

      MyApplicationRunner1MyCommandLineRunner1MyApplicationRunner2MyCommandLineRunner2MyApplicationRunner3MyCommandLineRunner3

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

      相關(guān)推薦

      聯(lián)系我們

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