Spring框架BeanPostProcessor解读

按照Spring bean的生命周期,先读取BeanDefinition,然后是实例化,之后是初始化,而BeanPostProcessor就是作用在实例化阶段之后,围绕着初始化阶段。

1
2
3
4
5
6
public interface BeanPostProcessor {
    //在初始化阶段开始前执行
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
    //在初始化阶段之后执行
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

Spring bean生命周期的图片如下,BPP环绕在Initializer周围.
Spring bean生命周期

源码解读

我们从BeanFactorygetBean方法开始分析源码,之后执行到AbstractAutowireCapableBeanFactorydoCreateBean方法
doCreateBean代码片段摘录如下

1
2
3
4
5
6
7
8
Object exposedObject = bean;
try {
    populateBean(beanName, mbd, instanceWrapper);
    if (exposedObject != null) {
        //在这里执行bean的初始化
        exposedObject = initializeBean(beanName, exposedObject, mbd);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//bean初始化代码整段摘录,可以只看有注释的地方
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
    if (System.getSecurityManager() != null) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                invokeAwareMethods(beanName, bean);
                return null;
            }
        }, getAccessControlContext());
    }
    else {
        invokeAwareMethods(beanName, bean);
    }

    Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {
        //这里执行了BeanPostProcessor的postProcessBeforeInitialization方法
        wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    }

    try {
        //这里执行bean的初始化
        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()) {
        //初始化执行继续执行BeanPostProcessor的postProcessAfterInitialization方法
        wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    }
    return wrappedBean;
}

参考:

  1. 《Spring技术内幕第二版》
  2. https://stackoverflow.com/questions/29743320/how-exactly-works-the-spring-bean-post-processor