代碼片段: org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean if (!mbd.isSynthetic() && hasInstantiationAwareBeanPos ...
代碼片段:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; //如果返回為 false, 則會終止屬性註入 if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { return; } } } }
需要註意一個問題, 如果返回值是 false , 那麼此處就回結束屬性註入方法: populateBean
這裡遍歷了容器中的後置處理器, 然後調用滿足條件的 postProcessAfterInstantiation() 方法.
boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException
通過調試, 很明瞭的能發現, 有三個後置處理器能滿足條件, 調用先後順序為:
根據前面調用的 InstantiationAwareBeanPostProcessor 後置處理器, 大致可以推斷, 這裡估計啥也不會幹.
因為 spring 的流程還沒走完呢, 怎麼能終止呢!
ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor
這裡是由其父類來完成的:
org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessAfterInstantiation
@Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { return true; }
啥也沒乾
CommonAnnotationBeanPostProcessor
@Override public boolean postProcessAfterInstantiation(Object bean, String beanName) { return true; }
啥也沒乾
AutowiredAnnotationBeanPostProcessor
由其父類完成:
org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
@Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { return true; }
如我所想, 這裡啥也沒乾, 之所以這麼留著, 我估計是spring擴展用的