溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

@PostConstruct、afterPropertiesSet和init-method執(zhí)行順序是怎樣的

發(fā)布時間:2021-09-15 13:02:27 來源:億速云 閱讀:235 作者:小新 欄目:開發(fā)技術

這篇文章主要介紹@PostConstruct、afterPropertiesSet和init-method執(zhí)行順序是怎樣的,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

@PostConstruct、init-method、afterPropertiesSet() 執(zhí)行順序

想要知道 @PostConstruct、init-method、afterPropertiesSet() 的執(zhí)行順序,只要搞明白它們各自在什么時候被誰調(diào)用就行了。

程序版本:Spring Boot 2.3.5.RELEASE

準備好要驗證的材料:

public class Foo implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet()");
    }
    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct");
    }
    private void initMethod() {
        System.out.println("initMethod()");
    }
}
@Configuration
public class FooConfiguration {
    @Bean(initMethod = "initMethod")
    public Foo foo() {
        return new Foo();
    }
}

執(zhí)行啟動類,可以看到在控制臺中輸出:

@PostConstruct

afterPropertiesSet()

initMethod()

說明執(zhí)行順序是:@PostConstruct、afterPropertiesSet()、init-method

接下來將跟著源碼來了解為什么是這個順序。

@PostConstruct 標注的方法在何時被誰調(diào)用

首先,在 init() 中打個斷點,然后以 debug 的方式啟動項目,得到下面的調(diào)用棧:

init:23, Foo (com.xurk.init.foo)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
invoke:62, NativeMethodAccessorImpl (sun.reflect)
invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
invoke:498, Method (java.lang.reflect)
invoke:389, InitDestroyAnnotationBeanPostProcessor$LifecycleElement (org.springframework.beans.factory.annotation)
invokeInitMethods:333, InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata (org.springframework.beans.factory.annotation)
postProcessBeforeInitialization:157, InitDestroyAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
applyBeanPostProcessorsBeforeInitialization:415, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
initializeBean:1786, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:594, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:516, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:324, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, 2103763750 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$169)
getSingleton:234, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:322, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:202, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:897, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:879, AbstractApplicationContext (org.springframework.context.support)
refresh:551, AbstractApplicationContext (org.springframework.context.support)
refresh:143, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)
refresh:758, SpringApplication (org.springframework.boot)
refresh:750, SpringApplication (org.springframework.boot)
refreshContext:405, SpringApplication (org.springframework.boot)
run:315, SpringApplication (org.springframework.boot)
run:1237, SpringApplication (org.springframework.boot)
run:1226, SpringApplication (org.springframework.boot)
main:14, InitApplication (com.xurk.init)

從上往下看,跳過使用 sun.reflect 的方法,進入到第6行。

public void invoke(Object target) throws Throwable {
   ReflectionUtils.makeAccessible(this.method);
   this.method.invoke(target, (Object[]) null);
}

很明顯,這里是在通過反射調(diào)用某個對象的一個方法,并且這個“某個對象”就是我們定義的 Foo的實例對象了。

那么這里的 method 又是在什么時候進行賦值的呢?

invoke(...) 全路徑是:

org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.LifecycleElement#invoke

并且 LifecycleElement 有且只有一個顯示聲明并且?guī)?shù)的構造器,這個要傳入構造器的參數(shù)正是 invoke(...) 使用的那個 Method 對象。

接下來就是查一下,是誰在 new LifecycleElement 。

于是定位到:

org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor#buildLifecycleMetadata

private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
    if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(this.initAnnotationType, this.destroyAnnotationType))) {
        return this.emptyLifecycleMetadata;
    }
    List<LifecycleElement> initMethods = new ArrayList<>();
    List<LifecycleElement> destroyMethods = new ArrayList<>();
    Class<?> targetClass = clazz;
    do {
        final List<LifecycleElement> currInitMethods = new ArrayList<>();
        final List<LifecycleElement> currDestroyMethods = new ArrayList<>();
        ReflectionUtils.doWithLocalMethods(targetClass, method -> {
            if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {
                LifecycleElement element = new LifecycleElement(method);
                currInitMethods.add(element);
                if (logger.isTraceEnabled()) {
                    logger.trace("Found init method on class [" + clazz.getName() + "]: " + method);
                }
            }
            if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) {
                currDestroyMethods.add(new LifecycleElement(method));
                if (logger.isTraceEnabled()) {
                    logger.trace("Found destroy method on class [" + clazz.getName() + "]: " + method);
                }
            }
        });
        initMethods.addAll(0, currInitMethods);
        destroyMethods.addAll(currDestroyMethods);
        targetClass = targetClass.getSuperclass();
    }
    while (targetClass != null && targetClass != Object.class);
    return (initMethods.isEmpty() && destroyMethods.isEmpty() ? this.emptyLifecycleMetadata :
            new LifecycleMetadata(clazz, initMethods, destroyMethods));
}

