publicabstractclassFrameworkServletextendsHttpServletBeanimplementsApplicationContextAware{protectedWebApplicationContextinitWebApplicationContext(){WebApplicationContextrootContext=WebApplicationContextUtils.getWebApplicationContext(getServletContext());WebApplicationContextwac=null;// TODO 注解方式激活MVC功能时,会走到这里if(this.webApplicationContext!=null){// A context instance was injected at construction time -> use itwac=this.webApplicationContext;if(wacinstanceofConfigurableWebApplicationContext){ConfigurableWebApplicationContextcwac=(ConfigurableWebApplicationContext)wac;if(!cwac.isActive()){// 设置父容器,这个父容器时从servletContext中获取的。// 父容器是在 ContextLoader.initWebApplicationContext(),调用完refresh()后设置到servletContext中的。if(cwac.getParent()==null){cwac.setParent(rootContext);}// TODO 配置、刷新WebApplicationContext,调用DispatcherServlet的init()configureAndRefreshWebApplicationContext(cwac);}}}// 省略非关键代码...returnwac;}}
privateclassContextRefreshListenerimplementsApplicationListener<ContextRefreshedEvent>{@OverridepublicvoidonApplicationEvent(ContextRefreshedEventevent){// TODO 重点:上下文刷新事件FrameworkServlet.this.onApplicationEvent(event);}}
publicclassDispatcherServletextendsFrameworkServlet{protectedvoidinitStrategies(ApplicationContextcontext){// 初始化MultipartResolver,用于处理文件上传服务,如果有文件上传,// 那么就会将当前的HttpServletRequest包装成DefaultMultipartHttpServletRequest,// 并且将每个上传的内容封装成CommonsMultipartFile对象。需要在dispatcherServlet-servlet.xml中配置文件上传解析器。initMultipartResolver(context);// 用于处理应用的国际化问题,本地化解析策略。initLocaleResolver(context);// 用于定义一个主题。initThemeResolver(context);// TODO handlerMappings的初始化initHandlerMappings(context);// TODO handlerAdapters的初始化initHandlerAdapters(context);// TODO 异常处理器的初始化,当Handler处理出错后,会通过此将错误日志记录在log文件中,默认实现类是SimpleMappingExceptionResolver。initHandlerExceptionResolvers(context);// 将指定的ViewName按照定义的RequestToViewNameTranslators替换成想要的格式。initRequestToViewNameTranslator(context);// 用于将View解析成页面。initViewResolvers(context);// 用于生成FlashMap管理器。initFlashMapManager(context);}}
publicclassDispatcherServletextendsFrameworkServlet{privatevoidinitHandlerMappings(ApplicationContextcontext){this.handlerMappings=null;if(this.detectAllHandlerMappings){logger.debug("detectAllHandlerMappings --> true");// Find all HandlerMappings in the ApplicationContext, including ancestor contexts.// 获取到IOC容器中所有的HandlerMapping实例映射Map<String,HandlerMapping>matchingBeans=BeanFactoryUtils.beansOfTypeIncludingAncestors(context,HandlerMapping.class,true,false);if(!matchingBeans.isEmpty()){this.handlerMappings=newArrayList<>(matchingBeans.values());// We keep HandlerMappings in sorted order.AnnotationAwareOrderComparator.sort(this.handlerMappings);}}// 省略非关键代码...}}
publicclassDispatcherServletextendsFrameworkServlet{privatevoidinitHandlerAdapters(ApplicationContextcontext){this.handlerAdapters=null;if(this.detectAllHandlerAdapters){// Find all HandlerAdapters in the ApplicationContext, including ancestor contexts.// 从IOC容器中获取所有的HandlerAdapter实例Map<String,HandlerAdapter>matchingBeans=BeanFactoryUtils.beansOfTypeIncludingAncestors(context,HandlerAdapter.class,true,false);if(!matchingBeans.isEmpty()){this.handlerAdapters=newArrayList<>(matchingBeans.values());// We keep HandlerAdapters in sorted order.AnnotationAwareOrderComparator.sort(this.handlerAdapters);}}// 省略非关键代码...}}
publicclassDispatcherServletextendsFrameworkServlet{privatevoidinitHandlerExceptionResolvers(ApplicationContextcontext){this.handlerExceptionResolvers=null;if(this.detectAllHandlerExceptionResolvers){// 获取所有的异常处理器// Find all HandlerExceptionResolvers in the ApplicationContext, including ancestor contexts.Map<String,HandlerExceptionResolver>matchingBeans=BeanFactoryUtils.beansOfTypeIncludingAncestors(context,HandlerExceptionResolver.class,true,false);if(!matchingBeans.isEmpty()){this.handlerExceptionResolvers=newArrayList<>(matchingBeans.values());// We keep HandlerExceptionResolvers in sorted order.AnnotationAwareOrderComparator.sort(this.handlerExceptionResolvers);}}// 省略非关键代码...}}