源码分析
- 将beanfactory中所有类型为BeanPostProcessor的beandefinition获取,通过getbean()得到实例
- 对这些BeanPostProcessor按继承Order接口类型分为3类,对分类进行sort
- 将所有BeanPostProcessor添加到beanfactory.beanPostProcessors属性
- beanPostProcessors为一个集合,允许重复添加,重复添加时,先删除存在后进行添加,核心目的保持排序
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
获取前置阶段所有加载的beandefinition中类型为BeanPostProcessor的beandefinition的名称集合
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
将其分为四类 优先级排序 排序 和普通 还有一个内部的[为前面三个中实现了MergedBeanDefinitionPostProcessor]
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
注册实现优先级排序相关接口的bean后置处理器
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);
}
}
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
注册实现排序相关接口的bean后置处理器
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
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);
注册普通的没有实现排序相关接口的bean后置处理器
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
排序后注册MergedBeanDefinitionPostProcessor
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
常见的BeanPostProcessor
名称 | 作用 |
---|
AutowiredAnnotationBeanPostProcessor | 处理 @Autowired@Value@Inject | CommonAnnotationBeanPostProcessor | 处理@PostConstruct@PreDestroy @Resource等注解 | AnnotationAwareAspectJAutoProxyCreator | aop相关 |
需要注意这三个bpp都实现了InstantiationAwareBeanPostProcessor
BeanPostProcessor分类
- SmartInstantiationAwareBeanPostProcessor
- InstantiationAwareBeanPostProcessor
- MergedBeanDefinitionPostProcessor
- DestructionAwareBeanPostProcessor
- BeanPostProcessor
总结
- 注册的BeanPostProcessors主要在实例化bean的时候完成bean的织入,增强,替换等功能
- BeanPostProcessors分为5类,如InstantiationAwareBeanPostProcessor等等,后面会专门有一篇文章讲解不同的BPP作用,这里主要了解,每一种bpp都有一些独有的特性[接口方法],作用于bean创建的各个阶段,有些作用在未加入bean工厂前,有些作用于已加入bean工厂
|