Bean后置处理器 - InstantiationAwareBeanPostProcessor#applyBeanPostProcessorsBeforeInstantiation

在 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后续流程还是要走的

 

你可能感兴趣的:(Bean后置处理器 - InstantiationAwareBeanPostProcessor#applyBeanPostProcessorsBeforeInstantiation)