RequestMappingHandlerMapping請求地址對映的初始化流程!

2022-12-14 06:00:30

之前的文章裡,介紹了DispatcherSerlvet處理請求的流程。
其中一個核心的步驟是:請求地址對映,即根據request獲取對應的HandlerExcecutionChain
為了後續的請求地址對映,在專案初始化時,需要先將request-handler對映關係快取起來。
HandlerMapping有很多實現類,比如RequestMappingHandlerMappingBeanNameUrlHandlerMappingRouterFunctionMapping,它們分別對應不同的Controller介面定義規則。
這篇文章要介紹的是RequestMappingHandlerMapping請求地址對映的初始化流程。

大家看到RequestMappingHandlerMapping可能會感到陌生。
實際上,它是我們日常打交道最多的HandlerMapping實現類:它是@Controller@RequestMapping的底層實現。
RequestMappingHanlderMapping初始化時,會根據@Controller@RequestMapping建立RequestMappingInfo,將request-handler對映關係快取起來。

首先,我們簡單來看一下RequestMappingHandlerMapping的類圖:

RequestMappingHandlerMapping實現了InitializingBean介面。
在Spring容器設定完所有bean的屬性,以及執行完XxxAware介面的setXxx()方法後,會觸發InitializingBeanafterPropertiesSet()方法。
AbstractHandlerMethodMappingafterPropertiesSet()方法中,會完成請求地址對映的初始化流程:

public void afterPropertiesSet() {  
   initHandlerMethods();  
}

AbstractHandlerMethodMappinginitHandlerMethods方法中,會遍歷容器中所有bean進行處理:

protected void initHandlerMethods() {  
    // 1、遍歷所有bean的名稱
   for (String beanName : getCandidateBeanNames()) {  
      if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {  
        // 2、解析bean
         processCandidateBean(beanName);  
      }  
   }  
   handlerMethodsInitialized(getHandlerMethods());  
}

AbstractHandlerMethodMappingprocessCandidateBean方法中,會對bean進行篩選。如果該bean的類物件中包含@ControllerRequestMapping註解,會進一步遍歷該類物件的各個方法:

protected void processCandidateBean(String beanName) {  
   Class<?> beanType = null;  
   try {  
      beanType = obtainApplicationContext().getType(beanName);  
   }  
   catch (Throwable ex) {  
      // An unresolvable bean type, probably from a lazy bean - let's ignore it.  
      if (logger.isTraceEnabled()) {  
         logger.trace("Could not resolve type for bean '" + beanName + "'", ex);  
      }  
   }  
   // 1、判斷bean的類物件是否包含@Controller或@RequestMapping
   if (beanType != null && isHandler(beanType)) {  
      // 2、構造request-handler對映資訊
      detectHandlerMethods(beanName);  
   }  
}

RequestMappingHandlerMappingisHandler()方法中,會判斷當前類物件是否包含@Controller@RequestMapping註解:

protected boolean isHandler(Class<?> beanType) {  
   return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||  
         AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));  
}

AbstractHandlerMethodMappingdetectHandlerMethods方法中,會構造並快取request-handler資訊:

protected void detectHandlerMethods(Object handler) {  
   Class<?> handlerType = (handler instanceof String ?  
         obtainApplicationContext().getType((String) handler) : handler.getClass());  
  
   if (handlerType != null) {  
      Class<?> userType = ClassUtils.getUserClass(handlerType);  
      // 1、遍歷類物件的各個方法,返回Method-RequestMappingInfo對映
      Map<Method, T> methods = MethodIntrospector.selectMethods(userType,  
            (MethodIntrospector.MetadataLookup<T>) method -> {  
               try {  
	               // 2、構造request-handler請求地址對映
                  return getMappingForMethod(method, userType);  
               }  
               catch (Throwable ex) {  
                  throw new IllegalStateException("Invalid mapping on handler class [" +  
                        userType.getName() + "]: " + method, ex);  
               }  
            });  
      if (logger.isTraceEnabled()) {  
         logger.trace(formatMappings(userType, methods));  
      }  
      else if (mappingsLogger.isDebugEnabled()) {  
         mappingsLogger.debug(formatMappings(userType, methods));  
      }  
      // 3、快取request-handler請求地址對映
      methods.forEach((method, mapping) -> {  
         Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);  
         registerHandlerMethod(handler, invocableMethod, mapping);  
      });  
   }  
}

