共计 5831 个字符,预计需要花费 15 分钟才能阅读完成。
前言
后面通过 invokeBeanFactoryPostProcessors 这一步理解到了什么是 BeanFactoryPostProcessor,以及 BeanFactoryPostProcessor 的应用及作用,并通过 invokeBeanFactoryPostProcessors 这一步源码,对 BeanFactoryPostProcessor 的加载流程有了进一步理解。
当初就一起进入下一个环节:
registerBeanPostProcessors(beanFactory);
这一步次要的作用是加载 BeanPostProcessor,从名字也能够看出,只是加载,并没有执行。
不过,在进入源码之前,仍然是联合官网,先理解以下几个问题:
- 什么是 BeanPostProcessor?
- BeanPostProcessor 是如何应用的?
- BeanPostProcessor 有什么用?
什么是 BeanPostProcessor?
如截图所示,在官网 1.8.1 Customizing Beans by Using a BeanPostProcessor 中介绍,BeanPostProcessor
接口定义回调办法,能够实现这些办法,从而在 Bean 实例化期间批改 Bean 的属性。
BeanPostProcessor 是如何应用的?
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof UserComponent) {System.out.println("BeanPostProcessor 开始执行 初始化前..." + beanName);
UserComponent userComponent = (UserComponent) bean;
userComponent.setUserName("liuzhihang-postProcessBeforeInitialization");
return userComponent;
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof UserComponent) {System.out.println("BeanPostProcessor 开始执行 初始化后..." + beanName);
UserComponent userComponent = (UserComponent) bean;
userComponent.setUserName("liuzhihang-postProcessAfterInitialization");
return userComponent;
}
return bean;
}
}
如代码所示,只须要申明一个本人的 MyBeanPostProcessor 来实现 BeanPostProcessor
并重写其办法:
postProcessBeforeInitialization:在 Bean 实例后调用初始化办法之前进行解决。
postProcessAfterInitialization:在 Bean 实例化后调用初始化办法之后进行解决。
并且在测试时能够发现,BeanPostProcessor 批改的属性会笼罩 BeanFactoryPostProcessor,至于起因能够浏览下 Spring 源码学习 11:invokeBeanFactoryPostProcessors 这篇文章,置信对 BeanFactoryPostProcessor 有了肯定的理解之后,肯定会有本人的答案。
当然我集体的了解就是 BeanFactoryPostProcessor 是在 Bean 实例化之前,是通过批改元数据从而批改的 Bean 的元素信息。
这块也能够通过代码进行验证。
能够看出,执行程序是
BeanFactoryPostProcessor#postProcessBeanFactory
-> BeanPostProcessor#postProcessBeforeInitialization
-> BeanPostProcessor#postProcessAfterInitialization
所以,前面批改的属性,会笼罩之前批改的属性。
至于 BeanPostProcessor
是如何批改属性的,在这里先不做介绍,持续进入本节的配角 registerBeanPostProcessors 源码剖析
registerBeanPostProcessors 源码
在 Spring refresh 办法中,执行 registerBeanPostProcessors 次要作用是将 BeanPostProcessor 注册到容器中,源码如下:
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
这里调用了 PostProcessorRegistrationDelegate
类的静态方法,持续跟进:
public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
// 查找 BeanPostProcessor 类型的 Bean 的名称汇合, 就是获取所有继承了 BeanPostProcessor 的类
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// Register BeanPostProcessorChecker that logs an info message when
// a bean is created during BeanPostProcessor instantiation, i.e. when
// a bean is not eligible for getting processed by all BeanPostProcessors.
// 注册一个 BeanPostProcessorChecker,用来记录 bean 在 BeanPostProcessor 实例化时的信息。int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// Separate between BeanPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
// 四个汇合 辨别实现不同接口的 BeanPostProcessors
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors.add(pp);
}
} else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {orderedPostProcessorNames.add(ppName);
} else {nonOrderedPostProcessorNames.add(ppName);
}
}
// First, register the BeanPostProcessors that implement PriorityOrdered.
// 排序后执行 实现 PriorityOrdered 的 BeanPostProcessors
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// Next, register the BeanPostProcessors that implement Ordered.
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String ppName : orderedPostProcessorNames) {BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors.add(pp);
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// Now, register all regular BeanPostProcessors.
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String ppName : nonOrderedPostProcessorNames) {BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// Finally, re-register all internal BeanPostProcessors.
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
代码中增加了相应的正文,置信读过上一篇文章(invokeBeanFactoryPostProcessors 源码)的小伙伴,肯定会感觉十分相熟,这个办法的逻辑和下面基本一致,都是申明汇合,排序,注册到 BeanFactory 中。
不过还是有区别的:
registerBeanPostProcessors 这一步仅仅将 BeanPostProcessor 注册到 BeanFactory 中,并没有执行!!!
总结
本文源码局部绝对比较简单,次要破费局部篇幅介绍什么是 BeanPostProcessor 以及 BeanPostProcessor 的应用办法。
一句话总结这一步就是:注册 BeanPostProcessor 到 BeanFactory 中,然而没有执行。
相干举荐
- Spring 源码学习 11:invokeBeanFactoryPostProcessors
- Spring 源码学习 10:prepareBeanFactory 和 postProcessBeanFactory
- Spring 源码学习 09:refresh 大略流程