SpringMVC source code parsing, springmvc source code

Source: Internet
Author: User

SpringMVC source code parsing, springmvc source code

HandlerMethodArgumentResolver is mainly responsible for the preparation of parameters before handler execution.

Let's look at an example. The id of the red part is initialized, And the fill value is what it does:

1     @RequestMapping(value = "/{id}")2     @ResponseBody3     public String getBook(@PathVariable("id") String id) {4         Hello helloProxy = new HelloProxy();5         helloProxy.say("Jack");6         7         return id;8     }

 

Analysis directory:

1. Interface Definition (whether parameters are supported and processing parameters)

2. The spokesman HandlerMethodArgumentResolverComposite encapsulates Other implementations

3. Specific implementation

Except for composite, HandlerMethodReturnValueHandle can be divided into XXXMethodArgumentResolver (not implemented) and XXXMethodProssor (implemented) based on whether HandlerMethodReturnValueHandle is implemented)

HandlerMethodReturnValueHandle is used to process the return value.

There are too many implementation classes. For more information, see the following.

    

 

1. Interface Definition

The old rule is to look at the interface first. It seems very simple: whether or not it is supported and how it is processed.

1 package org. springframework. web. method. support; 2 public interface HandlerMethodArgumentResolver {3 4/** 5 * Whether the parser supports parameters 6 */7 boolean supportsParameter (MethodParameter parameter); 8 9/** 10 * Resolution parameters, fill in the parameter value 11 */12 Object resolveArgument (MethodParameter parameter, 13 ModelAndViewContainer mavContainer, 14 NativeWebRequest webRequest, 15 WebDataBinderFactory binderFactory) throws Exception; 16 17}

 

2. The spokesman HandlerMethodArgumentResolverComposite encapsulates Other implementations

By encapsulating Other implementations, the DNS service is provided externally. In this way, you do not have to worry about how to parse the parameters during use. You only need to call suppsparameter and resolveArgument.

If you want to expand the supported types, you can also easily add implementation classes and register them with addResolver.

I can see how this reminds me of the design mode's open and closed principle. It is open to extensions and closed to modifications.

Aspect code:

Encapsulate Other implementations and provide registration functions

1 package org. springframework. web. method. support; 2 public class implements detail {3 private final List <strong> argumentResolvers = 4 new Entity List <strong> (); 5 public entity addResolver (HandlerMethodArgumentResolver argumentResolver) {6 this. argumentResolv Ers. add (argumentResolver); 7 return this; 8} 9 10 public HandlerMethodArgumentResolverComposite addResolvers (11 List <? Extends HandlerMethodArgumentResolver> argumentResolvers) {12 if (argumentResolvers! = Null) {13 for (HandlerMethodArgumentResolver resolver: argumentResolvers) {14 this. argumentResolvers. add (resolver); // It is rare that addResolver15} 16} 17 return this; 18}

Let's take a look at how the request is handled:

All are implemented through private HandlerMethodArgumentResolver getArgumentResolver (MethodParameter parameter) to find the resolver.

First use parameter as the key to get in the cache

If no reoslevers can be obtained, supportsParameter is executed one by one until there is a true reoslevers.

Consistent with the definition of the GOF responsibility chain design model.

 1 package org.springframework.web.method.support; 2 public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentResolver { 3     public boolean supportsParameter(MethodParameter parameter) { 4         return getArgumentResolver(parameter) != null; 5     } 6     public Object resolveArgument( 7             MethodParameter parameter, ModelAndViewContainer mavContainer, 8             NativeWebRequest webRequest, WebDataBinderFactory binderFactory) 9             throws Exception {10 11         HandlerMethodArgumentResolver resolver = getArgumentResolver(parameter);12         return resolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);13     }14     private HandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) {15         HandlerMethodArgumentResolver result = this.argumentResolverCache.get(parameter);16         if (result == null) {17             for (HandlerMethodArgumentResolver methodArgumentResolver : this.argumentResolvers) {18                 if (methodArgumentResolver.supportsParameter(parameter)) {19                     result = methodArgumentResolver;20                     this.argumentResolverCache.put(parameter, result);21                     break;22                 }23             }24         }25         return result;26     }27 }

 

3. Specific implementation

There are too many. Let's take a look at the responsibilities of each class first. The specific analysis will be carried out in debug later.

AbstractMessageConverterMethodArgumentResolver uses HttpMessageConverter to parse the base class from the request body parameter value.

-- AbstractMessageConverterMethodProcessor adds the return value for processing, so it is renamed

---- HttpEntityMethodProcessor only processes HttpEntity when parsing parameters. HttpEntity and ResponseEntity are supported when processing return values.

---- RequestResponseBodyMethodProcessor use HttpMessageConverter to parse the @ RequestBody parameter; process the return value using @ ResponseBody

-- RequestPartMethodArgumentResolver: parses parameters annotated with @ RequestPart and @ RequestParam. Subclass parameters of MultipartFile and Part

AbstractNamedValueMethodArgumentResolver parses named vlaue type parameters (actually Key-value pairs ).

-- AbstractCookieValueMethodArgumentResolver parses parameters annotated with @ CookieValue

---- ServletCookieValueMethodArgumentResolver parses the cookie value from HttpServletRequest

-- ExpressionValueMethodArgumentResolver parses the @ Value annotation Parameter

-- MatrixVariableMethodArgumentResolver: The parameter parsed by @ MatrixVariable annotation, excluding the map type.

-- PathVariableMethodArgumentResolver: parses parameters annotated with @ PathVariable from the uri.

-- RequestHeaderMethodArgumentResolver: parses parameters annotated with @ RequestHeader, excluding the map type.

-- RequestParamMethodArgumentResolver parses parameters that obtain values from the request stream, such as @ RequestParam, MultipartFile, and Part. By default, basic parameters are parsed.

AbstractWebArgumentResolverAdapter uses the base class of @ WebArgumentResolver for backward compatibility. It is applicable to WebArgumentResolver.

-- ServletWebArgumentResolverAdapter create NativeWebRequest --

ErrorsMethodArgumentResolver processes Errors subclass Parameters

MapMethodProcessor processes map Parameters

MatrixVariableMapMethodArgumentResolver uses the map type parameter annotated by @ MatrixVariable

ModelAttributeMethodProcessor: @ ModelAttribute annotation parameter for parsing

-- ServletModelAttributeMethodProcessor uses ServletRequestDataBinder to parse parameters

ModelMethodProcessor Processes model Parameters

PathVariableMapMethodArgumentResolver processes map parameters annotated with @ PathVariable

RedirectAttributesMethodArgumentResolver processes RedirectAttributes type parameters

RequestHeaderMapMethodArgumentResolver processes the map parameters annotated with @ RequestHeader

RequestParamMapMethodArgumentResolver processes the map parameters annotated with @ RequestParam

ServletRequestMethodArgumentResolver processes request-related parameters: WebRequest, ServletRequest, MultipartRequest, HttpSession, Principal, Locale, InputStream, Reader

ServletResponseMethodArgumentResolver parses response-related parameters: ServletResponse, OutputStream, Writer

SessionStatusMethodArgumentResolver processes SessionStatus Parameters

UriComponentsBuilderMethodArgumentResolver processes UriComponentsBuilder type parameters

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.