關(guān)于@PostConstruct、afterPropertiesSet和init-method的執(zhí)行順序
@PostConstruct、init-method、afterPropertiesSet() 執(zhí)行順序
想要知道 @PostConstruct、init-method、afterPropertiesSet() 的執(zhí)行順序,只要搞明白它們各自在什么時候被誰調(diào)用就行了。
程序版本:Spring Boot 2.3.5.RELEASE
準(zhǔn)備好要驗證的材料:
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 標(biāo)注的方法在何時被誰調(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ù)的構(gòu)造器,這個要傳入構(gòu)造器的參數(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行是在通過反射判斷方法上是否存在某個注解,如果是的話就加到一個集合中,在最后用于構(gòu)建 LifecycleMetadata 實例。
在這里因為我們要查找的是被賦值的內(nèi)容,所以在使用IDE進行查找時只要關(guān)注 write 相關(guān)的內(nèi)容就行了。
到這里,已經(jīng)知道了是在 CommonAnnotationBeanPostProcessor 的構(gòu)造器中進行 set 的,也就是當(dāng)創(chuàng)建 CommonAnnotationBeanPostProcessor 實例的時候就會進行賦值,并且 CommonAnnotationBeanPostProcessor 是 InitDestroyAnnotationBeanPostProcessor 的子類。
并且,CommonAnnotationBeanPostProcessor 構(gòu)造器中調(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 標(biāo)注,如果被標(biāo)注的話放到一個名字叫 currInitMethods 的集合中。
看到這里或許你已經(jīng)意識到了, @PostConstruct 可以標(biāo)注多個方法,并且因為反射獲取方法時是根據(jù)聲明順序的、 currInitMethods 是 ArrayList,兩者之間的順序是一樣的。
好了,被 @PostConstruct 標(biāo)注的方法已經(jīng)找到放到集合中了,將被用來構(gòu)建 LifecycleMetadata 實例了。
buildLifecycleMetadata(...) 方法返回一個 LifecycleMetadata 實例,這個返回值中包含傳入Class實例中得到的所有被 @PostConstruct 標(biāo)注的 Method 實例,接下來要看看是誰在調(diào)用 buildLifecycleMetadata(...) 方法,看看它是怎么用的?
追溯到 findLifecycleMetadata(...) 而 findLifecycleMetadata(...) 又有好幾處被調(diào)用。
查看最早得到的方法調(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 標(biāo)注的方法執(zhí)行已經(jīng)完成了。至于這個方法誰調(diào)用可以自己查看調(diào)用棧。
init-method、afterPropertiesSet() 的調(diào)用
細(xì)心的朋友可能已經(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,如果是的話會進行類型強轉(zhuǎn),然后調(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 標(biāo)注的方法,invokeInitMethods(...) 會執(zhí)行 afterPropertiesSet() 和自定義的初始化方法,并且 afterPropertiesSet() 在自定義的初始化方法之前執(zhí)行。
所以它們之間的執(zhí)行順序是:
@PostConstruct > afterPropertiesSet() > initMethod()
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Intellij IDEA中的Version Control問題
這篇文章主要介紹了Intellij IDEA中的Version Control問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11SpringSecurityOAuth2 如何自定義token信息
這篇文章主要介紹了SpringSecurityOAuth2 自定義token信息的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java8 LocalDateTime極簡時間日期操作小結(jié)
這篇文章主要介紹了Java8-LocalDateTime極簡時間日期操作整理,通過實例代碼給大家介紹了java8 LocalDateTime 格式化問題,需要的朋友可以參考下2020-04-04java 中序列化NotSerializableException問題解決辦法
這篇文章主要介紹了java 中序列化NotSerializableException問題解決辦法的相關(guān)資料,這里對序列化問題進行描述說明,并提供解決辦法,希望能幫助到大家,需要的朋友可以參考下2017-08-08Flyway詳解及Springboot集成Flyway的詳細(xì)教程
Flayway是一款數(shù)據(jù)庫版本控制管理工具,,支持?jǐn)?shù)據(jù)庫版本自動升級,Migrations可以寫成sql腳本,也可以寫在java代碼里。這篇文章主要介紹了Flyway詳解及Springboot集成Flyway的詳細(xì)教程的相關(guān)資料,需要的朋友可以參考下2020-07-07