1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringBoot啟動(dòng)時(shí)實(shí)現(xiàn)自動(dòng)執(zhí)行代碼的幾種方式講解

        共 2287字,需瀏覽 5分鐘

         ·

        2022-01-01 12:24

        ????關(guān)注后回復(fù)?“進(jìn)群”?,拉你進(jìn)程序員交流群????


        來源丨blog.csdn.net/u011291072/article/

        details/81813662

        前言

        目前開發(fā)的SpringBoot項(xiàng)目在啟動(dòng)的時(shí)候需要預(yù)加載一些資源。而如何實(shí)現(xiàn)啟動(dòng)過程中執(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)造方法

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

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

        @PostConstruct注解

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

        ApplicationRunner和CommandLineRunner

        SpringBoot提供了兩個(gè)接口來實(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="margin-right: 2px;margin-left: 2px;font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace;word-break: break-all;background: rgb(248, 245, 236);color: rgb(255, 53, 2);line-height: 1.5;font-size: 90%;padding: 3px 5px;border-radius: 2px;">ApplicationRunner的run方法入?yún)?code style="margin-right: 2px;margin-left: 2px;font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace;word-break: break-all;background: rgb(248, 245, 236);color: rgb(255, 53, 2);line-height: 1.5;font-size: 90%;padding: 3px 5px;border-radius: 2px;">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í)使用的訪問應(yīng)用參數(shù)。即我們可以獲取到SpringApplication.run(…)的應(yīng)用參數(shù)。

        Order注解

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

        代碼測試

        為了測試啟動(dòng)時(shí)運(yùn)行的效果和順序,編寫幾個(gè)測試代碼來運(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)過程中,肯定是要自動(dòng)掃描有@Component注解的類,加載類并初始化對象進(jìn)行自動(dòng)注入。加載類時(shí)首先要執(zhí)行static靜態(tài)代碼塊中的代碼,之后再初始化對象時(shí)會(huì)執(zhí)行構(gòu)造方法。

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

        因此,加載順序?yàn)?code style="margin-right: 2px;margin-left: 2px;font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace;word-break: break-all;background: rgb(248, 245, 236);color: rgb(255, 53, 2);line-height: 1.5;font-size: 90%;padding: 3px 5px;border-radius: 2px;">static>constructer>@PostConstruct>CommandLineRunnerApplicationRunner.

        -End-

        最近有一些小伙伴,讓我?guī)兔φ乙恍?面試題?資料,于是我翻遍了收藏的 5T 資料后,匯總整理出來,可以說是程序員面試必備!所有資料都整理到網(wǎng)盤了,歡迎下載!

        點(diǎn)擊??卡片,關(guān)注后回復(fù)【面試題】即可獲取

        在看點(diǎn)這里好文分享給更多人↓↓

        瀏覽 29
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

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

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            免费观看全黄做爰大片现在 | 哪里有毛片 | 84pao国产成人高清视频免费播放 | 亚洲高清在线观看 | 激情男女呻吟一进一出 | 迪丽热巴自慰 | 鲁丝一区二区三区免费 | 哺乳少妇奶水视频播放 | 张开双腿嬷嬷调教宫口h | 娇妻被引诱与老外多p |