Spring源码-03-容器refresh

1 refresh

java
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
// AbstractApplicationContext.java
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

// Prepare this context for refreshing.
/**
* 1 容器启动前置准备
*/
this.prepareRefresh();

// Tell the subclass to refresh the internal bean factory.
/**
* 模板方法
* 从子类的实现(GenericApplicationContext)获取Bean工厂beanFactory(DefaultListableBeanFactory)
*/
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.
/**
* 2 Bean工厂前置准备
*/
this.prepareBeanFactory(beanFactory);

try {
// Allows post-processing of the bean factory in context subclasses.
/**
* 3 空实现
*/
this.postProcessBeanFactory(beanFactory);

StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context.
/**
* 4 调用所有注册的BeanFactoryPostProcessor的实例
* - BeanFactoryPostProcessor
* - BeanDefinitionRegistryPostProcessor
* Bean工厂后置处理器职责
* - 作用对象是Bean工厂
* - 现在还没有将用户BeanDefinition注册到Bean工厂中
* - 特定的后置处理器作用是将用户BeanDefinition注册Bean工厂中
* ConfigurationClassPostProcessor作用的时机
* - 解析配置类
* - 注册BeanDefinition到Bean工厂
* - 增强full型配置类
* - 向Bean工厂注册一个Bean后置处理器ImportAwareBeanPostProcessor
*/
this.invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.
/**
* 5 Bean实例化初始化之前将后置处理器注册到容器中
* 后置处理器的职责
* - BeanDefinition都已经注册到了Bean工厂中(DefaultListableBeanFactory的beanDefinitionMap)
* - Spring可以根据BeanDefinition进行Bean的实例化和初始化
* - 在此之前注册Bean的后置处理器
* - 在Bean实例化前后回调
* - 在Bean初始化前后回调
* 上面Bean工厂后置处理器执行结束之后 就已经将BeanDefinition都注册到Bean工厂beanDefinitionMap中了
* 现在将所有Bean工厂中BeanDefinition中是Bean后置处理器的都进行实例化注册到Bean工厂的beanPostProcessors中
*/
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();

// Initialize message source for this context.
/**
* 6 初始化信息源
* - 国际化
*/
initMessageSource();

// Initialize event multicaster for this context.
/**
* 7 初始化容器事件传播器
*/
initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.
/**
* 8 特定上下文子类中初始化特殊的Bean
*/
onRefresh();

// Check for listener beans and register them.
/**
* 9 监听器Bean
*/
registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.
/**
* 10 实例化非延迟初始化单例
*/
finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.
/**
* 11 初始化容器生命周期事件处理器 发布容器生命周期事件
*/
finishRefresh();
}

catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}

// Destroy already created singletons to avoid dangling resources.
destroyBeans();

// Reset 'active' flag.
cancelRefresh(ex);

// Propagate exception to caller.
throw ex;
}

finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
contextRefresh.end();
}
}
}

2 步骤

序号 传送门
1 prepareRefresh方法
2 prepareBeanFactory方法
3 postProcessBeanFactory方法
4 invokeBeanFactoryPostProces
5 registerBeanPostProcessors方法
6 initMessageSource方法
7 initApplicationEventMulticaster方法
8 onRefresh方法
9 registerListeners方法
10 finishBeanFactoryInitialization方法
11 finishRefresh方法

3 示意图


Spring源码-03-容器refresh
https://bannirui.github.io/2023/03/11/Spring源码-03-容器refresh/
作者
dingrui
发布于
2023年3月11日
许可协议