在 createBean 方法中, doCreateBean 方法前, 呼叫了這樣一句代碼:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. // 在 bean實體化之前 應用后置處理,如果后置處理回傳的bean不為空,則直接回傳 // 回傳的是一個寡婦物件, 屬性什么的, spring不會去維護 // spring不推薦開發人員使用這個介面 InstantiationAwareBeanPostProcessor Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null) { return bean; }
這是一個擴展方法, 如果回傳了 bean , 那么就不走后面的創建流程了.
要注意這里的執行時機: 物件實體化之前執行
@Nullable protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) { Object bean = null; if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) { // Make sure bean class is actually resolved at this point. if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { Class<?> targetType = determineTargetType(beanName, mbd); if (targetType != null) { bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName); if (bean != null) { bean = applyBeanPostProcessorsAfterInitialization(bean, beanName); } } } mbd.beforeInstantiationResolved = (bean != null); } return bean; }
InstantiationAwareBeanPostProcessor 這個后置處理器, 不但定義了 實體化之前的處理器, 還定義了 實體化之后的處理器.
所以, 這里如果回傳的bean不為null, 還需要執行實體化之后的處理器, 來保證流程的完整性.
在這里, 就不看實體化之后的回呼了.
直接看 applyBeanPostProcessorsBeforeInstantiation() 方法:
@Nullable protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName); if (result != null) { return result; } } } return null; }
首先來看一下, 這個介面:
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor { @Nullable default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { return null; } default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { return true; } @Nullable default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException { return null; } @Deprecated @Nullable default PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { return pvs; } }
注意到, 這個介面是 繼承了 BeanPostProcessor , 表示他是一個 bean 的后置處理器. 和前面章節出現過的 BeanFactory 后置處理器 是不同的
通過斷點除錯, 不難發現, 6個后置處理器中, 滿足條件的就三個, 按照執行順序排列為:
1.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor
2.CommonAnnotationBeanPostProcessor
3.AutowiredAnnotationBeanPostProcessor
那接下來, 就需要看一下, 里面分別干了什么
ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor
org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessBeforeInstantiation
@Override @Nullable public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { return null; }
從這里看, 他本身都沒有實作或重寫這個方法, 而是由它的父類去實作的.
里面啥也沒干, 直接回傳了 null.
CommonAnnotationBeanPostProcessor
@Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) { return null; }
啥也沒干
AutowiredAnnotationBeanPostProcessor
org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessBeforeInstantiation
@Override @Nullable public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { return null; }
他本身也沒有實作這個方法, 而是父類去實作的.
里面也是啥也沒干.
到這里, 這個后置處理器就執行完了, 大家都沒干事情, 都是直接回傳的 null. 所以, spring后續流程還是要走的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/131434.html
標籤:Java
