Spring Framework之加载机制的内置接口Bean加载(四)

一、前言

声明:本章不会针对所有内置接口Bean进行解析,但会对主要的进行深度剖析

前文分别介绍了单例Bean单例循环依赖Bean,本章主要说明部分内置接口Bean的加载(接口Bean),这种Bean拥有着一系列的扩展点,使其在Spring Framework的中有着独特的生命周期,本章上下文刷新需要从前文得到概览图,通过下面2点我们讲了解简单的Spring加载:
1.内置接口Bean在Spring Framework中的意义
2.游走内置接口Bean加载源码

二、内置接口Bean在Spring Framework中的意义

一个内置接口Bean拥有着独特的生命周期,能逆天改命,也能做一切的事。

开源框架中通常使用这种内置接口Bean加载自己的特定类来与Spring完成集成。

三、游走内置接口Bean加载源码

BeanDefinitionRegistryPostProcessor接口,上下文刷新中invokeBeanFactoryPostProcessors(beanFactory);处理

代码块

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
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;

/*********************************************
* 我的Bean定义注表后置处理器构建器 *
*********************************************/
public class MyBeanDefinitionRegistryPostProcessorBuilder {
//为BeanFactoryPostProcessors而生
public static MyBeanDefinitionRegistryPostProcessor buildBeanFactoryPostProcessor(){return new MyBeanDefinitionRegistryPostProcessorOfAddBeanFactoryPostProcessor();}
//优先定制的
public static Class buildPriorityOrdered(){return MyBeanDefinitionRegistryPostProcessorPriorityOrdered.class;}
//定制的
public static Class buildOrdered(){return MyBeanDefinitionRegistryPostProcessorOrdered.class;}
//普通的
public static Class build(){return MyBeanDefinitionRegistryPostProcessor.class;}

}

class MyBeanDefinitionRegistryPostProcessorOfAddBeanFactoryPostProcessor extends MyBeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.print("postProcessBeanDefinitionRegistry()\t我最快\t");
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.print("postProcessBeanFactory()\t我最快\t");
}
}

class MyBeanDefinitionRegistryPostProcessorPriorityOrdered extends MyBeanDefinitionRegistryPostProcessor implements PriorityOrdered {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.print("我第一快\t");
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.print("我第一快\t");
}

@Override
public int getOrder() {
return 100;
}
}

class MyBeanDefinitionRegistryPostProcessorOrdered extends MyBeanDefinitionRegistryPostProcessor implements Ordered {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.print("我第二快\t");
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.print("我第二快\t");
}

@Override
public int getOrder() {
return 1;
}
}

class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.print("我最后\n");
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.print("我最后\n");
}
}

junit代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Test
public void Test(){
// 构建应用上下文
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// 注册
context.register(
MyBeanDefinitionRegistryPostProcessorBuilder.buildPriorityOrdered(),
MyBeanDefinitionRegistryPostProcessorBuilder.buildOrdered(),
MyBeanDefinitionRegistryPostProcessorBuilder.build()
);
context.addBeanFactoryPostProcessor(MyBeanDefinitionRegistryPostProcessorBuilder.buildBeanFactoryPostProcessor());
// 刷新应用上下文
context.refresh();
}

AbstractApplicationContext
avatar
PostProcessorRegistrationDelegate

代码实在太多,不好截图,直接放源码

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//BeanFactoryPostProcessor接口对象方法回调
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

//回调过的Bean集
Set<String> processedBeans = new HashSet<>();

//全面处理BeanDefinitionRegistryPostProcessor接口对象 和 beanFactoryPostProcessors的BeanFactoryPostProcessor方法回调
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
//BeanFactoryPostProcessor处理集
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
//BeanFactoryPostProcessor处理集
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

//beanFactoryPostProcessors回调,并按接口分层
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
//回调postProcessBeanDefinitionRegistry
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
//BeanFactoryPostProcessor
regularPostProcessors.add(postProcessor);
}
}

//当前处理集
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

//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);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//回调postProcessBeanDefinitionRegistry
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();

//Ordered接口处理
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//回调postProcessBeanDefinitionRegistry
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();

//处理剩余的
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate = true;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//回调postProcessBeanDefinitionRegistry
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();
}

//回调postProcessBeanFactory -> BeanDefinitionRegistryPostProcessor接口
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
//回调postProcessBeanFactory -> beanFactoryPostProcessors中为BeanFactoryPostProcessor接口的
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}

else {
// Invoke factory processors registered with the context instance.
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}

//全面处理BeanFactoryPostProcessor接口对象
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
//跳过上面上阶段处理的bean
}
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}

sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
//回调postProcessBeanFactory -> PriorityOrdered接口
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
//回调postProcessBeanFactory -> Ordered接口
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
//回调postProcessBeanFactory -> 剩余的
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

//清空bean工厂的元数据缓存
beanFactory.clearMetadataCache();
}

