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啟動時(shí)讓方法自動執(zhí)行的幾種實(shí)現(xiàn)方式

        共 2119字,需瀏覽 5分鐘

         ·

        2021-02-24 12:53

        點(diǎn)擊上方藍(lán)色“小哈學(xué)Java”,選擇“設(shè)為星標(biāo)

        回復(fù)“資源”獲取獨(dú)家整理的學(xué)習(xí)資料!

        來源:https://www.cnblogs.com/lsgspace/p/10508180.html

        在SpringBoot中我們有時(shí)候需要讓項(xiàng)目在啟動時(shí)提前加載相應(yīng)的數(shù)據(jù)或者執(zhí)行某個(gè)方法,那么實(shí)現(xiàn)提前加載的方式有哪些呢?接下來我?guī)ьI(lǐng)大家逐個(gè)解答

        1.實(shí)現(xiàn)ServletContextAware接口并重寫其setServletContext方法

        @Component
        public?class?TestStarted?implements?ServletContextAware?{
        ????/**
        ?????*?在填充普通bean屬性之后但在初始化之前調(diào)用
        ?????*?類似于initializingbean的afterpropertiesset或自定義init方法的回調(diào)
        ?????*
        ?????*/

        ????@Override
        ????public?void?setServletContext(ServletContext?servletContext)?{
        ????????System.out.println("setServletContext方法");
        ????}
        }

        注意:該方法會在填充完普通Bean的屬性,但是還沒有進(jìn)行Bean的初始化之前執(zhí)行

        2.實(shí)現(xiàn)ServletContextListener接口

        /**
        ?*?在初始化Web應(yīng)用程序中的任何過濾器或servlet之前,將通知所有servletContextListener上下文初始化。
        ?*/

        @Override
        public?void?contextInitialized(ServletContextEvent?sce)?{
        ????//ServletContext?servletContext?=?sce.getServletContext();
        ????System.out.println("執(zhí)行contextInitialized方法");
        }

        3.將要執(zhí)行的方法所在的類交個(gè)spring容器掃描(@Component),并且在要執(zhí)行的方法上添加@PostConstruct注解或者靜態(tài)代碼塊執(zhí)行

        @Component
        public?class?Test2?{
        ????//靜態(tài)代碼塊會在依賴注入后自動執(zhí)行,并優(yōu)先執(zhí)行
        ????static{
        ????????System.out.println("---static--");
        ????}
        ????/**
        ?????*??@Postcontruct’在依賴注入完成后自動調(diào)用
        ?????*/

        ????@PostConstruct
        ????public?static?void?haha(){
        ????????System.out.println("@Postcontruct’在依賴注入完成后自動調(diào)用");
        ????}
        }

        4.實(shí)現(xiàn)ApplicationRunner接口

        /**
        ?*?用于指示bean包含在SpringApplication中時(shí)應(yīng)運(yùn)行的接口??梢远x多個(gè)applicationrunner bean
        ?*?在同一應(yīng)用程序上下文中,可以使用有序接口或@order注釋對其進(jìn)行排序。
        ?*/

        @Override
        public?void?run(ApplicationArguments?args)?throws?Exception?{
        ????System.out.println("ApplicationRunner的run方法");
        }

        4.實(shí)現(xiàn)CommandLineRunner接口

        /**
        ?*?用于指示bean包含在SpringApplication中時(shí)應(yīng)運(yùn)行的接口??梢栽谕粦?yīng)用程序上下文中定義多個(gè)commandlinerunner bean,并且可以使用有序接口或@order注釋對其進(jìn)行排序。
        ?*?如果需要訪問applicationArguments而不是原始字符串?dāng)?shù)組,請考慮使用applicationrunner。
        ?*?
        ?*/

        @Override
        public?void?run(String...?)?throws?Exception?{
        ????System.out.println("CommandLineRunner的run方法");
        }


        1.?天貓二面:內(nèi)存耗盡后 Redis 會發(fā)生什么?

        2.?程序員:如何寫出殺手級簡歷

        3.?對于注冊中心,ZooKeeper、Eureka 哪個(gè)更合適?

        4.?面試必問:Spring 循環(huán)依賴的三種方式 !

        最近面試BAT,整理一份面試資料Java面試BATJ通關(guān)手冊,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫、數(shù)據(jù)結(jié)構(gòu)等等。

        獲取方式:點(diǎn)“在看”,關(guān)注公眾號并回復(fù)?Java?領(lǐng)取,更多內(nèi)容陸續(xù)奉上。

        文章有幫助的話,在看,轉(zhuǎn)發(fā)吧。

        謝謝支持喲 (*^__^*)

        瀏覽 49
        點(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>
            亚洲乱伦视频网 | 小黄片视频在线观看 | 亚洲日韩毛片 | 开心久久激情五月 | 欧美最爽乱淫视频免费观看 | 青春草在线视频免费观看 | 午夜色综合 | 无遮挡一级毛片 | 国产成人啪免费观看 | 免费看91片 |