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

          @PostConstruct注解

          共 6257字,需瀏覽 13分鐘

           ·

          2021-06-12 02:18

          點擊上方藍色字體,選擇“標星公眾號”

          優(yōu)質文章,第一時間送達

          @PostConstruct基本:

          @PostConstruct注解好多人以為是Spring提供的。其實是Java自己的注解。

          Java中該注解的說明:@PostConstruct該注解被用來修飾一個非靜態(tài)的void()方法。被@PostConstruct修飾的方法會在服務器加載Servlet的時候運行,并且只會被服務器執(zhí)行一次。PostConstruct在構造函數(shù)之后執(zhí)行,init()方法之前執(zhí)行。

          通常我們會是在Spring框架中使用到@PostConstruct注解 該注解的方法在整個Bean初始化中的執(zhí)行順序:

          Constructor(構造方法) -> @Autowired(依賴注入) -> @PostConstruct(注釋的方法)

          應用:在靜態(tài)方法中調用依賴注入的Bean中的方法。

          package com.example.studySpringBoot.util;
           
          import com.example.studySpringBoot.service.MyMethorClassService;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.stereotype.Component;
           
          import javax.annotation.PostConstruct;
           
          @Component
          public class MyUtils {
           
              private static MyUtils          staticInstance = new MyUtils();
           
              @Autowired
              private MyMethorClassService    myService;
           
              @PostConstruct
              public void init(){
                  staticInstance.myService = myService;
              }
           
              public static Integer invokeBean(){
                  return staticInstance.myService.add(10,20);
              }
          }

          那么Java提供的@PostConstruct注解,Spring是如何實現(xiàn)的呢?

          需要先學習下BeanPostProcessor這個接口:

          public interface BeanPostProcessor {
           
           /**
               * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
            * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
            * or a custom init-method). The bean will already be populated with property values.
            * The returned bean instance may be a wrapper around the original.
               * 
               * 任何Bean實例化,并且Bean已經(jīng)populated(填充屬性) 就會回調這個方法
               *
               */
           Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
           
           /**
            * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
            * initialization callbacks (like InitializingBean'
          s {@code afterPropertiesSet}
            * or a custom init-method). The bean will already be populated with property values.
            * The returned bean instance may be a wrapper around the original.
               *
               * 任何Bean實例化,并且Bean已經(jīng)populated(填充屬性) 就會回調這個方法
               *
               */
           Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;


          那Spring初始化是那里回調這些方法呢?
          AbstractApplicationContext.finishBeanFactoryInitialization(...);
              beanFactory.preInstantiateSingletons();
                 DefaultListableBeanFactory.getBean(beanName);
                    AbstractBeanFactory.doGetBean();
                      AbstractAutowireCapableBeanFactory.createBean(....)
                          populateBean(beanName, mbd, instanceWrapper);
                          initializeBean(...)
                           //調用BeanPostProcessor.postProcessBeforeInitialization()方法
                            applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
                           //調用BeanPostProcessor.postProcessBeforeInitialization()方法
                            applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

          BeanPostProcessor有個實現(xiàn)類CommonAnnotationBeanPostProcessor,就是專門處理@PostConstruct  @PreDestroy注解。

          CommonAnnotationBeanPostProcessor的父類InitDestroyAnnotationBeanPostProcessor()
           InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization()
              InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata()
                  // 組裝生命周期元數(shù)據(jù)
                  InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata()
                      // 查找@PostConstruct注釋的方法
                      InitDestroyAnnotationBeanPostProcessor.initAnnotationType
                      // 查找@PreDestroy注釋方法
                      InitDestroyAnnotationBeanPostProcessor.destroyAnnotationType
           // 反射調用          
           metadata.invokeInitMethods(bean, beanName);    



          版權聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權協(xié)議,轉載請附上原文出處鏈接和本聲明。

          本文鏈接:

          https://blog.csdn.net/qq360694660/article/details/82877222











          瀏覽 82
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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片免费看 | 亚洲精品无码视频在线观看 | 五月天av导航 | 免费毛片在线 |