SpringMVC源码系列:HandlerMapping

HandlerMapping接口是用来查找Handler的。在SpringMvc中,DispatcherServlet处理分发不少请求,而每一个请求都须要一个Handler来处理,具体接受到一个请求后使用哪一个Handler来处理呢?这就是Handler要作的事情。所以,HandlerMapping的做用就是根据request找到相应的处理器Handler和Interceptors。ios

下面是Spring中对HandlerMapping接口的说明:bash

This class can be implemented by application developers, although this is not necessary, as BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping are included in the framework. The former is the default if no HandlerMapping bean is registered in the application context.
这个类能够由应用程序开发人员实现,尽管这不是必须的,由于BeanNameUrlHandlerMapping和DefaultAnnotationHandlerMapping已经包含在框架中,做为HandlerMapping的默认实现。 若是在应用程序上下文中没有注册HandlerMapping bean,BeanNameUrlHandlerMapping是默认值。cookie

HandlerMapping implementations can support mapped interceptors but do not have to. A handler will always be wrapped in a HandlerExecutionChain instance, optionally accompanied by some HandlerInterceptor instances.The DispatcherServlet will first call each HandlerInterceptor's preHandle method in the given order, finally invoking the handler itself if all preHandle methods have returned true
HandlerMapping实现能够支持映射的拦截器,但没必要如此;handler将始终被封装在HandlerExecutionChain实例中,并可由一些HandlerInterceptor实例执行。在给定的顺序中,DispatcherServlet将首先调用每一个HandlerInterceptor的preHandle方法,若是全部的preHandle方法都返回true,那么最后调用handler自己。session

The ability to parameterize this mapping is a powerful and unusual capability of this MVC framework. For example, it is possible to write a custom mapping based on session state, cookie state or many other variables. No other MVC framework seems to be equally flexible.
参数化这个映射的能力是这个MVC框架的一个强大且不一样寻常的能力。 例如,能够根据会话状态,cookie状态或许多其余变量编写自定义映射。 没有其余MVC框架彷佛一样灵活。app

Note: Implementations can implement the Ordered interface to be able to specify a sorting order and thus a priority for getting applied by DispatcherServlet. Non-Ordered instances get treated as lowest priority.
注:实现能够实现Ordered接口,以便可以指定排序顺序,从而指定由DispatcherServlet应用的优先级。 无序实例被视为最低优先级。框架

1.接口常量

1.一、PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE

HttpServletRequest属性的名称,它包含处理程序映射中的路径,好比模式匹配,或者彻底相关的URI(一般在DispatcherServlet的映射中)。此属性不须要全部HandlerMapping实现支持。基于url的HandlerMappings一般会支持它,可是处理程序不该该指望这个请求属性在全部场景中都存在。post

/**
 * Name of the {@link HttpServletRequest} attribute that contains the path
 * within the handler mapping, in case of a pattern match, or the full
 * relevant URI (typically within the DispatcherServlet's mapping) else. * <p>Note: This attribute is not required to be supported by all * HandlerMapping implementations. URL-based HandlerMappings will * typically support it, but handlers should not necessarily expect * this request attribute to be present in all scenarios. */ String PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE = HandlerMapping.class.getName() + ".pathWithinHandlerMapping"; 复制代码

1.二、BEST_MATCHING_PATTERN_ATTRIBUTE

HttpServletRequest属性的名称,包括处理程序映射中的最佳匹配模式学习

/**
 * Name of the {@link HttpServletRequest} attribute that contains the
 * best matching pattern within the handler mapping.
 * <p>Note: This attribute is not required to be supported by all
 * HandlerMapping implementations. URL-based HandlerMappings will
 * typically support it, but handlers should not necessarily expect
 * this request attribute to be present in all scenarios.
 */
String BEST_MATCHING_PATTERN_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingPattern";
复制代码

1.三、INTROSPECT_TYPE_LEVEL_MAPPING

HttpServletRequest属性的名称,指示是否应该检查类型级别的映射。flex

/**
 * Name of the boolean {@link HttpServletRequest} attribute that indicates
 * whether type-level mappings should be inspected.
 * <p>Note: This attribute is not required to be supported by all
 * HandlerMapping implementations.
 */
String INTROSPECT_TYPE_LEVEL_MAPPING = HandlerMapping.class.getName() + ".introspectTypeLevelMapping";
复制代码

1.四、URI_TEMPLATE_VARIABLES_ATTRIBUTE

包含URI模板映射的HttpServletRequest属性的名称,将变量名称映射到值。ui

/**
 * Name of the {@link HttpServletRequest} attribute that contains the URI
 * templates map, mapping variable names to values.
 * <p>Note: This attribute is not required to be supported by all
 * HandlerMapping implementations. URL-based HandlerMappings will
 * typically support it, but handlers should not necessarily expect
 * this request attribute to be present in all scenarios.
 */
String URI_TEMPLATE_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".uriTemplateVariables";
复制代码

1.五、MATRIX_VARIABLES_ATTRIBUTE

包含带有URI矩阵变量的映射的HttpServletRequest属性的名称。此属性不须要全部HandlerMapping实现支持,也可能不存在,这取决于HandlerMapping是否被配置为在请求URI中保留矩阵变量内容。

/**
 * Name of the {@link HttpServletRequest} attribute that contains a map with
 * URI matrix variables.
 * <p>Note: This attribute is not required to be supported by all
 * HandlerMapping implementations and may also not be present depending on
 * whether the HandlerMapping is configured to keep matrix variable content
 * in the request URI.
 */
String MATRIX_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".matrixVariables";
复制代码

1.六、PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE

HttpServletRequest属性的名称,该属性包含可用于映射处理程序的可生成的MediaTypes集合。

/**
 * Name of the {@link HttpServletRequest} attribute that contains the set of
 * producible MediaTypes applicable to the mapped handler.
 * <p>Note: This attribute is not required to be supported by all
 * HandlerMapping implementations. Handlers should not necessarily expect
 * this request attribute to be present in all scenarios.
 */
String PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE = HandlerMapping.class.getName() + ".producibleMediaTypes";
复制代码

2.核心方法

HandlerMapping接口中只有一个方法,以下:

HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
复制代码

从方法定义能够看出,getHandler方法就是经过request来获取一个HandlerExecutionChain;该方法在不一样的子类中都有实现,具体的实现后面说子类的时候在详细分析。

3.HandlerMapping的子类

图中黄色部分表示已通过时的类,时间开发中不建议再使用。

在HandlerMapping的体系中能够看出,HandlerMapping下属子类可分为两个分支;

  • AbstractHandlerMethodMapping
  • AbstractUrlHandlerMapping

上述两个抽象类又均是AbstractHandlerMapping的子类。关于AbstractHandlerMapping咱们下篇文章来学习。

你们若是有什么意见或者建议能够在下方评论区留言,也能够给咱们发邮件(glmapper_2018@163.com)!欢迎小伙伴与咱们一块儿交流,一块儿成长。

相关文章
相关标签/搜索