程序执行输出

1
2
postProcessBeanDefinitionRegistry()	我最快	我第一快	我第二快	我最后
postProcessBeanFactory() 我最快 我第一快 我第二快 我最后

BeanPostProcessor接口,上下文刷新中registerBeanPostProcessors(beanFactory);处理,特别注意:这个是在初始化的时候就会进行后置回调,所以要结合程序输出来看

代码块

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;

/*********************************************
* 我的Bean后置处理器构建器 *
*********************************************/
public class MyBeanPostProcessorBuilder {
//结合bean定义的后置处理器
public static Class buildMergedBeanDefinitionPostProcessor(){return MyMergedBeanDefinitionPostProcessor.class;}
//优先定制的
public static Class buildPriorityOrdered(){return MyBeanPostProcessorPriorityOrdered.class;}
//定制的
public static Class buildOrdered(){return MyBeanPostProcessorOrdered.class;}
//普通的
public static Class build(){return MyBeanPostProcessor.class;}

}

class MyBeanPostProcessorPriorityOrdered extends MyBeanPostProcessor implements PriorityOrdered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("-----------------------------");
System.out.println("PriorityOrdered\tBefore");
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("PriorityOrdered\tAfter");
return bean;
}

@Override
public int getOrder() {
return 100;
}
}

class MyBeanPostProcessorOrdered extends MyBeanPostProcessor implements Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Ordered\tBefore");
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Ordered\tAfter");
return bean;
}

@Override
public int getOrder() {
return 1;
}
}

class MyBeanPostProcessor implements BeanPostProcessor {

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("common\tBefore");
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("common\tAfter");
return bean;
}
}

class MyMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor {

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("merged\tBefore");
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("merged\tAfter");
return bean;
}

@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
System.out.println("merged\tunique");
}

@Override
public void resetBeanDefinition(String beanName) {
System.out.println("=============================");
System.out.println("merged\treset");
}
}

junit代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Test
public void Test(){
// 构建应用上下文
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// 注册
context.register(
MyBeanPostProcessorBuilder.buildMergedBeanDefinitionPostProcessor(),
MyBeanPostProcessorBuilder.buildPriorityOrdered(),
MyBeanPostProcessorBuilder.buildOrdered(),
MyBeanPostProcessorBuilder.build(),
String.class
);
// 刷新应用上下文
context.refresh();
// 重新注册(任意类注册),来进行resetBeanDefinition回调
context.register(MyBeanPostProcessorBuilder.buildMergedBeanDefinitionPostProcessor());
}

程序执行输出

在虚线后面是额外加的注释,来帮助理解

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
-----------------------------MyBeanPostProcessorOrdered初始化引起回调
PriorityOrdered Before
PriorityOrdered After
-----------------------------MyMergedBeanDefinitionPostProcessor初始化引起的回调
PriorityOrdered Before
Ordered Before
PriorityOrdered After
Ordered After
-----------------------------MyBeanPostProcessor初始化引起回调
PriorityOrdered Before
Ordered Before
PriorityOrdered After
Ordered After
//这里往下为一组,String初始化引起回调
merged unique
-----------------------------
PriorityOrdered Before
Ordered Before
common Before
merged Before
PriorityOrdered After
Ordered After
common After
merged After
=============================重新注册引起的回调
merged reset

AbstractApplicationContext
avatar
PostProcessorRegistrationDelegate

代码实在太多,不好截图,直接放源码

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
//注册beanPostProcessor接口对象,并进行方法回调
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

//获取所有BeanPostProcessor接口beanName
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

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)) {
//实例化PriorityOrdered对象
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);
//将实例化PriorityOrdered对象注册到BeanPostProcessors中
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);


List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String ppName : orderedPostProcessorNames) {
//实例化Ordered对象
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
//将实例化Ordered对象注册到BeanPostProcessors中
registerBeanPostProcessors(beanFactory, orderedPostProcessors);

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

//将实例化剩余的对象注册到BeanPostProcessors中
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
sortPostProcessors(internalPostProcessors, beanFactory);
//将实例化MergedBeanDefinitionPostProcessor对象注册到BeanPostProcessors中
registerBeanPostProcessors(beanFactory, internalPostProcessors);

//添加ApplicationListenerDetector监听
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

回调机制

除重置其它的执行时间都在AbstractAutowireCapableBeanFactory的doCreateBean方法中,下面只列具体的执行时机代码点

AbstractAutowireCapableBeanFactory
MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition回调
avatar
avatar
BeanPostProcessor.postProcessBeforeInitialization回调
BeanPostProcessor.postProcessAfterInitialization回调
avatar
avatar
avatar
avatar

重置点,MergedBeanDefinitionPostProcessor.resetBeanDefinition();方法回调,直接定位DefaultListableBeanFactory.registerBeanDefinition();

DefaultListableBeanFactory
avatar
avatar

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~

支付宝
微信