<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 種方式!還有誰不會?

          共 5401字,需瀏覽 11分鐘

           ·

          2021-11-09 16:35

          作者 | chilx

          來源 | https://blog.csdn.net/showchi/article/details/97005720

          注意:調用者要被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 注釋用于在依賴關系注入完成之后需要執(zhí)行的方法上,以執(zhí)行任何初始化。此方法必須在將類放入服務之前調用。

          支持依賴關系注入的所有類都必須支持此注釋。即使類沒有請求注入任何資源,用 PostConstruct 注釋的方法也必須被調用。只有一個方法可以用此注釋進行注釋。

          如果您正在學習Spring Boot,推薦一個連載多年還在繼續(xù)更新的免費教程:http://blog.didispace.com/spring-boot-learning-2x/

          應用 PostConstruct 注釋的方法必須遵守以下所有標準:

          • 該方法不得有任何參數,除非是在 EJB 攔截器 (interceptor) 的情況下,根據 EJB 規(guī)范的定義,在這種情況下它將帶有一個 InvocationContext 對象 ;
          • 該方法的返回類型必須為 void;
          • 該方法不得拋出已檢查異常;
          • 應用 PostConstruct 的方法可以是 public、protected、package private 或 private;
          • 除了應用程序客戶端之外,該方法不能是 static;
          • 該方法可以是 final;
          • 如果該方法拋出未檢查異常,那么不得將類放入服務中,除非是能夠處理異常并可從中恢復的 EJB。
          • Spring Boot 學習筆記,這個太全了!

          方式二

          啟動類ApplicationContext

          實現方式:在springboot的啟動類中,定義static變量ApplicationContext,利用容器的getBean方法獲得依賴對象。

          如果您正在學習Spring Boot,推薦一個連載多年還在繼續(xù)更新的免費教程:http://blog.didispace.com/spring-boot-learning-2x/

          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.classargs);
              }
           
          }

          調用方式

          /**
           * @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);
              }
          }

          方式三

          手動注入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<Timplements ApplicationContextAware {
              private static ApplicationContext applicationContext;
              @Override
              public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
                  StaticMethodGetBean_3.applicationContext = applicationContext;
              }
           
              public static <T> T  getBean(Class<T> clazz) {
                  return applicationContext != null?applicationContext.getBean(clazz):null;
              }
          }

          另外,如果您正在學習Spring Cloud,推薦一個連載多年還在繼續(xù)更新的免費教程:https://blog.didispace.com/spring-cloud-learning/

          調用方式

          /**
           * 方式三
           */

          @Test
          public void method_3() {
              AutoMethodDemoService autoMethodDemoService = StaticMethodGetBean_3.getBean(AutoMethodDemoService.class);
              String test3 = autoMethodDemoService.test3();
              System.out.println(test3);
          }

          以上三種方式樓主都測試過可以為完美使用。

          (完)

          往期推薦

          為什么IDEA不推薦你使用@Autowired ?

          GitHub高贊,一款足以取代迅雷的開源下載工具

          炸裂!跑P站上教微積分,年入170w...

          網傳字節(jié)跳動、騰訊將執(zhí)行1075和965工作制?加班要審批才行!

          后端開掛:3行代碼 = 8個接口


          技術交流群

          最近有很多人問,有沒有讀者交流群,想知道怎么加入。加入方式很簡單,有興趣的同學,只需要點擊下方卡片,回復“加群,即可免費加入我們的高質量技術交流群!

          點擊閱讀原文,送你免費Spring Boot教程!

          瀏覽 74
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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片 | 婷婷国产| 日韩激情综合网 | 成人电影无码免费 |