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 啟動時自動執(zhí)行代碼的幾種方式,還有誰不會??

        共 2189字,需瀏覽 5分鐘

         ·

        2022-05-16 01:31



        1

        目錄


        • 前言

        • java自身的啟動時加載方式

        • Spring啟動時加載方式

        • 代碼測試

        • 總結(jié)



        2

        前言


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


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



        3

        java自身的啟動時加載方式


        static代碼塊


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


        構(gòu)造方法


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



        4

        Spring啟動時加載方式


        @PostConstruct注解


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


        ApplicationRunner和CommandLineRunner


        SpringBoot提供了兩個接口來實現(xiàn)Spring容器啟動完成后執(zhí)行的功能,兩個接口分別為CommandLineRunner和ApplicationRunner。


        這兩個接口需要實現(xiàn)一個run方法,將代碼在run中實現(xiàn)即可。這兩個接口功能基本一致,其區(qū)別在于run方法的入?yún)?。ApplicationRunner的run方法入?yún)锳pplicationArguments,為CommandLineRunner的run方法入?yún)镾tring數(shù)組。


        何為ApplicationArguments


        官方文檔解釋為:


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


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


        Order注解


        當有多個類實現(xiàn)了CommandLineRunner和ApplicationRunner接口時,可以通過在類上添加@Order注解來設(shè)定運行順序。



        5

        代碼測試


        為了測試啟動時運行的效果和順序,編寫幾個測試代碼來運行看看。


        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é)果




        6

        總結(jié)


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


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


        因此,加載順序為static>constructer>@PostConstruct>CommandLineRunner和ApplicationRunner.


        來源:blog.csdn.net/u011291072/article/details/81813662


        PS:防止找不到本篇文章,可以收藏點贊,方便翻閱查找哦。

        如有文章對你有幫助,

        在看”和轉(zhuǎn)發(fā)是對我最大的支持!

        推薦

        點擊領(lǐng)?。?span style="outline: 0px;max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">151個大廠面試講解!(圖片可上下滑動?。??

        瀏覽 46
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        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>
            国产精品久久久久久一二三四五 | 丰满老妇高潮一级A片 | 欧美色图亚洲综合 | 亚洲欧洲一区二区 | 啊轻点灬太粗嗯在浴室太深了用力 | 亚洲一级a免费在线观看 | 午夜福利国产精品 | 大香蕉性爱视频 | 五月丁香啪啪啪网 | 骚骚影院 |