<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          springboot 中CommandLineRunner接口的作用

          共 948字,需瀏覽 2分鐘

           ·

          2020-09-06 15:36

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

          ? 作者?|??奮斗1314?

          來(lái)源 |? urlify.cn/quy222

          66套java從入門(mén)到精通實(shí)戰(zhàn)課程分享?

          先看CommandLineRunner接口的API:

          import?org.springframework.core.annotation.Order;
          public?interface?CommandLineRunner?{
          ????/**
          ?????* Callback used to run the bean.
          ?????* @param?args incoming main method arguments
          ?????* @throws?Exception on error
          ?????*/

          ????void?run(String... args)?throws?Exception;
          }

          平常開(kāi)發(fā)中有可能需要實(shí)現(xiàn)在項(xiàng)目啟動(dòng)后執(zhí)行的功能,SpringBoot提供的一種簡(jiǎn)單的實(shí)現(xiàn)方案就是添加一個(gè)model并實(shí)現(xiàn)CommandLineRunner接口,實(shí)現(xiàn)功能的代碼放在實(shí)現(xiàn)的run方法中。

          eg:

          import?org.springframework.boot.CommandLineRunner;
          import?org.springframework.stereotype.Component;

          @Component
          public?class?StartupRunner?implements?CommandLineRunner{
          ????@Override
          ????public?void?run(String... args)?throws?Exception {
          ????????System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作<<<<<<<<<<<<<");
          ????}
          }

          啟動(dòng)截圖示例:

          如果有多個(gè)類(lèi)實(shí)現(xiàn)CommandLineRunner接口,如何保證順序:

          SpringBoot在項(xiàng)目啟動(dòng)后會(huì)遍歷所有實(shí)現(xiàn)CommandLineRunner的實(shí)體類(lèi)并執(zhí)行run方法,如果需要按照一定的順序去執(zhí)行,那么就需要在實(shí)體類(lèi)上使用一個(gè)@Order注解【 @Order(value=1..)】(或者實(shí)現(xiàn)Order接口)來(lái)表明順序.

          eg1:

          import?org.springframework.boot.CommandLineRunner;
          import?org.springframework.core.annotation.Order;
          import?org.springframework.stereotype.Component;

          @Component
          @Order(value=1)
          public class StartupRunnerOne implements CommandLineRunner{
          ????@Override
          ????public void run(String... args) throws Exception {
          ????????System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動(dòng)第一個(gè)開(kāi)始執(zhí)行的任務(wù),執(zhí)行加載數(shù)據(jù)等操作<<<<<<<<<<<<<");
          ????}
          }

          eg2:

          import?org.springframework.boot.CommandLineRunner;
          import?org.springframework.core.annotation.Order;
          import?org.springframework.stereotype.Component;

          @Component
          @Order(value=2)
          public class StartupRunnerTwo implements CommandLineRunner{
          ????@Override
          ????public void run(String... args) throws Exception {
          ????????System.out.println(">>>>>>>>>>>>>>>服務(wù)第二順序啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作<<<<<<<<<<<<<");
          ????}
          }




          粉絲福利:108本java從入門(mén)到大神精選電子書(shū)領(lǐng)取

          ???

          ?長(zhǎng)按上方鋒哥微信二維碼?2 秒
          備注「1234」即可獲取資料以及
          可以進(jìn)入java1234官方微信群



          感謝點(diǎn)贊支持下哈?

          瀏覽 34
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  好逼av| AV一区二区三区 | 99视频精品免费 | 欧美精品在线视频 | 97爱视频 |