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

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

    SpringBoot 項(xiàng)目啟動(dòng)后執(zhí)行代碼

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

    執(zhí)行順序:

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

    MyApplicationRunner1MyCommandLineRunner1MyApplicationRunner2MyCommandLineRunner2MyApplicationRunner3MyCommandLineRunner3

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

    相關(guān)推薦

    聯(lián)系我們

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