源码版本4.3.10-RELEASE
我们都知道,实现BeanPostProcessor接口,在bean初始化前后,spring会分别回调postProcessBeforeInitialization和postProcessAfterInitialization。目的是保留扩展接口修改bean的属性,甚至可以替换bean:
public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
那spring究竟是如何做到的呢?我们一起来看看吧。
从spring容器启动开始看起:new ClassPathXmlApplicationContext("classpath:application.xml");
看AbstractApplicationContext.refresh();方法。
1、在obtainFreshBeanFactory里会解析xml配置,把所有bean注册到beanDefinitionNames,包含我们自定义的BeanPostProcessor。
2、先看prepareBeanFactory:
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
}
这里添加了三个spring默认的BeanPostProcessor。
3、再看postProcessBeanFactory:
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
}
这个是AbstractRefreshableWebApplicationContext(AbstractApplicationContext的子类)的方法,它同样也添加了一个默认的BeanPostProcessor。
4、接着看invokeBeanFactoryPostProcessors:
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}
这里设置一下TempClassLoader,并重新添加LoadTimeWeaverAwareProcessor,后面会单独开个文章分析内建的这些类的作用。
5、继续看registerBeanPostProcessors:
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
registerBeanPostProcessors会继续添加一个内建Processor:
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
然后从容器取出所有Processor(包括我们自定义的)进行排序,再添加回beanFactory(先移除后添加),最后重新把ApplicationListenerDetector添加到末尾:
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
6、最后看一下调用过程吧,finishBeanFactoryInitialization:
在这里beanFactory.preInstantiateSingletons();(在子类DefaultListableBeanFactory里实现)
再到其父类的AbstractBeanFactory.doGetBean->AbstractAutowireCapableBeanFactory.createBean->resolveBeforeInstantiation
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
//省略部分代码
if (targetType != null) {
bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
}
}
mbd.beforeInstantiationResolved = (bean != null);
}
return bean;
}
就是在这里回调接口的2个方法,其实这里允许直接返回bean了。继续看resolveBeforeInstantiation后面的doCreateBean,同样会在applyMergedBeanDefinitionPostProcessors里回调,不过只是针对某一类型的Processor:
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof MergedBeanDefinitionPostProcessor) {
MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
}
}
}
doCreateBean的另一个方法initializeBean里也会做回调:
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
//省略部分代码
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
//省略部分代码
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
执行完createBean后,再回到其父类的AbstractBeanFactory.getObjectForBeanInstance(该方法就是处理FactoryBean类型的bean)
调到其父类FactoryBeanRegistrySupport.getObjectFromFactoryBean
到AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(在doGetObjectFromFactoryBean后执行,该方法正是初始化bean的过程)->applyBeanPostProcessorsAfterInitialization
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
result = beanProcessor.postProcessAfterInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}
至此,完.
网友评论