MethodIntrospectorselectMethods()方法中,會遍歷類物件各個方法,呼叫RequestMappingHandlerMappinggetMappingForMethod()方法,構造request地址資訊:

  • 如果該方法滿足書寫規則,即含有@RequestMapping,會返回RequestMappingInfo物件
  • 如果該方法不滿足書寫規則,會返回null

MethodIntrospectorselectMethods()方法會將所有request地址資訊不為nullMethod-RequestMappingInfo對映返回。

RequestMappingHandlerMappinggetMappingForMethod()方法中,會構造完整的request地址資訊。主要包括以下步驟:

  1. 構造方法級別的request地址資訊
  2. 構造類級別的request地址資訊
  3. 整合兩個級別的request地址資訊,構造出完整的request地址資訊

RequestMappingHandlerMappinggetMappingForMethod()方法原始碼如下:

protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
	// 1、構造方法級別的request-handler資訊
   RequestMappingInfo info = createRequestMappingInfo(method);  
   if (info != null) {  
	   // 2、構造類級別的request-handler資訊
      RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType);  
      if (typeInfo != null) {  
	      // 3、整合兩個級別的request-handler資訊,構造出完整的request-handler資訊
         info = typeInfo.combine(info);  
      }  
      String prefix = getPathPrefix(handlerType);  
      if (prefix != null) {  
         info = RequestMappingInfo.paths(prefix).options(this.config).build().combine(info);  
      }  
   }  
   return info;  
}

構造request地址資訊很簡單,只是從@RequestMapping註解中獲取各個屬性,建立RequestMappingInfo(在實際請求地址對映時,會對所有屬性進行校驗):

protected RequestMappingInfo createRequestMappingInfo(  
      RequestMapping requestMapping, @Nullable RequestCondition<?> customCondition) {  
   RequestMappingInfo.Builder builder = RequestMappingInfo  
         .paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))  
         .methods(requestMapping.method())  
         .params(requestMapping.params())  
         .headers(requestMapping.headers())  
         .consumes(requestMapping.consumes())  
         .produces(requestMapping.produces())  
         .mappingName(requestMapping.name());  
   if (customCondition != null) {  
      builder.customCondition(customCondition);  
   }  
   return builder.options(this.config).build();  
}

在整合request地址資訊過程中,會分別呼叫各個屬性的整合規則進行整合:

public RequestMappingInfo combine(RequestMappingInfo other) {  
   String name = combineNames(other);  
  
   PathPatternsRequestCondition pathPatterns =  
         (this.pathPatternsCondition != null && other.pathPatternsCondition != null ?  
               this.pathPatternsCondition.combine(other.pathPatternsCondition) : null);  
  
   PatternsRequestCondition patterns =  
         (this.patternsCondition != null && other.patternsCondition != null ?  
               this.patternsCondition.combine(other.patternsCondition) : null);  
  
   RequestMethodsRequestCondition methods = this.methodsCondition.combine(other.methodsCondition);  
   ParamsRequestCondition params = this.paramsCondition.combine(other.paramsCondition);  
   HeadersRequestCondition headers = this.headersCondition.combine(other.headersCondition);  
   ConsumesRequestCondition consumes = this.consumesCondition.combine(other.consumesCondition);  
   ProducesRequestCondition produces = this.producesCondition.combine(other.producesCondition);  
   RequestConditionHolder custom = this.customConditionHolder.combine(other.customConditionHolder);  
  
   return new RequestMappingInfo(name, pathPatterns, patterns,  
         methods, params, headers, consumes, produces, custom, this.options);  
}

不同的屬性有不同的整合規則,比如對於methodsparamsheaders會取並集,而對於consumesproduces方法級別優先。

介紹完request地址資訊的構造過程,我們回到AbstractHandlerMethodMappingdetectHandlerMethods方法中。此時,我們得到了Method-RequestMappingInfo對映資訊。

