这一章先总体看一下refresh方法
public void refresh() throws BeansException, IllegalStateException {
//首先,获得锁,话说这名字取得够随意。在5.0.6版本之前,是没有这行代码的,哪位大佬知道作者为什么要这样写么?
Object var1 = this.startupShutdownMonitor;
// 加锁执行容器初始化操作
synchronized(this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 准备此上下文刷新
this.prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
// 初始化beanFactory
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 为beanFactory设置一些参数
this.prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 注册一些postProcessBeanFactory
this.postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 执行postProcessBeanFactory
this.invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 注册一些BeanPostProcessors
this.registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
// 初始化MessageSource
this.initMessageSource();
// Initialize event multicaster for this context.
// 初始化spring事件派发器
this.initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
// 执行子类的一些onRefresh方法,如果有的话
this.onRefresh();
// Check for listener beans and register them.
// 注册监听器
this.registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
// 实例化剩余的非懒加载的bean
this.finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
// 执行事件
this.finishRefresh();
} catch (BeansException var9) {
if(this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
}
// Destroy already created singletons to avoid dangling resources.
// 销毁bean,有兴趣的自己看看
this.destroyBeans();
// Reset 'active' flag.
this.cancelRefresh(var9);
throw var9;
} finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
// 清除一些缓存,有兴趣的自己看看
t1his.resetCommonCaches();
}
1
}
##总体流程就是这样啦,下一章开始就是具体每个步骤啦,拜拜~