一、介绍
使用ApplicationContext启动Spring容器时,代码如下:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
这行代码,包含了整个Spring启动的全部过程,中间会经历很多步骤。我们今天探讨的是,这些很多步骤中的最后一步:finishRefresh()
目的是为了看看容器中,所有的Bean实例都初始化完成之后,Spring还做了哪些事情?
二、源码分析
1. Application上下文刷新经典入口
启动容器的步骤由AbstractApplicationContext类的refresh方法完成,代码如下:
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// 1. 准备刷新的上下文环境
prepareRefresh();
// 2. 初始化BeanFactory,并进行XML文件的加载
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// 3. 对BeanFactory进行各种功能填充
prepareBeanFactory(beanFactory);
try {
// 4. 子类覆盖犯法做额外的处理
postProcessBeanFactory(beanFactory);
// 5. 调用BeanFactory后处理器
invokeBeanFactoryPostProcessors(beanFactory);
// 6. 注册bean后处理器,在调用getBean的时候回调这些bean后处理器的方法
registerBeanPostProcessors(beanFactory);
// 7. 为上下文初始化Message源
initMessageSource();
// 8. 初始化事件多播器
initApplicationEventMulticaster();
// 9. 留给子类初始化其他bean
onRefresh();
// 10. 注册监听器
registerListeners();
// 11. 初始化剩下的单例Bean(非惰性的)
finishBeanFactoryInitialization(beanFactory);
// 12. 最后一步,发布通知事件
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// 销毁已经创建的单例,以避免挂起资源。
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();
}
}
}
很显然,我们本章的重点就是第12个步骤,点进去看下~
2. Application上下文刷新完成
Spring官方对这个方法的解释:调用LifecycleProcessor的onRefresh()方法;并且发布ContextRefreshedEvent
protected void finishRefresh() {
// 1. 清除上下文级别的资源缓存(例如扫描的ASM元数据)。
clearResourceCaches();
// 2. 为上下文初始化LifecycleProcessor
initLifecycleProcessor();
// 3. 调用LifecycleProcessor的onRefresh方法
getLifecycleProcessor().onRefresh();
// 4. 发布上下文刷新完成的事件
publishEvent(new ContextRefreshedEvent(this));
// 5. 参与LiveBeansView MBean,如果是激活状态。
LiveBeansView.registerApplicationContext(this);
}
从代码行数上来看,一共做了5件事情。其中第1、5两个步骤我们无需关注,实际用途不大。
那么Spring在完成上下文刷新时,做的事情可以简要地概括为以下三个步骤:
- 初始化LifecycleProcessor
- 调用LifecycleProcessor的onRefresh方法
- 发布ContextRefreshedEvent(上下文刷新完成事件)
用来通知Spring容器中的监听器
LifecycleProcessor即生命周期处理器,在ApplicationContext启动和停止的时候,通知Spring管理的实现了Lifecycle接口的对象。
分别来看下这三个步骤
3. 初始化LifecycleProcessor
protected void initLifecycleProcessor() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
this.lifecycleProcessor =
beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
if (logger.isDebugEnabled()) {
logger.debug("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");
}
}
else {
// 默认的LifecycleProcessor
DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
defaultProcessor.setBeanFactory(beanFactory);
this.lifecycleProcessor = defaultProcessor;
// 把默认的LifecycleProcessor注册到容器中
beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate LifecycleProcessor with name '" +
LIFECYCLE_PROCESSOR_BEAN_NAME +
"': using default [" + this.lifecycleProcessor + "]");
}
}
}
这个步骤比较简单,容器中如果没有用户定义的LifecycleProcessor,就使用默认的DefaultLifecycleProcessor
4. onRefresh
public void onRefresh() {
startBeans(true);
this.running = true;
}
private void startBeans(boolean autoStartupOnly) {
// 1. 获取容器中所有Lifecycle类型的Bean
Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
Map<Integer, LifecycleGroup> phases = new HashMap<>();
// 如果是SmartLifecycle类型的Bean,isAutoStartup需要返回true,才会继续执行
// 因为autoStartupOnly值为true,所以非SmartLifecycle类型的Bean跳过
lifecycleBeans.forEach((beanName, bean) -> {
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
// 2. 获取该Lifecycle的阶段值,用来控制调用顺序
int phase = getPhase(bean);
LifecycleGroup group = phases.get(phase);
if (group == null) {
group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
phases.put(phase, group);
}
group.add(beanName, bean);
}
});
if (!phases.isEmpty()) {
List<Integer> keys = new ArrayList<>(phases.keySet());
// 排序
Collections.sort(keys);
for (Integer key : keys) {
// 循环调用Lifecycle的start方法
phases.get(key).start();
}
}
}
容器中可以定义多个实现Lifecycle接口的Bean,从代码中可以看到,此时并不会调用这些Bean的start方法。
我们一般会实现SmartLifecycle接口,并把isAutoStartup()返回值设置为true。
ps:实现SmartLifecycle接口时要覆盖getPhase()方法,该方法用来控制Lifecycle的start()和stop()方法的执行顺序,值越小越先执行,默认值是0
5. 发布上下文刷新完成的事件
还没用过监听的同学,请先移步《Spring监听器的完整使用步骤》
发布事件的目的,是为了通知容器中的监听器,进而执行监听器中自定义的逻辑。发布事件的底层是调用了SimpleApplicationEventMulticaster类的multicastEvent方法,来看一下
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
// 循环监听器
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
// 调用监听方法
invokeListener(listener, event);
}
}
}
看下invokeListener方法
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) {
ErrorHandler errorHandler = getErrorHandler();
if (errorHandler != null) {
try {
// 真正执行的方法
doInvokeListener(listener, event);
}
catch (Throwable err) {
errorHandler.handleError(err);
}
}
else {
// 真正执行的方法
doInvokeListener(listener, event);
}
}
这个方法没有什么逻辑,继续看doInvokeListener方法
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
// 调用监听器的onApplicationEvent方法
listener.onApplicationEvent(event);
}
catch (ClassCastException ex) {
String msg = ex.getMessage();
if (msg == null || matchesClassCastMessage(msg, event.getClass().getName())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type for
// -> let's suppress the exception and just log a debug message.
Log logger = LogFactory.getLog(getClass());
if (logger.isDebugEnabled()) {
logger.debug("Non-matching event type for listener: " + listener, ex);
}
}
else {
throw ex;
}
}
}
看到这个地方就能看出,为什么自定义的监听器都会覆盖onApplicationEvent方法 了,因为容器发布事件后,最终都会调用监听器的onApplicationEvent方法。
三、总结
借用Spring官方对finishRefresh()方法的解释:调用LifecycleProcessor的onRefresh()方法;并且发布ContextRefreshedEvent。
- LifecycleProcessor是为了把ApplicationContext的声明周期,通知给容器中实现了SmartLifecycle接口的Bean。即调用这些Bean的start()和stop()方法。需要把isAutoStartup()方法返回值设为true,才会生效。
- 发布ContextRefreshedEvent是为了,通知容器中实现了ApplicationListener接口的Bean(监听器),即调用这些Bean的onApplicationEvent()方法
思考:写一篇易懂的文章,需要去寻找你了解,但别人却不了解的前置知识,并把它表达出来。