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一般會支持它,可是處理程序不該該指望這個請求屬性在全部場景中都存在。學習

/**
 * 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屬性的名稱,包括處理程序映射中的最佳匹配模式flex

/**
 * 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屬性的名稱,指示是否應該檢查類型級別的映射。ui

/**
 * 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屬性的名稱,將變量名稱映射到值。this

/**
 * 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)!歡迎小夥伴與咱們一塊兒交流,一塊兒成長。

相關文章
相關標籤/搜索