1.入口到参数处理调用流程
入口为spring-webmvc-4.3.18.RELEASE.jar中org.springframework.web.servlet.DispatcherServlet.doService(request,respose)
--》doDispatch(request, response)
--》ha.handle
--》org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal()
--》invokeHandlerMethod()
--》invocableMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);//将parameterNameDiscoverer对象值赋给org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod父类org.springframework.web.method.support.InvocableHandlerMethod.parameterNameDiscoverer对象invocableMethod.parameterNameDiscoverer。
2.取request中参数流程
当处理request时RequestMappingHandlerAdapter.invokeHandlerMethod()调用org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle()调用父类org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest()调用getMethodArgumentValues()取到request中的对应参数值
3.初始化parameterNameDiscoverer流程及源码
以下为初始化org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.parameterNameDiscoverer流程
--》 private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
--》spring-core-4.3.18.jar org.springframework.core.DefaultParameterNameDiscoverer();
-->spring-core-4.3.18.jar org.springframework.core.PrioritizedParameterNameDiscoverer.addDiscoverer(new org.springframework.core.StandardReflectionParameterNameDiscoverer() extends ParameterNameDiscoverer)
{
List<ParameterNameDiscoverer> parameterNameDiscoverers.add(ParameterNameDiscoverer pnd)
}
//-->spring-core-4.3.18.jar org.springframework.core.MethodParameters.initParameterNameDiscovery()
在org.springframework.core.ParameterNameDiscoverer.getParameterNames(Method method)
-->org.springframework.core.StandardReflectionParameterNameDiscoverer.getParameterNames(Method method)
package org.springframework.core; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import org.springframework.lang.UsesJava8; @UsesJava8 public class StandardReflectionParameterNameDiscoverer implements ParameterNameDiscoverer { public String[] getParameterNames(Method method) { Parameter[] parameters = method.getParameters();//利用java8的新特性来取参数名 String[] parameterNames = new String[parameters.length]; for (int i = 0; i < parameters.length; ++i) { Parameter param = parameters[i]; if (!(param.isNamePresent())) return null; parameterNames[i] = param.getName(); } return parameterNames; } public String[] getParameterNames(Constructor<?> ctor) { Parameter[] parameters = ctor.getParameters(); String[] parameterNames = new String[parameters.length]; for (int i = 0; i < parameters.length; ++i) { Parameter param = parameters[i]; if (!(param.isNamePresent())) return null; parameterNames[i] = param.getName(); } return parameterNames; } }