Spring?MVC啟動之HandlerMapping作用及實現(xiàn)詳解
引言
在上一篇文章中,我們介紹了 Spring MVC 的啟動流程,接下來我們將發(fā)分多個篇章詳細介紹流程中的重點步驟
今天我們從 HandlerMapping 開始分析,HandlerMapping 是框架中的一個非常重要的組件。它的作用是將URL請求映射到合適的處理程序(Handler)上,這樣就能夠?qū)崿F(xiàn)控制器與頁面之間的交互。在 Spring MVC 中,有多種不同的 HandlerMapping 實現(xiàn),本文將對這些實現(xiàn)的作用和源碼原理進行詳細介紹。
作用
在 Spring MVC 框架中,HandlerMapping 是一個非常重要的組件,它的作用是將URL請求映射到相應的處理程序上。具體來說,HandlerMapping 會根據(jù)URL請求的路徑、請求參數(shù)等信息,確定需要執(zhí)行哪個處理程序,并將該處理程序返回給 DispatcherServlet。然后 DispatcherServlet 再將請求分配給相應的處理程序,處理程序處理完請求后,將結(jié)果返回給 DispatcherServlet,DispatcherServlet 再將結(jié)果返回給客戶端。
HandlerMapping的實現(xiàn)
Spring MVC 框架中有多種不同的 HandlerMapping 實現(xiàn),每種實現(xiàn)都有不同的作用和使用場景。下面將逐一介紹這些實現(xiàn)。
BeanNameUrlHandlerMapping
BeanNameUrlHandlerMapping 是 Spring MVC 框架中最簡單的 HandlerMapping 實現(xiàn),它的作用是將 URL 請求的路徑映射到 Bean 的名稱上。具體來說,當請求的路徑與一個 Bean 的名稱匹配時,BeanNameUrlHandlerMapping 會將該請求映射到對應的 Bean 上。
例如:
假設(shè)有一個名為 "/hello" 的請求
我們可以在 Spring 配置文件中定義一個名為 "helloController" 的 Bean
然后使用 BeanNameUrlHandlerMapping 將 "/hello" 請求映射到該 Bean 上
這樣,當客戶端發(fā)送 "/hello" 請求時,DispatcherServlet 就會將該請求分配給 "helloController" 處理。
BeanNameUrlHandlerMapping 的源碼比較簡單,它的核心代碼如下所示:
public class BeanNameUrlHandlerMapping extends AbstractUrlHandlerMapping { protected Object getHandlerInternal(HttpServletRequest request) throws Exception { String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); return obtainHandler(lookupPath, request); } }
從代碼中可以看出,BeanNameUrlHandlerMapping 實現(xiàn)了 AbstractUrlHandlerMapping 接口,并重寫了其中的 getHandlerInternal 方法。該方法的作用是根據(jù)請求的 URL 路徑查找相應的處理程序。
SimpleUrlHandlerMapping
SimpleUrlHandlerMapping 是 Spring MVC 框架中另一種常用的 HandlerMapping 實現(xiàn),它的作用是將 URL 請求的路徑映射到處理程序上。與 BeanNameUrlHandlerMapping 不同的是,SimpleUrlHandlerMapping 可以將一個URL請求映射到多個處理程序上。
例如:
假設(shè)有兩個請求 "/hello" 和 "/world"
我們可以使用 SimpleUrlHandlerMapping 將這兩個請求分別映射到不同的處理程序上。具體來說,我們可以在 Spring 配置文件中定義多個 Bean,并分別為它們設(shè)置不同的URL路徑。
然后使用 SimpleUrlHandlerMapping 將這些URL路徑與相應的處理程序進行映射。
這樣,當客戶端發(fā)送一個請求時,SimpleUrlHandlerMapping 就會根據(jù)請求的URL路徑查找相應的處理程序,并將請求分配給該處理程序處理。
SimpleUrlHandlerMapping 的源碼也比較簡單,它的核心代碼如下所示:
public class SimpleUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping { protected Object getHandlerInternal(HttpServletRequest request) throws Exception { String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); Object handler = lookupHandler(lookupPath, request); if (handler == null) { handler = getDefaultHandler(); } return handler; } }
從代碼中可以看出,SimpleUrlHandlerMapping 同樣實現(xiàn)了 AbstractUrlHandlerMapping 接口(在 AbstractDetectingUrlHandlerMapping 中繼承),并重寫了其中的 getHandlerInternal 方法。該方法的作用是根據(jù)請求的URL路徑查找相應的處理程序。
ControllerClassNameHandlerMapping
ControllerClassNameHandlerMapping 是Spring MVC 框架中一種比較特殊的 HandlerMapping 實現(xiàn),它的作用是將URL請求的路徑映射到 Controller 類名上。具體來說,ControllerClassNameHandlerMapping 會根據(jù)請求的URL路徑查找對應的 Controller 類名,并將該類返回給 DispatcherServlet,然后 DispatcherServlet 再將請求分配給該 Controller 類處理。
例如:
假設(shè)有一個名為 "/hello" 的請求
我們可以定義一個名為 HelloController的Controller 類
使用 ControllerClassNameHandlerMapping 將 "/hello" 請求映射到該類上。
這樣,當客戶端發(fā)送 "/hello" 請求時,ControllerClassNameHandlerMapping 就會將該請求映射到 HelloController 處理。
ControllerClassNameHandlerMapping 的源碼也比較簡單,它的核心代碼如下所示:
public class ControllerClassNameHandlerMapping extends AbstractControllerUrlHandlerMapping { protected Object getHandlerInternal(HttpServletRequest request) throws Exception { String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); String controllerClassName = getClassNameForUrlPath(lookupPath); return obtainApplicationContext().getBean(controllerClassName); } }
從代碼中可以看出,ControllerClassNameHandlerMapping 同樣實現(xiàn)了 AbstractUrlHandlerMapping 接口,并重寫了其中的 getHandlerInternal 方法。該方法的作用是根據(jù)請求的URL路徑查找相應的 Controller 類名,并將該類返回給 DispatcherServlet。
DefaultAnnotationHandlerMapping
DefaultAnnotationHandlerMapping 是Spring MVC 框架中另一種比較常用的 HandlerMapping實 現(xiàn),它的作用是將 URL 請求的路徑映射到標注了 @RequestMapping 注解的方法上。
具體來說,DefaultAnnotationHandlerMapping 會掃描應用程序中所有標注了 @RequestMapping 注解的方法,并將這些方法與相應的URL路徑進行映射。
然后當客戶端發(fā)送一個請求時,DefaultAnnotationHandlerMapping 就會根據(jù)請求的URL路徑查找相應的處理程序,并將該處理程序返回給 DispatcherServlet,然后 DispatcherServlet 再將請求分配給該處理程序處理。
例如:
假設(shè)有一個名為 "/hello" 的請求
我們可以在 Controller 類的某個方法上標注 @RequestMapping 注解
將該方法與 "/hello" 請求進行映射。
這樣,當客戶端發(fā)送 "/hello" 請求時,DefaultAnnotationHandlerMapping 就會將該請求映射到該方法上,然后將該方法返回給 DispatcherServlet 處理。
DefaultAnnotationHandlerMapping 的源碼比較復雜,因為它需要掃描應用程序中所有標注了 @RequestMapping 注解的方法。其核心代碼如下所示:
public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandlerMapping implements BeanFactoryAware, InitializingBean { private final List<RequestMappingInfoHandlerMapping> handlerMappings = new ArrayList<>(); protected Object getHandlerInternal(HttpServletRequest request) throws Exception { for (RequestMappingInfoHandlerMapping hm : this.handlerMappings) { HandlerExecutionChain handler = hm.getHandler(request); if (handler != null) { return handler; } } return null; } public void afterPropertiesSet() throws Exception { initHandlerMethods(); } protected void initHandlerMethods() { detectHandlerMethods(); if (logger.isInfoEnabled()) { logger.info("Mapped " + this.handlerMethods.size() + " request handler methods"); } } protected void detectHandlerMethods() { ... } }
從代碼中可以看出,DefaultAnnotationHandlerMapping 同樣實現(xiàn)了 AbstractUrlHandlerMapping 接口,并重寫了其中的 getHandlerInternal 方法。該方法的作用是根據(jù)請求的 URL 路徑查找相應的處理程序。
與其他 HandlerMapping 不同的是,DefaultAnnotationHandlerMapping 還實現(xiàn)了 BeanFactoryAware和InitializingBean 接口,以便在初始化時掃描應用程序中所有標注了 @RequestMapping 注解的方法。
具體來說,它會調(diào)用 detectHandlerMethods 方法,對應用程序中所有標注了 @RequestMapping 注解的方法進行掃描,并將這些方法與相應的URL路徑進行映射。
總結(jié)
在 Spring MVC 框架中,HandlerMapping 用于將URL請求的路徑映射到相應的處理程序上??蚣苤刑峁┝硕喾N不同的 HandlerMapping 實現(xiàn),包括 BeanNameUrlHandlerMapping、SimpleUrlHandlerMapping、ControllerClassNameHandlerMapping 和 DefaultAnnotationHandlerMapping 等。不同的HandlerMapping 實現(xiàn)方式適用于不同的應用場景。
- BeanNameUrlHandlerMapping 是最簡單的HandlerMapping實現(xiàn),它將 URL 請求的路徑直接映射到 Bean 的名稱上。
- SimpleUrlHandlerMapping 則將 URL 請求的路徑與相應的處理程序進行映射。
- ControllerClassNameHandlerMapping 將 URL 請求的路徑映射到 Controller 類名上。
- DefaultAnnotationHandlerMapping 則將 URL 請求的路徑映射到標注了 @RequestMapping 注解的方法上。
無論是哪種 HandlerMapping 實現(xiàn)方式,它們都遵循相同的流程來處理請求。具體來說,它們的流程如下:
客戶端發(fā)送請求到DispatcherServlet。
- DispatcherServlet根據(jù)請求的URL路徑選擇相應的HandlerMapping。
- HandlerMapping將請求的URL路徑與相應的處理程序進行映射。
- HandlerMapping返回相應的處理程序。
- DispatcherServlet將請求交給相應的處理程序進行處理。
- 處理程序進行業(yè)務邏輯的處理,并返回相應的結(jié)果。
- DispatcherServlet將處理程序的結(jié)果進行封裝,并返回給客戶端。
以上就是Spring MVC啟動之HandlerMapping作用及實現(xiàn)詳解的詳細內(nèi)容,更多關(guān)于Spring MVC啟動HandlerMapping的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot實現(xiàn)防重復提交和防重復點擊的示例
這篇文章主要介紹了springboot實現(xiàn)防重復提交和防重復點擊的示例,幫助大家更好的理解和學習springboot框架,感興趣的朋友可以了解下2020-09-09hadoop的hdfs文件操作實現(xiàn)上傳文件到hdfs
這篇文章主要介紹了使用hadoop的API對HDFS上的文件訪問,其中包括上傳文件到HDFS上、從HDFS上下載文件和刪除HDFS上的文件,需要的朋友可以參考下2014-03-03SpringMVC自定義參數(shù)綁定實現(xiàn)詳解
這篇文章主要介紹了SpringMVC自定義參數(shù)綁定實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11eclipse實現(xiàn)可認證的DH密鑰交換協(xié)議
這篇文章主要介紹了eclipse實現(xiàn)可認證的DH密鑰交換協(xié)議,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06