this.refreshContext(context//应用程序上下文); //->
private void refreshContext(ConfigurableApplicationContext context) { if (this.registerShutdownHook) { shutdownHook.registerApplicationContext(context); //shutdownHook注册应用程序上下文 -> } this.refresh(context); // -> applicationContext.refresh() -> }
SpringApplicationShutdownHook implements Runnable 属性:
private static final int SLEEP = 50; private static final long TIMEOUT; private static final Log logger; private final SpringApplicationShutdownHook.Handlers handlers = new SpringApplicationShutdownHook.Handlers(); //处理器:属性:Set<Runnable> actions -> private final Set<ConfigurableApplicationContext> contexts = new LinkedHashSet();//存储上下文 private final Set<ConfigurableApplicationContext> closedContexts = Collections.newSetFromMap(new WeakHashMap()); //存储关闭了的上下文 private final SpringApplicationShutdownHook.ApplicationContextClosedListener contextCloseListener = new SpringApplicationShutdownHook.ApplicationContextClosedListener(); //上下文关闭监听器 private final AtomicBoolean shutdownHookAdded = new AtomicBoolean(false); private boolean inProgress;
void registerApplicationContext(ConfigurableApplicationContext context) { this.addRuntimeShutdownHookIfNecessary(); Class var2 = SpringApplicationShutdownHook.class; synchronized(SpringApplicationShutdownHook.class) { this.assertNotInProgress(); context.addApplicationListener(this.contextCloseListener);//存储listener this.contexts.add(context); //存储该Context } }
public void refresh() throws BeansException, IllegalStateException {
synchronized(this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");//启动刷新步骤
this.prepareRefresh();//准备刷新 ->
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory(); //刷新beanFactory: 有的话删除,没有的话新创建,设置环境,加载器,解析器等,返回新的beanFactory
this.prepareBeanFactory(beanFactory);//准备beanFactory: 设置属性注册编辑器,bean后置处理器,
try {
this.postProcessBeanFactory(beanFactory);/ /处理BeanFactory 默认为空,可拓展
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");// 启动步骤
this.invokeBeanFactoryPostProcessors(beanFactory); //根据bean类型激活 :遍历所有处理器,依次进行处理
this.registerBeanPostProcessors(beanFactory);//注册处理器 ->
beanPostProcess.end();//结束步骤
this.initMessageSource();//初始化信息资源:beanFactory里有的话从beanFactory拿,没有的话新建t
this.initApplicationEventMulticaster();//初始化事件广播器:同上
this.onRefresh();//刷新,默认为空方法,可扩展
this.registerListeners();//注册listener:将所有listener添加到广播器中,
this.finishBeanFactoryInitialization(beanFactory);//设置一些基本信息:默认类加载器,初始化单例等
this.finishRefresh();//完成刷新后,清理资源缓存,初始化生命周期处理器并启动,广播上下文刷新事件,存在父节点时,也对父节点广播
} catch (BeansException var10) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var10);
}
this.destroyBeans();
this.cancelRefresh(var10);
throw var10;
} finally {
this.resetCommonCaches();//重置缓存
contextRefresh.end();//结束步骤
}
}
}
protected void prepareRefresh() {
this.startupDate = System.currentTimeMillis(); // 记录时间
this.closed.set(false);
this.active.set(true); //激活
if (this.logger.isDebugEnabled()) {
if (this.logger.isTraceEnabled()) {
this.logger.trace("Refreshing " + this);
} else {
this.logger.debug("Refreshing " + this.getDisplayName());
}
}
this.initPropertySources(); //初始化资源 原方法为空 可拓展
this.getEnvironment().validateRequiredProperties();
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet(this.applicationListeners);
} else {
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
this.earlyApplicationEvents = new LinkedHashSet();// 一系列初始化
}
public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false); //根据类型取得Bean的名字
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length; //bean的数目
beanFactory.addBeanPostProcessor(new PostProcessorRegistrationDelegate.BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)); //重置beanPostProcessor
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList();
List<BeanPostProcessor> internalPostProcessors = new ArrayList();
List<String> orderedPostProcessorNames = new ArrayList();
List<String> nonOrderedPostProcessorNames = new ArrayList();
String[] var8 = postProcessorNames;
int var9 = postProcessorNames.length;
String ppName;
BeanPostProcessor pp; //将Bean按照类型分别存在不同的list中
for(int var10 = 0; var10 < var9; ++var10) {
ppName = var8[var10];
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
pp = (BeanPostProcessor)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);//排序 todo
registerBeanPostProcessors(beanFactory, (List)priorityOrderedPostProcessors);
List<BeanPostProcessor> orderedPostProcessors = new ArrayList(orderedPostProcessorNames.size());
Iterator var14 = orderedPostProcessorNames.iterator();
while(var14.hasNext()) {
String ppName = (String)var14.next();
BeanPostProcessor pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, (List)orderedPostProcessors);
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList(nonOrderedPostProcessorNames.size());
Iterator var17 = nonOrderedPostProcessorNames.iterator();
while(var17.hasNext()) {
ppName = (String)var17.next();
pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, (List)nonOrderedPostProcessors); //添加到列表中存起来
sortPostProcessors(internalPostProcessors, beanFactory);//排序
registerBeanPostProcessors(beanFactory, (List)internalPostProcessors);//添加到列表中存起来
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}