承接前文Spring源码情操陶冶-AbstractApplicationContext#finishBeanFactoryInitialization
约定web.xml
配置的contextClass
为默认值XmlWebApplicationContext
直接源码AbstractApplicationContext#finishRefresh
/**
* Finish the refresh of this context, invoking the LifecycleProcessor's
* onRefresh() method and publishing the
* {@link org.springframework.context.event.ContextRefreshedEvent}.
*/
protected void finishRefresh() {
// Initialize lifecycle processor for this context.
//会找寻lifecycleProcessor对应的bean,否则默认采用DefaultLifecycleProcessor
initLifecycleProcessor();
// Propagate refresh to lifecycle processor first.
getLifecycleProcessor().onRefresh();
// Publish the final event.
//其实是执行ContextRefreshedEvent该事件是针对spring mvc来的
publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.
LiveBeansView.registerApplicationContext(this);
}
从注释上可以得知其是完成对context的刷新,并执行LifecycleProcessor的onRefresh()方法和执行相关的ContextRefreshedEvent事件,但可惜引导DispatcherServlet进行初始化的ContextRefreshListener并不是由spring来触发的,而是由tomcat加载web.xml中的spring mvc配置文件启动的
DispatcherServlet#initStrategies
我们简单看下此段代码,spring启动只会帮助springmvc先初始化默认的策略方案,其他则由springmvc本身来执行,具体如下
/**
* Initialize the strategy objects that this servlet uses.
* <p>May be overridden in subclasses in order to initialize further strategy objects.
*/
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
关于spring mvc的初始化操作,我们放在后续计划中讨论,本文便不再继续深究