1、前言
从技术角度去思考,任何一个现存的框架都有其存在理由,而这个理由就是解决实际的问题,或者提供更好的解决问题的方案。(此处预留一个滑稽)
2、SpringMVC解决了什么
相较于传统Servlet,SpringMVC为我们带来了以下功能:
- URL映射
- 表单参数映射
- 调用目标controller
- 数据模型映射
- 视图解析
- 异常处理
而SpringMVC之所以能够实现这些功能,则得益于它的一系列组件:
- HandlerMapping:url与控制器的映射
- HandlerAdapter:控制器执行适配器
- ViewResolver:视图解析器
- View:视图
- HandlerExceptionResolver:异常捕捉器
- HandlerInterceptor:拦截器
3、SpringMVC处理流程
从使用者的角度来看,我们对SpringMVC的感知仅仅在于配置了DispatcherServlet,即使是使用@Controller注解,也只是在xml文件中加上注解扫描路径
<context:component-scan base-package="xxx"/>
良好的框架封装以及设计使得我们在使用SpringMVC时十分方便,但也因此导致了大部分人知其然而不知其所以然,更别提SpringBoot零配置带来的便利。
所以我们先从上帝视角来看看,SpringMVC是怎么处理的:
4、DispatcherServlet源码
这里贴一下对源码的解读
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
//检查请求是否是multipart(即文件上传),若是进行相关处理
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
//获取request的Handler方法,handler被封装为HandlerExecutionChain,
//HandlerExecutionChain为handler方法加上拦截器的集合。
//包括了interceptor的前置和后置方法
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
//根据处理器(handler及HandlerExecutionChain)获取处理器适配器
//(处理器适配器是为了提供统一接口进行后续处理,从而支持多种类型的处理器)
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (logger.isDebugEnabled()) {
logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
}
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
//执行chain中拦截器附加的预处理方法,即preHandle方法
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
//适配器统一执行handle方法(适配器统一接口的作用),此处是真正处理业务逻辑的地方
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
//执行chain中拦截器附加的后处理方法,即postHandle方法
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
//处理请求分发的结果
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err));
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
//清除现场工作,如果是multipart的请求,清空上传的multipart
cleanupMultipart(processedRequest);
}
}
}
}