SpringMVC源码分析--容器初始化(五)DispatcherServlet

来源:互联网 发布:stc15单片机 编辑:程序博客网 时间:2024/05/23 11:45

本文为转载,地址:http://blog.csdn.NET/qq924862077/

上一篇博客SpringMVC源码分析--容器初始化(四)FrameworkServlet我们已经了解到了SpringMVC容器的初始化,SpringMVC对容器初始化后会进行一系列的其他属性的初始化操作,在SpringMVC初始化完成之后会调用onRefresh(wac)方法,它通过模板方式在子类DispatcherServlet中实现的。

onRefresh函数实现如下,其具体的实现就放到initStrategies函数中实现。

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.        /** 
  2.  * This implementation calls {@link #initStrategies}. 
  3.  */  
  4. //初始化策略,完成SpringMVC默认实现类的初始化,onRefresh是在父类FrameworkServlet中调用  
  5. @Override  
  6. protected void onRefresh(ApplicationContext context) {  
  7.     initStrategies(context);  
  8. }  
initStrategies函数中初始化了一些springMVC运行过程中需要使用的默认的实现类,接下来我们分别介绍一下默认实现的 类。

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.      //初始化SpringMVC的一些策略,也是springMVC主要的组件  
  2. rotected void initStrategies(ApplicationContext context) {  
  3. initMultipartResolver(context);//文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析  
  4. initLocaleResolver(context);//本地化解析  
  5. initThemeResolver(context);//主题解析  
  6. initHandlerMappings(context);//通过HandlerMapping,将请求映射到处理器  
  7. initHandlerAdapters(context);//通过HandlerAdapter支持多种类型的处理器  
  8. initHandlerExceptionResolvers(context);//如果执行过程中遇到异常,将交给HandlerExceptionResolver来解析  
  9. initRequestToViewNameTranslator(context);//直接解析请求到视图名  
  10. initViewResolvers(context);//通过viewResolver解析逻辑视图到具体视图实现  
  11. initFlashMapManager(context);//flash映射管理器  
initMultipartResolver初始化文件上传解析器,与文件上传的相关的操作,有关文件上传操作我们会仔细分析,这里不做过多介绍。

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.       ////文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析  
  2. rivate void initMultipartResolver(ApplicationContext context) {  
  3. try {  
  4.     //bean id被写死,在配置的时候需要注意,文件上传时需要注入bean名称为multipartResolver的类  
  5.     this.multipartResolver = context.getBean(MULTIPART_RESOLVER_BEAN_NAME, MultipartResolver.class);  
  6.     if (logger.isDebugEnabled()) {  
  7.         logger.debug("Using MultipartResolver [" + this.multipartResolver + "]");  
  8.     }  
  9. }  
  10. catch (NoSuchBeanDefinitionException ex) {  
  11.     // Default is no multipart resolver.  
  12.     this.multipartResolver = null;  
  13.     if (logger.isDebugEnabled()) {  
  14.         logger.debug("Unable to locate MultipartResolver with name '" + MULTIPART_RESOLVER_BEAN_NAME +  
  15.                 "': no multipart request handling provided");  
  16.     }  
  17. }  

initLocaleResolver是初始化一些多语言实现相关的类,在配置多语言本地化时会注入bean名称为localeResolver,默认实现的类有FixedLocaleResolver ,SessionLocaleResolver ,CookieLocaleResolver, AcceptHeaderLocaleResolver 

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.        //本地化解析,支持web应用程序国际化,在springMVC应用程序中,用户的区域是通过区域解析器事变的  
  2. //spring采用的是默认区域解析器 AcceptHeaderLocaleResolver  ,通过检验HTTP请求的accept-language头部来解析区域  
  3. //这个头部是有用户的浏览器根据底层操作系统的区域设置进行设定,这个区域解析器无法改变用户的区域,因为无法修改用户  
  4. //操作系统的区域设置  
  5. private void initLocaleResolver(ApplicationContext context) {  
  6.     try {  
  7.         //在配置多语言本地化时会注入bean名称为localeResolver,默认实现的类有FixedLocaleResolver ,SessionLocaleResolver ,CookieLocaleResolver, AcceptHeaderLocaleResolver   
  8.         this.localeResolver = context.getBean(LOCALE_RESOLVER_BEAN_NAME, LocaleResolver.class);  
  9.         if (logger.isDebugEnabled()) {  
  10.             logger.debug("Using LocaleResolver [" + this.localeResolver + "]");  
  11.         }  
  12.     }  
  13.     catch (NoSuchBeanDefinitionException ex) {  
  14.         // We need to use the default.  
  15.         this.localeResolver = getDefaultStrategy(context, LocaleResolver.class);  
  16.         if (logger.isDebugEnabled()) {  
  17.             logger.debug("Unable to locate LocaleResolver with name '" + LOCALE_RESOLVER_BEAN_NAME +  
  18.                     "': using default [" + this.localeResolver + "]");  
  19.         }  
  20.     }  
  21. }  
initThemeResolver与样式相关的解析器,需要在配置文件中注入bean名称为themeResolver的,FixedThemeResolver, SessionThemeResolver和CookieThemeResolver

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.        //动态样式支持  
  2. private void initThemeResolver(ApplicationContext context) {  
  3.     try {  
  4.         //需要在配置文件中注入bean名称为themeResolver的,FixedThemeResolver, SessionThemeResolver和CookieThemeResolver  
  5.         this.themeResolver = context.getBean(THEME_RESOLVER_BEAN_NAME, ThemeResolver.class);  
  6.         if (logger.isDebugEnabled()) {  
  7.             logger.debug("Using ThemeResolver [" + this.themeResolver + "]");  
  8.         }  
  9.     }  
  10.     catch (NoSuchBeanDefinitionException ex) {  
  11.         // We need to use the default.  
  12.         this.themeResolver = getDefaultStrategy(context, ThemeResolver.class);  
  13.         if (logger.isDebugEnabled()) {  
  14.             logger.debug(  
  15.                     "Unable to locate ThemeResolver with name '" + THEME_RESOLVER_BEAN_NAME + "': using default [" +  
  16.                             this.themeResolver + "]");  
  17.         }  
  18.     }  
  19. }  
initHandlerMappings初始化HandlerMapping,HandlerMapping的工作就是为每个请求找到合适的请求找到一个处理器handler

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.        //会加载HandlerMapping,默认使用BeanNameUrlHandlerMapping  
  2. private void initHandlerMappings(ApplicationContext context) {  
  3.     this.handlerMappings = null;  
  4.   
  5.     if (this.detectAllHandlerMappings) {  
  6.         // Find all HandlerMappings in the ApplicationContext, including ancestor contexts.  
  7.         //默认加载所有的HandlerMapping,  
  8.         Map<String, HandlerMapping> matchingBeans =  
  9.                 BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.classtruefalse);  
  10.         if (!matchingBeans.isEmpty()) {  
  11.             //数组中含有多个HandlerMapping  
  12.             this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());  
  13.             // We keep HandlerMappings in sorted order.  
  14.             AnnotationAwareOrderComparator.sort(this.handlerMappings);  
  15.         }  
  16.     }  
  17.     else {  
  18.         try {  
  19.             HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);  
  20.             this.handlerMappings = Collections.singletonList(hm);  
  21.         }  
  22.         catch (NoSuchBeanDefinitionException ex) {  
  23.             // Ignore, we'll add a default HandlerMapping later.  
  24.         }  
  25.     }  
  26.   
  27.     // Ensure we have at least one HandlerMapping, by registering  
  28.     // a default HandlerMapping if no other mappings are found.  
  29.     if (this.handlerMappings == null) {  
  30.         this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);  
  31.         if (logger.isDebugEnabled()) {  
  32.             logger.debug("No HandlerMappings found in servlet '" + getServletName() + "': using default");  
  33.         }  
  34.     }  
  35. }  
