BeanPostProcessor 注册流程

警告
本文最后更新于 2020-11-24,文中内容可能已过时。

spring源码系列文章,示例代码的中文注释,均是 copy 自 https://gitee.com/wlizhi/spring-framework

链接中源码是作者从 github 下载,并以自身理解对核心流程及主要节点做了详细的中文注释。


1 BeanPostProcessor的作用

BeanPostProcessor是对SpringIOC容器中bean实例化的一些扩展,在bean实例化的关键节点进行了一些插桩。

真正实例化bean的方法是beanFactory.getBean()方法。而在bean实例化之前、实例化之后、依赖注入、初始化方法执行之前、初始化方法执行之后等节点进行一些钩子回调。BeanPostProcessor的作用就体现于此。

BeanPostProcessor接口只提供初始化方法执行前、后的节点操作。其余操作在它的子接口中定义。

2 BeanPostProcessor的体系结构

https://oss.wlizhi.cc/blog/spring/BeanPostProcessor-inheritance-structure.png

上图详细注释了BeanPostProcessor及其子接口每个方法的作用节点,从BeanPostProcessor接口继承体系可以看出,BeanPostProcessor贯穿整个Spring容器中Bean的实例化流程。可以说,在Spring容器中,任何一个Bean的实例化,在其关键节点,都有BeanPostProcessor的存在。甚至可以说它是SpringIOC容器组成部分中最重要的一个类。

3 BeanPostProcessor的注册流程

以下是spring中BeanPostProcessor的源码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
final class PostProcessorRegistrationDelegate {
public static void registerBeanPostProcessors(
			ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

		// 从beanDefinitionNames中获取所有BeanPostProcessor类型的beanName。
		String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

		// 这里之所以要+1,因为在这个方法的结尾处,单独注册了一个BeanPostProcessor:ApplicationListenerDetector
		// 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.
		int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
		beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

		// Separate between BeanPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.
		// 实现了PriorityOrdered接口的。
		List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
		// MergedBeanDefinitionPostProcessor类型的。
		List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
		// 实现了Ordered接口的。
		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);
			}
		}

		// 注册BeanPostProcessors
		// First, register the BeanPostProcessors that implement PriorityOrdered.
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
		registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

		// 注册实现了Ordered接口的BeanPostProcessor。
		// Next, register the BeanPostProcessors that implement Ordered.
		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);

		// 注册没有实现接口的BeanPostProcessor
		// Now, register all regular BeanPostProcessors.
		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);

		// 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));
	}
}

从上面代码可以看出,BeanPostProcessor的注册流程和BeanFactoryPostProcessor的注册流程类似。获取到BeanFactory中存储的BeanPostProcessor类型的beanName,然后对实现了不同排序接口的类、及没有实现排序接口的类分别进行getBean()操作、排序和注册。

spring会在不同的流程节点执行BeanPostProcessor及其子接口对应的方法。具体的实现类在后面的spring容器bean的实例化相关文章描述。