SpringMVC的执行流程
1.客户端发起请求: 用户通过游览器或者其他的客户端向服务端发起HTTP请求
2.前端控制器(DispatcherServlet)用来接收请求: 所有的请求都会被SpringMVC的前端控制接收(这也是和Servlet最大的区别), 他是MVC的核心,去负责任务的调度与处理不同的请求
3.处理映射器(HandlerMapping): 当前端控制器接收到请求的时候, 回去查询一个或者多个的handlerMapper,来确定由那个具体的处理器来接收。
4.返回处理器的执行链,里面会包含多个拦截器的信息,以及需要查找的处理器handler的信息。(我们通常使用的mvc的拦截器就是会在这里生效)
5.找处理器适配器HandlerAdapter,这一步开始就会去调用handler里面的方法,通过执行这个handler里面的方法,会去找具体的controller方法,找到具体的controller之后,会返回一个modelAndView对象给这个HanderAdapter给处理器适配器
8.返回到前端控制器: 处理器适配器获取到ModelAndView之后,会将这个结果返回给DispatcherServlet前端控制器
9.视图解析:通过这个ViewResolver视图解析器进行解析这个ModelAndView
10.渲染视图:解析完成之后,会将这个view返回给前端DispatcherServlet前端控制器,再将model中的数据填充到这个view视图里面,最后去渲染视图
SpringMVC是如何路由到不同的Controller中的
要知道,一个http请求中,携带有不同的信息,如url,method,header等等,SpringMVC通过Match类统一封装所有的RequestMappingInfo中的各种condition,同时利用compare方法,直接比较出最优的那个handlerMethod。同时,不管是RequestMappingInfo和其组合的各个condition都实现了RequestCondition接口,所以,这也符合组合模式的基本思想
但凡涉及到路由, 那其中包含的数据接口一定是与map相关联的, 所以就会将一个大的map将url和cotroller中对应的方法作为键值对的方式存储起来, 以达到路由的目的
在SpringMVC启动的时候, 会将带有@Requestmapping的注解封装成一个RequestMappingInfo和HandlerMethod,然后注册到容器中,当HttpServletRequest访问时,会通过AbstractHandlerMethodMapping#lookupHandlerMethod方法获取对应的HandlerMethod
源码
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
List<Match> matches = new ArrayList<>();
// 先通过url获取到对应的RequestMappingInfo集合
List<T> directPathMatches = this.mappingRegistry.getMappingsByDirectPath(lookupPath);
if (directPathMatches != null) {
// 把RequestMappingInfo和HandlerMethod放到match里面
addMatchingMappings(directPathMatches, matches, request);
}
if (matches.isEmpty()) {
addMatchingMappings(this.mappingRegistry.getRegistrations().keySet(), matches, request);
}
if (!matches.isEmpty()) {
Match bestMatch = matches.get(0);
// 如果匹配到多个Match(譬如url相同但是方法不同),则通过RequestMappingInfo中的各种condition匹配出对应的bestMatch
if (matches.size() > 1) {
}
// 获取match中的HandlerMethod
return bestMatch.getHandlerMethod();
}
else {
return handleNoMatch(this.mappingRegistry.getRegistrations().keySet(), lookupPath, request);
}
}