springMVC原始碼學習之獲取引數名
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; } }