转载自:https://www.jianshu.com/p/5323114c0d05
描述
BeanFactoryPostProcessor 为spring在容器初始化时对外对外暴露的扩展点,Spring IoC容器允许BeanFactoryPostProcessor在容器加载注册BeanDefinition完成之后读取BeanDefinition(配置元数据),并可以修改它。
下面跟踪spring源码阅读BeanFactoryPostProcessor的加载机制以及执行时机
执行入口
// AbstractApplicationContext 类
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
//容器刷新前的处理方法
prepareRefresh();
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//使用容器前beanFactory的前置处理方法
prepareBeanFactory(beanFactory);
try {
postProcessBeanFactory(beanFactory);
//在容器中执行bean工厂后置处理器
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
initMessageSource();
initApplicationEventMulticaster();
onRefresh();
registerListeners();
finishBeanFactoryInitialization(beanFactory);
finishRefresh();
}
catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
resetCommonCaches();
}
}
}
spring IOC容器ApplicationContext 在完成包扫描,xml配置加载(BeanDefinition注册后)都会调用refresh方法完成容器的刷新工作即容器的启动工作。
在refresh方法中有如下一行:
//执行容器工厂的后置处理器,
invokeBeanFactoryPostProcessors(beanFactory);
此行就是spring执行容器后置处理器的入口方法,下面接着跟踪invokeBeanFactoryPostProcessors方法:
/**
* 实例化并调用所有已注册的BeanFactoryPostProcessor bean,如果给定,则遵守显式顺序。
*/
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//通过PostProcessorRegistrationDelegate的invokeBeanFactoryPostProcessors方法执行BeanFactoryPostProcessor
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
}
invokeBeanFactoryPostProcessors 方法很简单,通过PostProcessorRegistrationDelegate类的静态方法invokeBeanFactoryPostProcessors执行后置处理器。此方法完成两件事:
- 调用getBeanFactoryPostProcessors()方法获取手工注册到ApplicationContext的容器后置处理器集合
- 调用静态方法invokeBeanFactoryPostProcessors并传入后置处理器集合批量执行后置处理器
后置处理器执行
在上面看到了spring是通过静态方法invokeBeanFactoryPostProcessors执行后置处理,下面跟踪invokeBeanFactoryPostProcessors方法:
// 类PostProcessorRegistrationDelegate
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
//保存所有后置处理器bean名称的集合
Set<String> processedBeans = new HashSet<>();
//判断beanFactory 是否是BeanDefinitionRegistry
// 当是BeanDefinitionRegistry 时
// 将beanFactoryPostProcessors 分为BeanFactoryPostProcessor集合与BeanDefinitionRegistryPostProcessor集合
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
//BeanFactoryPostProcessor集合
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
//BeanDefinitionRegistryPostProcessor集合
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
//遍历查询BeanFactoryPostProcessor是否为BeanDefinitionRegistryPostProcessor
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
//当前postProcessor为BeanDefinitionRegistryPostProcessor是执行postProcessBeanDefinitionRegistry方法
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// Separate between BeanDefinitionRegistryPostProcessors that implement
// PriorityOrdered, Ordered, and the rest.
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// 首先在容器中查找实现了PriorityOrdered接口的的BeanDefinitionRegistryPostProcessors(bean定义注册后置处理器)名称
// 将查找到的名称集合放置到processedBeans中,以及将后置处理器添加到集合currentRegistryProcessors中
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
//对currentRegistryProcessors集合排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
//将当前查找到的currentRegistryProcessors放置到注册后置处理器集合中
registryProcessors.addAll(currentRegistryProcessors);
//遍历循环执行currentRegistryProcessors集合中的bean定义注册后置处理器
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
//清空currentRegistryProcessors集合
currentRegistryProcessors.clear();
// 下来在容器中查找实现了Ordered接口的的BeanDefinitionRegistryPostProcessors(bean定义注册后置处理器)名称
// 将查找到的名称集合放置到processedBeans中,以及将后置处理器添加到集合currentRegistryProcessors中
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName) && beanFactory