initHandlerAdapters是初始化HandlerAdapter,HandlerAdapter是真正调用Controller操作的类
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.        //初始化HandlerAdapters,默认使用的是SimpleControllerHandlerAdapter  
  2. private void initHandlerAdapters(ApplicationContext context) {  
  3.     this.handlerAdapters = null;  
  4.   
  5.     if (this.detectAllHandlerAdapters) {  
  6.         // Find all HandlerAdapters in the ApplicationContext, including ancestor contexts.  
  7.         Map<String, HandlerAdapter> matchingBeans =  
  8.                 BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerAdapter.classtruefalse);  
  9.         if (!matchingBeans.isEmpty()) {  
  10.             //数组中含有多个HandlerAdapters  
  11.             this.handlerAdapters = new ArrayList<HandlerAdapter>(matchingBeans.values());  
  12.             // We keep HandlerAdapters in sorted order.  
  13.             AnnotationAwareOrderComparator.sort(this.handlerAdapters);  
  14.         }  
  15.     }  
  16.     else {  
  17.         try {  
  18.             HandlerAdapter ha = context.getBean(HANDLER_ADAPTER_BEAN_NAME, HandlerAdapter.class);  
  19.             this.handlerAdapters = Collections.singletonList(ha);  
  20.         }  
  21.         catch (NoSuchBeanDefinitionException ex) {  
  22.             // Ignore, we'll add a default HandlerAdapter later.  
  23.         }  
  24.     }  
  25.   
  26.       
  27.     //如果没有设置HandlerAdapters,则获取默认的配置  
  28.     if (this.handlerAdapters == null) {  
  29.         this.handlerAdapters = getDefaultStrategies(context, HandlerAdapter.class);  
  30.         if (logger.isDebugEnabled()) {  
  31.             logger.debug("No HandlerAdapters found in servlet '" + getServletName() + "': using default");  
  32.         }  
  33.     }  
  34. }  
