跳到主要内容

吃透Spring源码(十五):invokeBeanFactoryPostProcessors 执行流程

进入源码之前先来了解一下BeanFactoryPostProcessorBeanDefinitionRegistryPostProcessor

  • BeanFactoryPostProcessor:主要针对的操作对象是BeanFactory,主要是对BeanFactory的修改。
  • BeanDefinitionRegistryPostProcessor:是BeanFactoryPostProcessor的子接口,主要针对的操作对象是BeanDefinition
public interface BeanFactoryPostProcessor {



void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {



void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

}

refresh()方法

public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {


@Override
public void refresh() throws BeansException, IllegalStateException {


synchronized (this.startupShutdownMonitor) {


// Prepare this context for refreshing.
/**
* 前戏,做容器刷新前的准备工作
* 1、设置容器的启动时间
* 2、设置活跃状态为true
* 3、设置关闭状态为false
* 4、获取Environment对象,并加载当前系统的属性值到Environment对象中
* 5、准备监听器和事件的集合对象,默认为空的集合
*/

prepareRefresh();

// Tell the subclass to refresh the internal bean factory.
// 创建容器对象:DefaultListableBeanFactory
// 加载xml配置文件的属性值到当前工厂中,最重要的就是BeanDefinition
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.
// beanFactory的准备工作,对各种属性进行填充
prepareBeanFactory(beanFactory);

try {


// Allows post-processing of the bean factory in context subclasses.
// 子类覆盖方法做额外的处理,此处我们自己一般不做任何扩展工作,但是可以查看web中的代码,是有具体实现的
postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.
// 调用各种beanFactory处理器
invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.
// 注册bean处理器,这里只是注册功能,真正调用的是getBean方法
registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.
// 为上下文初始化message源,即不同语言的消息体,国际化处理,在springmvc的时候通过国际化的代码重点讲
initMessageSource();

// Initialize event multicaster for this context.
// 初始化事件监听多路广播器
initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.
// 留给子类来初始化其他的bean
onRefresh();

// Check for listener beans and register them.
// 在所有注册的bean中查找listener bean,注册到消息广播器中
registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.
// 初始化剩下的单实例(非懒加载的)
finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.
// 完成刷新过程,通知生命周期处理器lifecycleProcessor刷新过程,同时发出ContextRefreshEvent通知别人
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.
// 为防止bean资源占用,在异常处理中,销毁已经在前面过程中生成的单件bean
destroyBeans();

// Reset 'active' flag.
// 重置active标志
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();
}
}
}
}