说在前面

本次主要介绍springmvc配置解析。更多源码解析文章请关注“天河聊技术”微信公众号。

springmvc配置解析

接着上次,进入到这个方法org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser#parse这一行

//     获取返回值handler ->
      ManagedList<?> returnValueHandlers = getReturnValueHandlers(element, context);

进入到这个方法org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser#getReturnValueHandlers

private ManagedList<?> getReturnValueHandlers(Element element, ParserContext context) {
   Element handlers = DomUtils.getChildElementByTagName(element, "return-value-handlers");
   return (handlers != null ? extractBeanSubElements(handlers, context) : null);
}

往上返回到这个方法org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser#getAsyncTimeout

private String getAsyncTimeout(Element element) {
   Element asyncElement = DomUtils.getChildElementByTagName(element, "async-support");
   return (asyncElement != null ? asyncElement.getAttribute("default-timeout") : null);
}

往上返回到这个方法org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser#getAsyncExecutor

private RuntimeBeanReference getAsyncExecutor(Element element) {
//    注册异步执行器
      Element asyncElement = DomUtils.getChildElementByTagName(element, "async-support");
      if (asyncElement != null && asyncElement.hasAttribute("task-executor")) {
         return new RuntimeBeanReference(asyncElement.getAttribute("task-executor"));
      }
      return null;
   }

往上返回到这个方法org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser#getCallableInterceptors

private ManagedList<?> getCallableInterceptors(Element element, Object source, ParserContext context) {
      ManagedList<Object> interceptors = new ManagedList<Object>();
      Element asyncElement = DomUtils.getChildElementByTagName(element, "async-support");
      if (asyncElement != null) {
//       注册异步支持回调拦截器
         Element interceptorsElement = DomUtils.getChildElementByTagName(asyncElement, "callable-interceptors");
         if (interceptorsElement != null) {
            interceptors.setSource(source);
            for (Element converter : DomUtils.getChildElementsByTagName(interceptorsElement, "bean")) {
               BeanDefinitionHolder beanDef = context.getDelegate().parseBeanDefinitionElement(converter);
               beanDef = context.getDelegate().decorateBeanDefinitionIfRequired(converter, beanDef);
               interceptors.add(beanDef);
            }
         }
      }
      return interceptors;
   }

往上返回到这个方法org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser#getDeferredResultInterceptors

private ManagedList<?> getDeferredResultInterceptors(Element element, Object source, ParserContext context) {
      ManagedList<Object> interceptors = new ManagedList<Object>();
      Element asyncElement = DomUtils.getChildElementByTagName(element, "async-support");
      if (asyncElement != null) {
//       注册默认延迟结果拦截器
         Element interceptorsElement = DomUtils.getChildElementByTagName(asyncElement, "deferred-result-interceptors");
         if (interceptorsElement != null) {
            interceptors.setSource(source);
            for (Element converter : DomUtils.getChildElementsByTagName(interceptorsElement, "bean")) {
               BeanDefinitionHolder beanDef = context.getDelegate().parseBeanDefinitionElement(converter);
               beanDef = context.getDelegate().decorateBeanDefinitionIfRequired(converter, beanDef);
               interceptors.add(beanDef);
            }
         }
      }
      return interceptors;
   }

往上返回到这个方法org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser#addRequestBodyAdvice

protected void addRequestBodyAdvice(RootBeanDefinition beanDef) {
   if (jackson2Present) {
      beanDef.getPropertyValues().add("requestBodyAdvice",
            new RootBeanDefinition(JsonViewRequestBodyAdvice.class));
   }
}

往上返回到这个方法org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser#addResponseBodyAdvice

protected void addResponseBodyAdvice(RootBeanDefinition beanDef) {
   if (jackson2Present) {
      beanDef.getPropertyValues().add("responseBodyAdvice",
            new RootBeanDefinition(JsonViewResponseBodyAdvice.class));
   }
}

往上返回到这个方法org.springframework.web.servlet.config.MvcNamespaceUtils#registerDefaultComponents

public static void registerDefaultComponents(ParserContext parserContext, Object source) {
//    注册BeanNameUrlHandlerMapping bean定义 ->
      registerBeanNameUrlHandlerMapping(parserContext, source);
//    注册HttpRequestHandlerAdapter bean定义 ->
      registerHttpRequestHandlerAdapter(parserContext, source);
//    注册SimpleControllerHandlerAdapter bean定义 ->
      registerSimpleControllerHandlerAdapter(parserContext, source);
//    注册HandlerMappingIntrospector bean定义 ->
      registerHandlerMappingIntrospector(parserContext, source);
   }

