上一篇博客spring bean初始化扩展之Aware源码解析,介绍了Aware的用处,其作为spring的一个扩展点。这篇博客将介绍spring的另一个扩展点BeanPostProcessor。先看下BeanPostProcessor源码
public interface BeanPostProcessor {
/**
* Apply this BeanPostProcessor to the given new bean instance before any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* Apply this BeanPostProcessor to the given new bean instance after any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* In case of a FactoryBean, this callback will be invoked for both the FactoryBean
* instance and the objects created by the FactoryBean (as of Spring 2.0). The
* post-processor can decide whether to apply to either the FactoryBean or created
* objects or both through corresponding {@code bean instanceof FactoryBean} checks.
*
This callback will also be invoked after a short-circuiting triggered by a
* {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
* in contrast to all other BeanPostProcessor callbacks.
*
The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.FactoryBean
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
可以看到BeanPostProcessor接口定义了两个方法,一个是before,一个是after,入参都是bean和beanName。
继续通过AbstractAutowireCapableBeanFactory中初始化bean的源码中,了解一下BeanPostProcessor的作用执行时机。
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
//调用调用此方法进行部分Aware接口的回调
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;
}
可以看到在bean初始化的过程中,在调用bean自定义初始化方法的前后,分别执行applyBeanPostProcessorsBeforeInitialization 和 applyBeanPostProcessorsAfterInitialization 两个方法。我们看一下这两个方法。
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
可以看到这两个方法都是分别拿到所有的BeanPostProcessor,然后遍历执行相应的before和after方法。因此可以总结,BeanPostProcessor的作用其实就是在普通bean初始化时,调用bean的自定义或非自定义初始化方法的前后进行相应的操作,以达到对bean进行修改扩展。
通过以上的介绍,我们知道BeanPostProcessor的作用,那么这些BeanPostProcessor是什么时候被注册上去的呢?上面源码有通过getBeanPostProcessors方法获取所有的BeanPostProcessor。这里getBeanPostProcessors其实是调用AbstractAutowireCapableBeanFactory的父类AbstractBeanFactory的getBeanPostProcessors方法
/**
* Return the list of BeanPostProcessors that will get applied
* to beans created with this factory.
*/
public List<BeanPostProcessor> getBeanPostProcessors() {
return this.beanPostProcessors;
}
可以看到这里直接返回一个list。而这个list是AbstractBeanFactory的一个成员变量,也就是肯定有入口add相应的beanPostProcessor进入这个list。而这个地方就是AbstractBeanFactory的addBeanPostProcessor方法。
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
// Remove from old position, if any
this.beanPostProcessors.remove(beanPostProcessor);
// Track whether it is instantiation/destruction aware
if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
this.hasInstantiationAwareBeanPostProcessors = true;
}
if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
this.hasDestructionAwareBeanPostProcessors = true;
}
// Add to end of list
this.beanPostProcessors.add(beanPostProcessor);
}
那么spring容器在什么时候将beanPostProcessor添加进这个list呢。这里有两种方式,一种是普通的容器beanFactory的显式add,另一种是ApplicationContext的自动注册。
普通的 BeanFactory 是不会自动注册 BeanPostProcessor 的,需要我们手动调用 addBeanPostProcessor() 进行注册,注册后的 BeanPostProcessor 才能适用于所有该 BeanFactory 创建的 bean
ClassPathResource resource = new ClassPathResource("spring.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
//MyBeanPostProcessor是我自定义的一个BeanPostProcessor,后面示例会有源码
MyBeanPostProcessor myBeanPostProcessor = new MyBeanPostProcessor();
//显式调用,将myBeanPostProcessor添加到beanFactory中。
factory.addBeanPostProcessor(myBeanPostProcessor);
ApplicationContext 容器可以自动检测所有的 BeanPostProcessor 并自动完成注册,而不需要手动添加注册。
这个自动注册过程其实在之前讲解aop源码的博客中有讲过。细节分析可以阅读这篇博客基于注解式的SpringAOP源码解析-3。这里再贴一下源码。
AbstractApplicationContext的refresh方法
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
//这一步就是注册beanPostProcessor
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
可以看到容器在初始化的过程中,调用了registerBeanPostProcessors方法。这个方法就是自动注册BeanPostProcessor的过程。这个过程可以前面说的博客中了解。
自定义BeanPostProcessor
/**
* 后置处理器:初始化前后进行处理
*
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
*
* @param bean bean刚创建的实例
* @param beanName 实例的beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
return bean;
}
}
配置扫描让自定义MyBeanPostProcessor被spring容器管理。
@ComponentScan("spring.annotation.pobean.lifecycle")
@Configuration
public class MainConfigOfLifeCycle {
}
编写单元测试,创建容器
public class IOCTest_LifeCycle {
@Test
public void test01(){
//1、创建ioc容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
System.out.println("容器创建完成...");
//关闭容器
applicationContext.close();
}
}