第15-26行是在通過反射判斷方法上是否存在某個注解,如果是的話就加到一個集合中,在最后用于構建 LifecycleMetadata 實例。

在這里因為我們要查找的是被賦值的內(nèi)容,所以在使用IDE進行查找時只要關注 write 相關的內(nèi)容就行了。

@PostConstruct、afterPropertiesSet和init-method執(zhí)行順序是怎樣的

到這里,已經(jīng)知道了是在 CommonAnnotationBeanPostProcessor 的構造器中進行 set 的,也就是當創(chuàng)建 CommonAnnotationBeanPostProcessor 實例的時候就會進行賦值,并且 CommonAnnotationBeanPostProcessor 是 InitDestroyAnnotationBeanPostProcessor 的子類。

@PostConstruct、afterPropertiesSet和init-method執(zhí)行順序是怎樣的

并且,CommonAnnotationBeanPostProcessor 構造器中調(diào)用的 setInitAnnotationType 其實是它父類的方法,實際是對 InitDestroyAnnotationBeanPostProcessor 的實例的 initAnnotationType 字段進行賦值。

到這里已經(jīng)可以明確 buildLifecycleMetadata(...) 中判斷的正是 @PostConstruct。

再回到buildLifecycleMetadata(...) ,查看其使用的 doWithLocalMethods(...) 的實現(xiàn)。

public static void doWithLocalMethods(Class<?> clazz, MethodCallback mc) {
    Method[] methods = getDeclaredMethods(clazz, false);
    for (Method method : methods) {
        try {
            mc.doWith(method);
        }
        catch (IllegalAccessException ex) {
            throw new IllegalStateException("Not allowed to access method '" + method.getName() + "': " + ex);
        }
    }
}

很簡單,通過反射獲得類的所有方法,然后調(diào)用一個函數(shù)接口的方法,這個函數(shù)接口的實現(xiàn)就是判斷方法是不是被 @PostConstruct 標注,如果被標注的話放到一個名字叫 currInitMethods 的集合中。

看到這里或許你已經(jīng)意識到了, @PostConstruct可以標注多個方法,并且因為反射獲取方法時是根據(jù)聲明順序的、 currInitMethods 是 ArrayList,兩者之間的順序是一樣的。

好了,被 @PostConstruct 標注的方法已經(jīng)找到放到集合中了,將被用來構建 LifecycleMetadata 實例了。

buildLifecycleMetadata(...) 方法返回一個 LifecycleMetadata 實例,這個返回值中包含傳入Class實例中得到的所有被 @PostConstruct 標注的 Method 實例,接下來要看看是誰在調(diào)用 buildLifecycleMetadata(...) 方法,看看它是怎么用的?

追溯到 findLifecycleMetadata(...) 而 findLifecycleMetadata(...) 又有好幾處被調(diào)用。

@PostConstruct、afterPropertiesSet和init-method執(zhí)行順序是怎樣的

查看最早得到的方法調(diào)用棧,查到postProcessBeforeInitializatio(...) ,它又是再被誰調(diào)用?

init:23, Foo (com.xurk.init.foo)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
invoke:62, NativeMethodAccessorImpl (sun.reflect)
invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
invoke:498, Method (java.lang.reflect)
invoke:389, InitDestroyAnnotationBeanPostProcessor$LifecycleElement (org.springframework.beans.factory.annotation)
invokeInitMethods:333, InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata (org.springframework.beans.factory.annotation)
postProcessBeforeInitialization:157, InitDestroyAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
applyBeanPostProcessorsBeforeInitialization:415, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
initializeBean:1786, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)

跟著方法調(diào)用棧,我們來到

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization

@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
      throws BeansException {
   Object result = existingBean;
   for (BeanPostProcessor processor : getBeanPostProcessors()) {
      Object current = processor.postProcessBeforeInitialization(result, beanName);
      if (current == null) {
         return result;
      }
      result = current;
   }
   return result;
}