进入到这个方法org.springframework.web.servlet.config.MvcNamespaceUtils#registerBeanNameUrlHandlerMapping

private static void registerBeanNameUrlHandlerMapping(ParserContext context, Object source) {
      if (!context.getRegistry().containsBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME)){
//       注册默认handleMapping组件bean定义
         RootBeanDefinition mappingDef = new RootBeanDefinition(BeanNameUrlHandlerMapping.class);
         mappingDef.setSource(source);
         mappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
         mappingDef.getPropertyValues().add("order", 2);    // consistent with WebMvcConfigurationSupport
//       注册跨域配置bean定义 ->
         RuntimeBeanReference corsRef = MvcNamespaceUtils.registerCorsConfigurations(null, context, source);
         mappingDef.getPropertyValues().add("corsConfigurations", corsRef);
         context.getRegistry().registerBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME, mappingDef);
         context.registerComponent(new BeanComponentDefinition(mappingDef, BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME));
      }
   }

进入到这个方法org.springframework.web.servlet.config.MvcNamespaceUtils#registerCorsConfigurations

public static RuntimeBeanReference registerCorsConfigurations(
         Map<String, CorsConfiguration> corsConfigurations, ParserContext context, Object source) {

//    mvcCorsConfigurations 跨域支持bean定义解析
      if (!context.getRegistry().containsBeanDefinition(CORS_CONFIGURATION_BEAN_NAME)) {
         RootBeanDefinition corsDef = new RootBeanDefinition(LinkedHashMap.class);
         corsDef.setSource(source);
         corsDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
         if (corsConfigurations != null) {
            corsDef.getConstructorArgumentValues().addIndexedArgumentValue(0, corsConfigurations);
         }
//       注册mvcCorsConfigurations bean定义
         context.getReaderContext().getRegistry().registerBeanDefinition(CORS_CONFIGURATION_BEAN_NAME, corsDef);
         context.registerComponent(new BeanComponentDefinition(corsDef, CORS_CONFIGURATION_BEAN_NAME));
      }
      else if (corsConfigurations != null) {
         BeanDefinition corsDef = context.getRegistry().getBeanDefinition(CORS_CONFIGURATION_BEAN_NAME);
         corsDef.getConstructorArgumentValues().addIndexedArgumentValue(0, corsConfigurations);
      }
      return new RuntimeBeanReference(CORS_CONFIGURATION_BEAN_NAME);
   }

往上返回到这个方法org.springframework.web.servlet.config.MvcNamespaceUtils#registerHttpRequestHandlerAdapter

private static void registerHttpRequestHandlerAdapter(ParserContext context, Object source) {
      if (!context.getRegistry().containsBeanDefinition(HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME)) {
//       注册默认handlerAdapter组件bean定义
         RootBeanDefinition adapterDef = new RootBeanDefinition(HttpRequestHandlerAdapter.class);
         adapterDef.setSource(source);
         adapterDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
         context.getRegistry().registerBeanDefinition(HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME, adapterDef);
         context.registerComponent(new BeanComponentDefinition(adapterDef, HTTP_REQUEST_HANDLER_ADAPTER_BEAN_NAME));
      }
   }

往上返回到这个方法org.springframework.web.servlet.config.MvcNamespaceUtils#registerSimpleControllerHandlerAdapter

private static void registerSimpleControllerHandlerAdapter(ParserContext context, Object source) {
      if (!context.getRegistry().containsBeanDefinition(SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME)) {
//       注册默认单一控制器handlerAdapter组件bean定义
         RootBeanDefinition beanDef = new RootBeanDefinition(SimpleControllerHandlerAdapter.class);
         beanDef.setSource(source);
         beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
         context.getRegistry().registerBeanDefinition(SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME, beanDef);
         context.registerComponent(new BeanComponentDefinition(beanDef, SIMPLE_CONTROLLER_HANDLER_ADAPTER_BEAN_NAME));
      }
   }

往上返回到这个方法org.springframework.web.servlet.config.MvcNamespaceUtils#registerHandlerMappingIntrospector

private static void registerHandlerMappingIntrospector(ParserContext parserContext, Object source) {
      if (!parserContext.getRegistry().containsBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)){
//       注册默认内省handlerMapping组件bean定义
         RootBeanDefinition beanDef = new RootBeanDefinition(HandlerMappingIntrospector.class);
         beanDef.setSource(source);
         beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
         beanDef.setLazyInit(true);
         parserContext.getRegistry().registerBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME, beanDef);
         parserContext.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME));
      }
   }

往上返回到这个方法org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser#parse

说到最后

本次源码解析仅代表个人观点,仅供参考。

03-14 13:17