RequestMappingHandlerMapping詳解


我們先理簡單梳理一個關系

關系梳理

  1. spring ioc 是spring的核心,用來管理spring bean的生命周期
  2. MVC 是一種使用 MVC(Model View Controller 模型-視圖-控制器)設計創建 Web 應用程序的模式
  3. spring mvc 是spring的一個獨立的模塊,就像AOP一樣

在spring mvc中把web框架和spring ioc融合在一起,是通過ContextLoaderListener監聽servlet上下文的創建后來加載父容器完成的,然后通過配置一個servlet對象DispatcherServlet,在初始化DispatcherServlet時來加載具體子容器,詳細的可以參考spring ioc & web宿主 這篇文章

關於我們今天要講的RequestMappingHandlerMapping也是在DispatcherServlet的初始化過程中自動加載的,默認會自動加載所有實現HandlerMapping接口的bean,且我們可以通過serOrder來設置優先級,系統默認會加載RequestMappingHandlerMapping、BeanNameUrlHandlerMapping、SimpleUrlHandlerMapping 並且按照順序使用

private void initHandlerMappings(ApplicationContext context) { this.handlerMappings = null; if (this.detectAllHandlerMappings) { // Find all HandlerMappings in the ApplicationContext, including ancestor contexts. Map<String, HandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false); if (!matchingBeans.isEmpty()) { this.handlerMappings = new ArrayList<>(matchingBeans.values()); // We keep HandlerMappings in sorted order. AnnotationAwareOrderComparator.sort(this.handlerMappings); } } } 

RequestMappingHandlerMapping 加載過程

  1. RequestMappingHandlerMapping 實現了接口InitializingBean,在bean加載完成后會自動調用afterPropertiesSet方法,在此方法中調用了initHandlerMethods()來實現初始化
  2. 遍歷所有bean,如果bean實現帶有注解@Controller或者@RequestMapping 則進一步調用detectHandlerMethods處理,處理邏輯大致就是根據@RequestMapping配置的信息,構建RequestMappingInfo,然后注冊到MappingRegistry中
protected void initHandlerMethods() { String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(obtainApplicationContext(), Object.class) : obtainApplicationContext().getBeanNamesForType(Object.class)); for (String beanName : beanNames) { if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) { Class<?> beanType = null; beanType = obtainApplicationContext().getType(beanName); if (beanType != null && isHandler(beanType)) { detectHandlerMethods(beanName); } } } handlerMethodsInitialized(getHandlerMethods()); } 

protected void detectHandlerMethods(final Object handler) { Class<?> handlerType = (handler instanceof String ? obtainApplicationContext().getType((String) handler) : handler.getClass()); if (handlerType != null) { final Class<?> userType = ClassUtils.getUserClass(handlerType); Map<Method, T> methods = MethodIntrospector.selectMethods(userType, (MethodIntrospector.MetadataLookup<T>) method -> { try { return getMappingForMethod(method, userType); } catch (Throwable ex) { throw new IllegalStateException("Invalid mapping on handler class [" + userType.getName() + "]: " + method, ex); } }); methods.forEach((method, mapping) -> { Method invocableMethod = AopUtils.selectInvocableMethod(method, userType); registerHandlerMethod(handler, invocableMethod, mapping); }); } } 

RequestMappingHandlerMapping 解析過程

  1. 在DispatcherServlet中,根據請求對象調用getHander方法獲取HandlerExecutionChain對象
  2. 在getHander方法中也是遍歷上面默認加載的三個HandlerMapping,當然第一個就是RequestMappingHandlerMapping對象,調用其getHandler方法,根據請求path,找到一個最為匹配的HandlerMethod來處理請求
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { if (this.handlerMappings != null) { for (HandlerMapping hm : this.handlerMappings) { if (logger.isTraceEnabled()) { logger.trace( "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'"); } HandlerExecutionChain handler = hm.getHandler(request); if (handler != null) { return handler; } } } return null; } 
  1. 根據請求路徑獲取HandlerInterceptor,然后和上面獲得的HandlerMethod一起構成HandlerExecutionChain返回給DispatcherServlet

DispatcherServlet得到HandlerExecutionChain也就獲得了處理此次請求所需的Handler【即我們熟悉的Controller和對應的Action】,后續將會選擇合適HandlerAdapter來執行對應的Handler,獲取返回值,再根據返回值類型,進一步覺決定用什么方式展示給用戶



鏈接:https://www.jianshu.com/p/40eff6135311


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM