springmvc源码解读
一、基础流程
ApplicationContext初始化时建立所有url和Controller类的对应关系(AbstractHandlerMethodMapping类MappingRegistry 维护Map<URL,HandlerMethod>).
1、url和Controller的beanName保存至Map;
AbstractUrlHandlerMapping->registerHandler(String urlPath, Object handler);
2、根据请求url找到对应的Controller,并从Controller中找到处理请求的方法
DispatcherServlet
-> doService()
-> doDispatch()
-> getHandler(processedRequest) //获取HandlerExecutionChain(包含拦截器,及具体handlerMethod)
二、重要组成
)DispatherServlet
前端控制器
1、从HandlerMapping获取HandlerExecutionChain(Controller对象、HandlerInterceptor);
2、调用拦截器,将HandlerExecutionChain传入HandlerAdapter,返回ModelAndView;
3、将ModelAndView传入ViewResolver,获得View并返回Response;
)HandlerMapping
1、RequestMappingHandlerMapper,获取@Controller及@RequestMapping注释的Bean和拦截器封装成HandlerExecutionChain;
)HandlerAdapter
1、RequestMappingHandlerAdapter,获取HandlerAdapter;
2、RequestResponseBodyMethodProcessor,处理@RequestBody注释参数;
3、HandlerParamMethodArgumentResolver,解析方法参数,@RequestParam映射,asm编译字节码文件获取入参;
4、反射调用实际方法;
5、RequestResponseBodyMethodProcessor,处理@ResponseBody注释参数;
6、ModelAndViewMethodReturnValueHandler,处理方法返回值,返回ModelAndView;
7、HandlerExecutionChain:包含拦截器及HandlerMethod;
三、流程图
转载至:https://blog.csdn.net/huyang0304/article/details/82997514