<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>

          Spring Boot 啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種方式

          共 2693字,需瀏覽 6分鐘

           ·

          2022-05-31 17:45

          目前開發(fā)的SpringBoot項(xiàng)目在啟動(dòng)的時(shí)候需要預(yù)加載一些資源。而如何實(shí)現(xiàn)啟動(dòng)過(guò)程中執(zhí)行代碼,或啟動(dòng)成功后執(zhí)行,是有很多種方式可以選擇,我們可以在static代碼塊中實(shí)現(xiàn),也可以在構(gòu)造方法里實(shí)現(xiàn),也可以使用@PostConstruct注解實(shí)現(xiàn)。

          當(dāng)然也可以去實(shí)現(xiàn)Spring的ApplicationRunnerCommandLineRunner接口去實(shí)現(xiàn)啟動(dòng)后運(yùn)行的功能。在這里整理一下,在這些位置執(zhí)行的區(qū)別以及加載順序。

          java自身的啟動(dòng)時(shí)加載方式

          static代碼塊

          static靜態(tài)代碼塊,在類加載的時(shí)候即自動(dòng)執(zhí)行。

          構(gòu)造方法

          在對(duì)象初始化時(shí)執(zhí)行。執(zhí)行順序在static靜態(tài)代碼塊之后。

          Spring啟動(dòng)時(shí)加載方式

          @PostConstruct注解

          PostConstruct注解使用在方法上,這個(gè)方法在對(duì)象依賴注入初始化之后執(zhí)行。

          ApplicationRunner和CommandLineRunner

          SpringBoot提供了兩個(gè)接口來(lái)實(shí)現(xiàn)Spring容器啟動(dòng)完成后執(zhí)行的功能,兩個(gè)接口分別為CommandLineRunnerApplicationRunner。

          這兩個(gè)接口需要實(shí)現(xiàn)一個(gè)run方法,將代碼在run中實(shí)現(xiàn)即可。這兩個(gè)接口功能基本一致,其區(qū)別在于run方法的入?yún)ⅰ?code style="padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;font-size: 12px;color: rgb(71, 193, 168);">ApplicationRunner的run方法入?yún)?code style="padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;font-size: 12px;color: rgb(71, 193, 168);">ApplicationArguments,為CommandLineRunner的run方法入?yún)镾tring數(shù)組。

          何為ApplicationArguments

          官方文檔解釋為:

          Provides access to the arguments that were used to run a SpringApplication.

          在Spring應(yīng)用運(yùn)行時(shí)使用的訪問(wèn)應(yīng)用參數(shù)。即我們可以獲取到SpringApplication.run(…)的應(yīng)用參數(shù)。

          Order注解

          當(dāng)有多個(gè)類實(shí)現(xiàn)了CommandLineRunnerApplicationRunner接口時(shí),可以通過(guò)在類上添加@Order注解來(lái)設(shè)定運(yùn)行順序。

          如果你正在學(xué)習(xí)Spring Boot,推薦一個(gè)免費(fèi)教程:http://blog.didispace.com/spring-boot-learning-2x/,教程還有配套倉(cāng)庫(kù),每個(gè)知識(shí)點(diǎn)都有案例可以直接運(yùn)行和參考,趕緊Star收藏起來(lái)吧~

          • Github:https://github.com/dyc87112/SpringBoot-Learning/
          • Gitee:https://gitee.com/didispace/SpringBoot-Learning/

          代碼測(cè)試

          為了測(cè)試啟動(dòng)時(shí)運(yùn)行的效果和順序,編寫幾個(gè)測(cè)試代碼來(lái)運(yùn)行看看。

          TestPostConstruct

          @Component
          public?class?TestPostConstruct?{

          ????static?{
          ????????System.out.println("static");
          ????}
          ????public?TestPostConstruct()?{
          ????????System.out.println("constructer");
          ????}

          ????@PostConstruct
          ????public?void?init()?{
          ????????System.out.println("PostConstruct");
          ????}
          }

          TestApplicationRunner

          @Component
          @Order(1)
          public?class?TestApplicationRunner?implements?ApplicationRunner{
          ????@Override
          ????public?void?run(ApplicationArguments?applicationArguments)?throws?Exception?{
          ????????System.out.println("order1:TestApplicationRunner");
          ????}
          }

          TestCommandLineRunner

          @Component
          @Order(2)
          public?class?TestCommandLineRunner?implements?CommandLineRunner?{
          ????@Override
          ????public?void?run(String...?strings)?throws?Exception?{
          ????????System.out.println("order2:TestCommandLineRunner");
          ????}
          }

          執(zhí)行結(jié)果

          總結(jié)

          Spring應(yīng)用啟動(dòng)過(guò)程中,肯定是要自動(dòng)掃描有@Component注解的類,加載類并初始化對(duì)象進(jìn)行自動(dòng)注入。加載類時(shí)首先要執(zhí)行static靜態(tài)代碼塊中的代碼,之后再初始化對(duì)象時(shí)會(huì)執(zhí)行構(gòu)造方法。

          在對(duì)象注入完成后,調(diào)用帶有@PostConstruct注解的方法。當(dāng)容器啟動(dòng)成功后,再根據(jù)@Order注解的順序調(diào)用CommandLineRunnerApplicationRunner接口類中的run方法。

          因此,加載順序?yàn)?code style="padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;font-size: 12px;color: rgb(71, 193, 168);">static>constructer>@PostConstruct>CommandLineRunnerApplicationRunner.

          我們創(chuàng)建了一個(gè)高質(zhì)量的技術(shù)交流群,與優(yōu)秀的人在一起,自己也會(huì)優(yōu)秀起來(lái),趕緊點(diǎn)擊加群,享受一起成長(zhǎng)的快樂(lè)。另外,如果你最近想跳槽的話,年前我花了2周時(shí)間收集了一波大廠面經(jīng),節(jié)后準(zhǔn)備跳槽的可以點(diǎn)擊這里領(lǐng)取!

          推薦閱讀

          ··································

          你好,我是程序猿DD,10年開發(fā)老司機(jī)、阿里云MVP、騰訊云TVP、出過(guò)書創(chuàng)過(guò)業(yè)、國(guó)企4年互聯(lián)網(wǎng)6年。從普通開發(fā)到架構(gòu)師、再到合伙人。一路過(guò)來(lái),給我最深的感受就是一定要不斷學(xué)習(xí)并關(guān)注前沿。只要你能堅(jiān)持下來(lái),多思考、少抱怨、勤動(dòng)手,就很容易實(shí)現(xiàn)彎道超車!所以,不要問(wèn)我現(xiàn)在干什么是否來(lái)得及。如果你看好一個(gè)事情,一定是堅(jiān)持了才能看到希望,而不是看到希望才去堅(jiān)持。相信我,只要堅(jiān)持下來(lái),你一定比現(xiàn)在更好!如果你還沒(méi)什么方向,可以先關(guān)注我,這里會(huì)經(jīng)常分享一些前沿資訊,幫你積累彎道超車的資本。

          點(diǎn)擊領(lǐng)取2022最新10000T學(xué)習(xí)資料
          瀏覽 42
          點(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>
                  多人操逼视频 | 日韩性爱无码视频 | www.伊人大香蕉 | 黄色国产视频网站 | 欧美黄色免费第一看 |