SpringMVC源码分析(4)剖析DispatcherServlet重要组件

本文主要总结DispatcherServlet几个重要组件的关系。

1.类图

该类图并没有全面的描述SpringMVC相关类,重点说明组件的关系。

该类图基于springmvc3.0.5版本。

很多类属性并没有完全罗列,进行了取舍。

基于DispatcherServlet,而且深度尽量适可而止,否则造成图复杂混乱。


2.HanlerMapping组件介绍

2.1 HanlerMapping类图

HanlerMapping是springmvc中完成url到controller映射的组件。

2.2 HanlerMappingAPI

public interface HandlerMapping {
    //根据选择策略,返回Handler
    HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
}

3.HandlerAdapter

3.1类图

是MVC framework SPI ;负责具体干活。如调用方法,参数解析等。

3.2 API

public interface HandlerAdapter {
   
   /**
            判断是否可以处理handler
    */
   boolean supports(Object handler); 
   
   /**
    * 处理请求
    */
   ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;

   /**
    * getLastModified
    */
   long getLastModified(HttpServletRequest request, Object handler);

}

4.HandlerExceptionResolver

4.1 HandlerExceptionResolver类图

专门处理异常情况,根据异常情况,构造ModelAndView

4.2 HandlerExceptionResolver API

public interface HandlerExceptionResolver {

   /**
    * 处理异常
    */
   ModelAndView resolveException(
         HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);

}
  1. ViewResolver

5.1 ViewResolver类图

作用:将string的视图名称解析为View类型的视图

5.2 ViewResolver API

public interface ViewResolver {

   /** 
    * Resolve the given view by name.
       解析视图
    */
   View resolveViewName(String viewName, Locale locale) throws Exception;

}

6.RequestToViewNameTranslator

默认实现是DefaultRequestToViewNameTranslator.主要作用是根据请求信息获取视图名称。

public interface RequestToViewNameTranslator {

   String getViewName(HttpServletRequest request) throws Exception;

}

7.LocaleResolver

7.1 LocaleResolver类图

从request解析Locale,实现本地化;一般使用在解析视图时使用;和使用国际化资源和主题时使用

7.2 LocaleResolver API

public interface LocaleResolver {

  /**
   * Resolve the current locale via the given request.
   * Should return a default locale as fallback in any case.
   * @param request the request to resolve the locale for
   * @return the current locale (never <code>null</code>)
   */
   Locale resolveLocale(HttpServletRequest request);

  /**
   * Set the current locale to the given one.
   * @param request the request to be used for locale modification
   * @param response the response to be used for locale modification
   * @param locale the new locale, or <code>null</code> to clear the locale
    * @throws UnsupportedOperationException if the LocaleResolver implementation
    * does not support dynamic changing of the theme
   */
   void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale);

}

8.ThemeResolver

8.1 ThemeResolver类图

主要用来设置主题。

8.2 API

public interface ThemeResolver {

  /**
   * Resolve the current theme name via the given request.
   * Should return a default theme as fallback in any case.
   * @param request request to be used for resolution
   * @return the current theme name
   */
   String resolveThemeName(HttpServletRequest request);

  /**
   * Set the current theme name to the given one.
   * @param request request to be used for theme name modification
   * @param response response to be used for theme name modification
   * @param themeName the new theme name
    * @throws UnsupportedOperationException if the ThemeResolver implementation
    * does not support dynamic changing of the theme
   */
   void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName);

}

9.MultipartResolver

默认实现为CommonsMultipartResolver,处理文件上传请求。

public interface MultipartResolver {

   /**
    * Determine if the given request contains multipart content.
    */
   boolean isMultipart(HttpServletRequest request);

   /**
    * Parse the given HTTP request into multipart files and parameters,
    * and wrap the request inside a
    */
   MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;

   /**
    * Cleanup any resources used for the multipart handling,
    */
   void cleanupMultipart(MultipartHttpServletRequest request);

}

10 总结。

简单介绍了下各个组件,每个组件都各司其职,互相配合。才有了强大的SpringMVC功能。在以后的文章里,针对每个组件,会结合具体例子,由浅入深式的进行剖析。

另外一个重要的核心部分是HandlerMethodInvoker,涉及的东西比较多,非常有看头。

欢迎工作一到五年的Java工程师朋友们加入Java高并发: 957734884,

群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、

Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx

等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己

思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值