<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 獲取 bean 的 3 種方式!還有誰(shuí)不會(huì)??

          共 3681字,需瀏覽 8分鐘

           ·

          2021-11-07 06:08

          關(guān)注我們,設(shè)為星標(biāo),每天7:30不見(jiàn)不散,架構(gòu)路上與您共享?

          回復(fù)"架構(gòu)師"獲取資源


          大家好,我是架構(gòu)君,一個(gè)會(huì)寫(xiě)代碼吟詩(shī)的架構(gòu)師。


          注意:調(diào)用者要被spring管理

          方式一

          注解@PostConstruct

          import?com.example.javautilsproject.service.AutoMethodDemoService;
          import?org.springframework.beans.factory.annotation.Autowired;
          import?org.springframework.stereotype.Component;
          ?
          import?javax.annotation.PostConstruct;
          ?
          /**
          ?*?springboot靜態(tài)方法獲取?bean?的三種方式(一)
          ?*?@author:?clx
          ?*?@version:?1.1.0
          ?*/
          @Component
          public?class?StaticMethodGetBean_1?{
          ?
          ????@Autowired
          ????private?AutoMethodDemoService?autoMethodDemoService;
          ?
          ????@Autowired
          ????private?static?AutoMethodDemoService?staticAutoMethodDemoService;
          ?
          ????@PostConstruct
          ????public?void?init()?{
          ????????staticAutoMethodDemoService?=?autoMethodDemoService;
          ????}
          ?
          ????public?static?String?getAuthorizer()?{
          ????????return?staticAutoMethodDemoService.test();
          ????}
          }

          PostConstruct 注釋用于在依賴關(guān)系注入完成之后需要執(zhí)行的方法上,以執(zhí)行任何初始化。此方法必須在將類放入服務(wù)之前調(diào)用。

          支持依賴關(guān)系注入的所有類都必須支持此注釋。即使類沒(méi)有請(qǐng)求注入任何資源,用 PostConstruct 注釋的方法也必須被調(diào)用。只有一個(gè)方法可以用此注釋進(jìn)行注釋。

          應(yīng)用 PostConstruct 注釋的方法必須遵守以下所有標(biāo)準(zhǔn):

          方式二

          啟動(dòng)類ApplicationContext

          實(shí)現(xiàn)方式:在springboot的啟動(dòng)類中,定義static變量ApplicationContext,利用容器的getBean方法獲得依賴對(duì)象。推薦一個(gè) Spring Boot 基礎(chǔ)教程及實(shí)戰(zhàn)示例:https://github.com/javastacks/javastack

          import?org.springframework.boot.SpringApplication;
          import?org.springframework.boot.autoconfigure.SpringBootApplication;
          import?org.springframework.context.ConfigurableApplicationContext;
          /**
          ?*?@author:?clx
          ?*?@version:?1.1.0
          ?*/
          @SpringBootApplication
          public?class?Application?{
          ????public?static?ConfigurableApplicationContext?ac;
          ????public?static?void?main(String[]?args)?{
          ???????ac?=?SpringApplication.run(Application.class,?args);
          ????}
          ?
          }

          調(diào)用方式

          /**
          ?*?@author:?clx
          ?*?@version:?1.1.0
          ?*/
          @RestController
          public?class?TestController?{
          ????/**
          ?????*?方式二
          ?????*/
          ????@GetMapping("test2")
          ????public?void?method_2()?{
          ????????AutoMethodDemoService?methodDemoService?=?Application.ac.getBean(AutoMethodDemoService.class);
          ????????String?test2?=?methodDemoService.test2();
          ????????System.out.println(test2);
          ????}
          }

          方式三

          手動(dòng)注入ApplicationContext

          import?org.springframework.beans.BeansException;
          import?org.springframework.context.ApplicationContext;
          import?org.springframework.context.ApplicationContextAware;
          import?org.springframework.stereotype.Component;
          ?
          ?
          /**
          ?*?springboot靜態(tài)方法獲取?bean?的三種方式(三)
          ?*?@author:?clx
          ?*?@version:?1.1.0
          ?*/
          @Component
          public?class?StaticMethodGetBean_3?implements?ApplicationContextAware?{
          ????private?static?ApplicationContext?applicationContext;
          ????@Override
          ????public?void?setApplicationContext(ApplicationContext?applicationContext)?throws?BeansException?{
          ????????StaticMethodGetBean_3.applicationContext?=?applicationContext;
          ????}
          ?
          ????public?static??T??getBean(Class?clazz)?{
          ????????return?applicationContext?!=?null?applicationContext.getBean(clazz):null;
          ????}
          }

          調(diào)用方式

          /**
          ?*?方式三
          ?*/
          @Test
          public?void?method_3()?{
          ????AutoMethodDemoService?autoMethodDemoService?=?StaticMethodGetBean_3.getBean(AutoMethodDemoService.class);
          ????String?test3?=?autoMethodDemoService.test3();
          ????System.out.println(test3);
          }

          以上三種方式樓主都測(cè)試過(guò)可以為完美使用。


          封面自取



          文章來(lái)源:https://blog.csdn.net/showchi/article/details/97005720


          到此文章就結(jié)束了。如果今天的文章對(duì)你在進(jìn)階架構(gòu)師的路上有新的啟發(fā)和進(jìn)步,歡迎轉(zhuǎn)發(fā)給更多人。歡迎加入架構(gòu)師社區(qū)技術(shù)交流群,眾多大咖帶你進(jìn)階架構(gòu)師,在后臺(tái)回復(fù)“加群”即可入群。



          這些年小編給你分享過(guò)的干貨


          1.優(yōu)質(zhì)SpringBoot物流管理項(xiàng)目(附源碼)

          2.優(yōu)質(zhì)ERP系統(tǒng)帶進(jìn)銷存財(cái)務(wù)生產(chǎn)功能(附源碼)

          3.優(yōu)質(zhì)SpringBoot帶工作流管理項(xiàng)目(附源碼)

          4.最好用的OA系統(tǒng),拿來(lái)即用(附源碼)

          5.SBoot+Vue外賣系統(tǒng)前后端都有(附源碼

          6.SBoot+Vue可視化大屏拖拽項(xiàng)目(附源碼)



          轉(zhuǎn)發(fā)在看就是最大的支持??

          瀏覽 50
          點(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>
                  大香蕉日逼视频 | 免费日女人网站 | 先锋女人资源 | 好想操骚逼无码视频 | 日本特黄一级片 |