initHandlerExceptionResolvers是初始化HandlerExceptionResolver,用来操作异常,接下来我们会对其进行详细分析
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.       //初始化HandlerExceptionResolver  
  2. rivate void initHandlerExceptionResolvers(ApplicationContext context) {  
  3. this.handlerExceptionResolvers = null;  
  4.   
  5. if (this.detectAllHandlerExceptionResolvers) {  
  6.     // Find all HandlerExceptionResolvers in the ApplicationContext, including ancestor contexts.  
  7.     Map<String, HandlerExceptionResolver> matchingBeans = BeanFactoryUtils  
  8.             .beansOfTypeIncludingAncestors(context, HandlerExceptionResolver.classtruefalse);  
  9.     if (!matchingBeans.isEmpty()) {  
  10.         this.handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>(matchingBeans.values());  
  11.         // We keep HandlerExceptionResolvers in sorted order.  
  12.         AnnotationAwareOrderComparator.sort(this.handlerExceptionResolvers);  
  13.     }  
  14. }  
  15. else {  
  16.     try {  
  17.         HandlerExceptionResolver her =  
  18.                 context.getBean(HANDLER_EXCEPTION_RESOLVER_BEAN_NAME, HandlerExceptionResolver.class);  
  19.         this.handlerExceptionResolvers = Collections.singletonList(her);  
  20.     }  
  21.     catch (NoSuchBeanDefinitionException ex) {  
  22.         // Ignore, no HandlerExceptionResolver is fine too.  
  23.     }  
  24. }  
  25.   
  26. // Ensure we have at least some HandlerExceptionResolvers, by registering  
  27. // default HandlerExceptionResolvers if no other resolvers are found.  
  28. if (this.handlerExceptionResolvers == null) {  
  29.     this.handlerExceptionResolvers = getDefaultStrategies(context, HandlerExceptionResolver.class);  
  30.     if (logger.isDebugEnabled()) {  
  31.         logger.debug("No HandlerExceptionResolvers found in servlet '" + getServletName() + "': using default");  
  32.     }  
  33. }  
initRequestToViewNameTranslator是初始化到ViewName的处理器。
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.        /* 返回一个对应的视图名称*/  
  2. private void initRequestToViewNameTranslator(ApplicationContext context) {  
  3.     try {  
  4.         this.viewNameTranslator =  
  5.                 context.getBean(REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, RequestToViewNameTranslator.class);  
  6.         if (logger.isDebugEnabled()) {  
  7.             logger.debug("Using RequestToViewNameTranslator [" + this.viewNameTranslator + "]");  
  8.         }  
  9.     }  
  10.     catch (NoSuchBeanDefinitionException ex) {  
  11.         // We need to use the default.  
  12.         this.viewNameTranslator = getDefaultStrategy(context, RequestToViewNameTranslator.class);  
  13.         if (logger.isDebugEnabled()) {  
  14.             logger.debug("Unable to locate RequestToViewNameTranslator with name '" +  
  15.                     REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME + "': using default [" + this.viewNameTranslator +  
  16.                     "]");  
  17.         }  
  18.     }  
  19. }  
initViewResolvers初始化View的解析器
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.        //初始化viewResolver  
  2. private void initViewResolvers(ApplicationContext context) {  
  3.     this.viewResolvers = null;  
  4.   
  5.     if (this.detectAllViewResolvers) {  
  6.         // Find all ViewResolvers in the ApplicationContext, including ancestor contexts.  
  7.         Map<String, ViewResolver> matchingBeans =  
  8.                 BeanFactoryUtils.beansOfTypeIncludingAncestors(context, ViewResolver.classtruefalse);  
  9.         if (!matchingBeans.isEmpty()) {  
  10.             this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.values());  
  11.             // We keep ViewResolvers in sorted order.  
  12.             AnnotationAwareOrderComparator.sort(this.viewResolvers);  
  13.         }  
  14.     }  
  15.     else {  
  16.         try {  
  17.             ViewResolver vr = context.getBean(VIEW_RESOLVER_BEAN_NAME, ViewResolver.class);  
  18.             this.viewResolvers = Collections.singletonList(vr);  
  19.         }  
  20.         catch (NoSuchBeanDefinitionException ex) {  
  21.             // Ignore, we'll add a default ViewResolver later.  
  22.         }  
  23.     }  
  24.   
  25.     // Ensure we have at least one ViewResolver, by registering  
  26.     // a default ViewResolver if no other resolvers are found.  
  27.     if (this.viewResolvers == null) {  
  28.         this.viewResolvers = getDefaultStrategies(context, ViewResolver.class);  
  29.         if (logger.isDebugEnabled()) {  
  30.             logger.debug("No ViewResolvers found in servlet '" + getServletName() + "': using default");  
  31.         }  
  32.     }  
  33. }  
initFlashMapManager初始化FlashMapManager,与链接跳转相关的。
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.       //初始化 FlashMapManager  
  2. rivate void initFlashMapManager(ApplicationContext context) {  
  3. try {  
  4.     this.flashMapManager =  
  5.             context.getBean(FLASH_MAP_MANAGER_BEAN_NAME, FlashMapManager.class);  
  6.     if (logger.isDebugEnabled()) {  
  7.         logger.debug("Using FlashMapManager [" + this.flashMapManager + "]");  
  8.     }  
  9. }  
  10. catch (NoSuchBeanDefinitionException ex) {  
  11.     // We need to use the default.  
  12.     this.flashMapManager = getDefaultStrategy(context, FlashMapManager.class);  
  13.     if (logger.isDebugEnabled()) {  
  14.         logger.debug("Unable to locate FlashMapManager with name '" +  
  15.                 FLASH_MAP_MANAGER_BEAN_NAME + "': using default [" + this.flashMapManager + "]");  
  16.     }  
  17. }  

0 0
原创粉丝点击