[SpringMVC source code analysis] The secret of HandlerMethodArgumentResolver parameter injection

 Key interface

HandlerMethodArgumentResolver (processor method parameter resolver)

 

Its implementation class is as follows:

Commonly used ones are:

  • ServletRequestMethodArgumentResolver and ServletResponseMethodArgumentResolver handle the automatic binding of HttpServletRequest and HttpServletResponse
  • RequestParamMapMethodArgumentResolver handles @RequestParam
  • RequestHeaderMapMethodArgumentResolver handles @RequestHeader
  • PathVariableMapMethodArgumentResolver handles @PathVariable
  • ModelAttributeMethodProcessor processed @ModelAttribute
  • RequestResponseBodyMethodProcessor processed @RequestBody

 

Source code of its calling process

Springmvc workflow source code reference: https://blog.csdn.net/sumengnan/article/details/105381225

1. Start to get the RequestMappingHandlerAdapter adapter class directly, and then call the handle method to execute our handler:

 

2. Use the AbstractHandlerMethodAdapter class to force the handler into the HandlerMethod class, and then call the subclass RequestMappingHandlerAdapter:

 

3. Continue to call invokeHandlerMethod

Since the argumentResolvers property of RequestMappingHandlerAdapter will be used below, let's talk about it first:

The RequestMappingHandlerAdapter class implements the InitializingBean interface. If you know the bean life cycle, you know that the afterPropertiesSet method will be executed when the bean is instantiated, as shown in the figure:

Execute the getDefaultArgumentResolvers method, initialize the following method parameter parser, and initialize the binding parser and return value processor.

Then create the HandlerMethodArgumentResolverComposite object and add these resolvers.

 

4. Here, the HandlerMethod is transformed into the ServletInvocableHandlerMethod class through the createInvocableHandlerMethod method.

Explanation:

The actual ServletInvocableHandlerMethod is a subclass of HandlerMethod, as shown in the figure:

HandlerMethodArgumentResolverComposite is an intermediary for all calls to HandlerMethodArgumentResolver implementation classes.

That is, the HandlerMethodArgumentResolver implementation class is called by it. Similar to proxy mode

 

5、进入invokeAndHandle

 

6. In the invokeForRequest method, the method parameter values ​​are actually obtained, and then the handler is called through the doInvoke method

 

7. Now the main concern is the problem of parameters, so enter the getMethodArgumentValues ​​method and start parsing the parameters.

The for loop is parsed one by one in the order of the parameters.

 

8. Since the HandlerMethodArgumentResolverComposite object has been instantiated in the third step above, the supportsParameter and resolveArgument methods of this object will be called.

Then the corresponding parser class is called by HandlerMethodArgumentResolverComposite. Determine whether this parser is available through the supportsParameter method of the parser

 

9. Take an example: For example, the current handler parameter uses the @RequestParam annotation. The supportsParameter method determines that this implementation class can be used to parse this parameter.

It will execute the resolveArgument method for processing.

10. Finish

 

 

what's the effect?

We can customize annotations to parse the parameters.

For example: customize the @LoginUser annotation to get the currently logged-in user directly on the parameter, is it much more convenient!

Reference: https://blog.csdn.net/sumengnan/article/details/102852658

 

 

 

Guess you like

Origin blog.csdn.net/sumengnan/article/details/113774179
Recommended