SpringMVC原始碼系列:HandlerMapping

glmapper發表於2018-01-11

HandlerMapping介面是用來查詢Handler的。在SpringMvc中,DispatcherServlet處理分發很多請求,而每個請求都需要一個Handler來處理,具體接受到一個請求後使用哪個Handler來處理呢?這就是Handler要做的事情。因此,HandlerMapping的作用就是根據request找到相應的處理器Handler和Interceptors。

下面是Spring中對HandlerMapping介面的說明:

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是預設值。

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本身。

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框架似乎同樣靈活。

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.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.2、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.3、INTROSPECT_TYPE_LEVEL_MAPPING

HttpServletRequest屬性的名稱,指示是否應該檢查型別級別的對映。

/**
 * 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.4、URI_TEMPLATE_VARIABLES_ATTRIBUTE

包含URI模板對映的HttpServletRequest屬性的名稱,將變數名稱對映到值。

/**
 * 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.5、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.6、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的子類

SpringMVC原始碼系列:HandlerMapping
圖中黃色部分表示已經過時的類,時間開發中不建議再使用。

在HandlerMapping的體系中可以看出,HandlerMapping下屬子類可分為兩個分支;

  • AbstractHandlerMethodMapping
  • AbstractUrlHandlerMapping

上述兩個抽象類又均是AbstractHandlerMapping的子類。關於AbstractHandlerMapping我們下篇文章來學習。

大家如果有什麼意見或者建議可以在下方評論區留言,也可以給我們發郵件(glmapper_2018@163.com)!歡迎小夥伴與我們一起交流,一起成長。

相關文章