源码主要流程,省略了一些代码
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
//1 通过request从handlerMappings中获取HandlerExecutionChain
HandlerExecutionChain mappedHandler = getHandler(processedRequest);
//2通过handler获得处理器适配器HandlerAdapter
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
//3 执行过滤器前置方法applyPreHandle
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// 4 执行
ModelAndView mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
4.1 RequestMappingHandlerAdapter里面的invokeHandlerMethod方法
protected ModelAndView handleInternal(HttpServletRequest request,
ModelAndView mav = invokeHandlerMethod(request, response, handlerMethod);
}
4.1.1 ServletInvocableHandlerMethod类
protected ModelAndView invokeHandlerMethod(...) {
ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
invocableMethod.invokeAndHandle(webRequest, mavContainer);
}
4.1.1.1
public void invokeAndHandle(...) {
Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
}
4.1.1.1.1
public Object invokeForRequest(..) {
//获取参数
Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
//通过反射调用对应的HandleMethod对应的method方法
return doInvoke(args);
}
5 过滤器后置处理
mappedHandler.applyPostHandle(processedRequest, response, mv);
}