接下來,會遍歷這個對映,篩選出實際可執行的方法(即非私有的、非靜態的和非超類的)。

最終,將可執行的方法對應的request-handler資訊快取起來。核心程式碼位於AbstractHandlerMethodMapping.MappingRegistry內部類的register()方法:

public void register(T mapping, Object handler, Method method) {  
   this.readWriteLock.writeLock().lock();  
   try {  
	   // 1、建立HandlerMethod物件,即handler
      HandlerMethod handlerMethod = createHandlerMethod(handler, method);  
      // 2、校驗該request地址資訊是否已經存在
      validateMethodMapping(handlerMethod, mapping);  
		// 3、快取path-RequestMappingInfo對映
      Set<String> directPaths = AbstractHandlerMethodMapping.this.getDirectPaths(mapping);  
      for (String path : directPaths) {  
         this.pathLookup.add(path, mapping);  
      }  
		// 4、快取name-RequestMappingInfo對映
      String name = null;  
      if (getNamingStrategy() != null) {  
         name = getNamingStrategy().getName(handlerMethod, mapping);  
         addMappingName(name, handlerMethod);  
      }  
		// 5、快取CORS設定資訊
      CorsConfiguration corsConfig = initCorsConfiguration(handler, method, mapping);  
      if (corsConfig != null) {  
         corsConfig.validateAllowCredentials();  
         this.corsLookup.put(handlerMethod, corsConfig);  
      }  
		// 6、快取RequestMappingInfo-MappingRegistration資訊
      this.registry.put(mapping,  
            new MappingRegistration<>(mapping, handlerMethod, directPaths, name, corsConfig != null));  
   }  
   finally {  
      this.readWriteLock.writeLock().unlock();  
   }  
}

需要注意的是,在這個過程中還會快取跨域設定資訊,主要是@CrossOrigin註解方式的跨域設定資訊。
RequestMappingHandlerMappinginitCorsConfiguration()方法中,會獲取類級別和方法級別的@CrossOrigin資訊,構造出完整的跨域設定資訊:

protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {  
   HandlerMethod handlerMethod = createHandlerMethod(handler, method);  
   Class<?> beanType = handlerMethod.getBeanType();  
   // 1、獲取類級別的@CrossOrigin資訊
   CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(beanType, CrossOrigin.class);  
   // 2、獲取方法級別的@CrossOrigin資訊
   CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);  
  
   if (typeAnnotation == null && methodAnnotation == null) {  
      return null;  
   }  
	// 3、整合兩個級別的@CrossOrigin資訊
   CorsConfiguration config = new CorsConfiguration();  
   updateCorsConfig(config, typeAnnotation);  
   updateCorsConfig(config, methodAnnotation);  
  
   if (CollectionUtils.isEmpty(config.getAllowedMethods())) {  
      for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {  
         config.addAllowedMethod(allowedMethod.name());  
      }  
   }  
   return config.applyPermitDefaultValues();  
}

在整合@CrossOrigin資訊過程中,有三種情況:

  1. 對於originsoriginPatternsallowedHeadersexposedHeadersmethods等列表屬性,會獲取全部。
  2. 對於allowCredentials,會優先獲取方法級別的設定。
  3. 對於maxAge,會獲取最大值。

至此,我們走完了RequestMappingHandlerMapping中請求地址對映的初始化流程。最後總結一下流程如下:

  1. 遍歷容器中所有bean物件
  2. 如果bean的類物件含有@Controller@RequestMapping註解,進行下一步
  3. 遍歷bean的類物件的所有方法,根據方法的@RequestMapping註解,構造RequestMappingInfo物件
  4. 遍歷Method-RequestMappingInfo對映,過濾出可執行方法
  5. 快取各種request-handler對映資訊,同時會快取@CrossOrigin的跨域設定資訊

此時,我們可以充分理解到,request-handler請求地址對映資訊中requesthandler的含義:

  • request:主要是@RequestMapping中含有的各個屬性的資訊
  • handler:標註@RequestMapping的方法