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

          SpringBoot靜態(tài)方法獲取 bean的三種方式

          共 3522字,需瀏覽 8分鐘

           ·

          2021-11-19 20:11

          點(diǎn)擊關(guān)注公眾號(hào),Java干貨及時(shí)送達(dá)

          牛逼!又發(fā)現(xiàn)了一款面試題庫(kù),太全了!!

          點(diǎn)擊查看


          方式一? 注解@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
          ?*?@date:?2019/7/23
          ?*?@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說(shuō)明

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

          • 該方法不得有任何參數(shù),除非是在 EJB 攔截器 (interceptor) 的情況下,根據(jù) EJB 規(guī)范的定義,在這種情況下它將帶有一個(gè) InvocationContext 對(duì)象 ;

          • 該方法的返回類型必須為 void;

          • 該方法不得拋出已檢查異常;

          • 應(yīng)用 PostConstruct 的方法可以是 public、protected、package private 或 private;

          • 除了應(yīng)用程序客戶端之外,該方法不能是 static;

          • 該方法可以是 final;

          • 如果該方法拋出未檢查異常,那么不得將類放入服務(wù)中,除非是能夠處理異常并可從中恢復(fù)的 EJB。


          方式二? 啟動(dòng)類ApplicationContext

          實(shí)現(xiàn)方式:在springboot的啟動(dòng)類中,定義static變量ApplicationContext,利用容器的getBean方法獲得依賴對(duì)象

          ?
          import?org.springframework.boot.SpringApplication;
          import?org.springframework.boot.autoconfigure.SpringBootApplication;
          import?org.springframework.context.ConfigurableApplicationContext;
          /**
          ?*?@author:?clx
          ?*?@date:?2019/7/23
          ?*?@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
          ?*?@date:?2019/7/23
          ?*?@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

          手動(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
          ?*?@date:?2019/7/23
          ?*?@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ò)可以為完美使用



          ? 作者?|??chilx

          來(lái)源 |??csdn.net/showchi/article/details/97005720


          如有文章對(duì)你有幫助,

          歡迎關(guān)注??、點(diǎn)贊??、轉(zhuǎn)發(fā)??!



          推薦,?Java面試題庫(kù),詳情點(diǎn)擊:
          牛逼!又發(fā)現(xiàn)了一款牛逼的Java面試題庫(kù),史上最強(qiáng)!
          瀏覽 87
          點(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>
                  午夜毛片毛片 | 天堂草原电视剧在线观看图片高清 | 艹逼一区 | 国产精品欧美一级 | 天堂a 资源 |