在這個方法,遍歷 BeanPostProcessors 集合并執(zhí)行每個 BeanPostProcessor 的 postProcessBeforeInitialization(...) 方法。

**那么 getBeanPostProcessors() 中的內(nèi)容又是在什么時候放進去的呢?都有哪些內(nèi)容?**可以通過 AnnotationConfigUtils和 CommonAnnotationBeanPostProcessor 查找,這里就不再贅述了。

查找 applyBeanPostProcessorsBeforeInitialization(...) 的調(diào)用者,來到

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean

protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
   if (System.getSecurityManager() != null) {
      AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
         invokeAwareMethods(beanName, bean);
         return null;
      }, getAccessControlContext());
   }
   else {
      invokeAwareMethods(beanName, bean);
   }
   Object wrappedBean = bean;
   if (mbd == null || !mbd.isSynthetic()) {
      wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
   }
   try {
      invokeInitMethods(beanName, wrappedBean, mbd);
   }
   catch (Throwable ex) {
      throw new BeanCreationException(
            (mbd != null ? mbd.getResourceDescription() : null),
            beanName, "Invocation of init method failed", ex);
   }
   if (mbd == null || !mbd.isSynthetic()) {
      wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
   }
   return wrappedBean;
}

顧名思義,在這個方法中對 Bean 進行初始化。是在被注入前一定要經(jīng)過的過程,到這里被 @PostConstruct 標注的方法執(zhí)行已經(jīng)完成了。至于這個方法誰調(diào)用可以自己查看調(diào)用棧。

init-method、afterPropertiesSet() 的調(diào)用

細心的朋友可能已經(jīng)發(fā)現(xiàn)了在 initializeBean(...) 中有調(diào)用到一個叫做 invokeInitMethods(...) 的方法。

protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
      throws Throwable {
   boolean isInitializingBean = (bean instanceof InitializingBean);
   if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
      if (logger.isTraceEnabled()) {
         logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
      }
      if (System.getSecurityManager() != null) {
         try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
               ((InitializingBean) bean).afterPropertiesSet();
               return null;
            }, getAccessControlContext());
         }
         catch (PrivilegedActionException pae) {
            throw pae.getException();
         }
      }
      else {
         ((InitializingBean) bean).afterPropertiesSet();
      }
   }
   if (mbd != null && bean.getClass() != NullBean.class) {
      String initMethodName = mbd.getInitMethodName();
      if (StringUtils.hasLength(initMethodName) &&
            !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
            !mbd.isExternallyManagedInitMethod(initMethodName)) {
         invokeCustomInitMethod(beanName, bean, mbd);
      }
   }
}

BeanDefinition是Bean定義的一個抽象。類似于在Java中存在Class類用于描述一個類,里面有你定義的 Bean 的各種信息。

在第4行,判斷是不是 InitializingBean,如果是的話會進行類型強轉,然后調(diào)用 afterPropertiesSet()。

在第26行,獲得到自定義初始化方法的名字,然后在第30行調(diào)用 invokeCustomInitMethod 執(zhí)行完成。

順序的確定

protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
   if (System.getSecurityManager() != null) {
      AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
         invokeAwareMethods(beanName, bean);
         return null;
      }, getAccessControlContext());
   }
   else {
      invokeAwareMethods(beanName, bean);
   }
   Object wrappedBean = bean;
   if (mbd == null || !mbd.isSynthetic()) {
      wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
   }
   try {
      invokeInitMethods(beanName, wrappedBean, mbd);
   }
   catch (Throwable ex) {
      throw new BeanCreationException(
            (mbd != null ? mbd.getResourceDescription() : null),
            beanName, "Invocation of init method failed", ex);
   }
   if (mbd == null || !mbd.isSynthetic()) {
      wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
   }
   return wrappedBean;
}

initializeBean(...) 方法中,先執(zhí)行 applyBeanPostProcessorsBeforeInitialization(...) 在執(zhí)行 invokeInitMethods(...) 。

而 applyBeanPostProcessorsBeforeInitialization(...) 會執(zhí)行被 @PostConstruct 標注的方法,invokeInitMethods(...) 會執(zhí)行 afterPropertiesSet() 和自定義的初始化方法,并且 afterPropertiesSet() 在自定義的初始化方法之前執(zhí)行。

所以它們之間的執(zhí)行順序是:

@PostConstruct > afterPropertiesSet() > initMethod()

以上是“@PostConstruct、afterPropertiesSet和init-method執(zhí)行順序是怎樣的”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI