springmvc的工作原理(原创)
DispathcerServler继承FrameworkServlet FrameworkServlet 继承HttpServletBean HttpServletBean 继承 HttpServlet实现EnvironmentAware接口
容器启动首先执行HttpServletBean的init方法 init方法将执行FrameworkServlet的initServletBean方法
FrameworkServlet的initServletBean方法将 初始化webapplicationContext this.webApplicationContext = initWebApplicationContext();
initWebApplicationContext方法将判断容器是否启动,否则将 wac = createWebApplicationContext(rootContext);
createWebApplicationContext方法里将configureAndRefreshWebApplicationContext(wac);
接着在configure...方法里面添加 wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
private class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {
public void onApplicationEvent(ContextRefreshedEvent event) { FrameworkServlet.this.onApplicationEvent(event); } }
上面的方法将添加Listener后,监听ContextRefreshListener,然后出发onApplicationEvent启动DispatherServlet的onRefresh方法
@Override protected void onRefresh(ApplicationContext context) { initStrategies(context); }
initStrategies将初始化springmvc
protected void initStrategies(ApplicationContext context) { initMultipartResolver(context); initLocaleResolver(context); initThemeResolver(context); initHandlerMappings(context); initHandlerAdapters(context); initHandlerExceptionResolvers(context); initRequestToViewNameTranslator(context); initViewResolvers(context); initFlashMapManager(context); }