spring源码之BeanPostProcessor
工厂式的钩子方法,用来对于一个新的实例化之后的bean进行自定义的修改(
custom modification
),比如,校验标记接口(如Serializable
), 用代理类来封闭这个bean。spring内部的Aop实现也是通过BeanPostProcessor实现的
BeanPostProcessor源码
public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
postProcessBeforeInitialization方法
这个方法是在这个实例的bean对象还没有被初始化之前被调用的,如(是在InitializingBean接口的afterPropertiesSet方法和自定义 init-method
方法之前)被调用的,这里bean已经是被注入了属性值,而通过这个方法之后返回的bean应该是经过包装之后的bean(相对于原来的bean)
在AbstractAutowireCapableBeanFactory
类的方法invokeInitMethods
中:
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 {
//这个方法中就是调用InitializingBean.afterPropertiesSet方法
//和调用 init-method的实现地方!!
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
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()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
result = processor.postProcessBeforeInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}
postProcessAfterInitialization方法
这个方法是在这个实例的bean对象还没有被初始化之后被调用的,通常是在InitializingBean接口的afterPropertiesSet方法和自定义 init-method
方法之后被调用。这个方法即使是在InstantiationAwareBeanPostProcessor
的postProcessBeforeInstantiation
方法被短路(即返回一个非non-null的对象)也会被调用,这也是和其他BeanPostProcessor
中的回调方法不一样的地方
在AbstractAutowireCapableBeanFactory
类的方法resolveBeforeInstantiation
中:
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) {
//即使是在`InstantiationAwareBeanPostProcessor`的`postProcessBeforeInstantiation·方法被短路
//postProcessAfterInitialization方法也会被执行!!!
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
}
}
mbd.beforeInstantiationResolved = (bean != null);
}
return bean;
}
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
result = processor.postProcessAfterInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}