Bean后置处理器 - InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation

代码片段:

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

通过调试, 很明了的能发现, 有三个后置处理器能满足条件, 调用先后顺序为:

1.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor

2.CommonAnnotationBeanPostProcessor

3.AutowiredAnnotationBeanPostProcessor

 

根据前面调用的 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扩展用的

 

posted @ 2020-07-27 21:24  Sniper_ZL  阅读(906)  评论(0编辑  收藏  举报