SpringMVC DispatcherServles分析

SpringMVC DispatcherServles分析
web请求最先到达就是DispatcherServlet。他是springmvc的核心控制。再DispatcherServlet类中通过源码可看到:他是继承FrameworkServlet,HttpServletBean,HttpServlet, ApplicationContextAware EnvironmentCapable,EnvironmentAware 接口

SpringMVC DispatcherServles分析

HttpServlet 类中 我们 可以看到 service 方法的实现

1、通用servlet 可以接受任意类型的请求
将 ServletRequest、ServletResponse 转换成 HttpServletRequest 、HttpServletResponse

    public void service(ServletRequest req, ServletResponse res)
        throws ServletException, IOException
    {
        HttpServletRequest  request;
        HttpServletResponse response;
        
        if (!(req instanceof HttpServletRequest &&
                res instanceof HttpServletResponse)) {
            throw new ServletException("non-HTTP request or response");
        }

        request = (HttpServletRequest) req;
        response = (HttpServletResponse) res;

        service(request, response);
    }

protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();

    if (method.equals(METHOD_GET)) {
        long lastModified = getLastModified(req);
        if (lastModified == -1) {
            // servlet doesn't support if-modified-since, no reason
            // to go through further expensive logic
            doGet(req, resp);
        } else {
            long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
            if (ifModifiedSince < lastModified) {
                // If the servlet mod time is later, call doGet()
                // Round down to the nearest second for a proper compare
                // A ifModifiedSince of -1 will always be less
                maybeSetLastModified(resp, lastModified);
                doGet(req, resp);
            } else {
                resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            }
        }

    } else if (method.equals(METHOD_HEAD)) {
        long lastModified = getLastModified(req);
        maybeSetLastModified(resp, lastModified);
        doHead(req, resp);

    } else if (method.equals(METHOD_POST)) {
        doPost(req, resp);
        
    } else if (method.equals(METHOD_PUT)) {
        doPut(req, resp);
        
    } else if (method.equals(METHOD_DELETE)) {
        doDelete(req, resp);
        
    } else if (method.equals(METHOD_OPTIONS)) {
        doOptions(req,resp);
        
    } else if (method.equals(METHOD_TRACE)) {
        doTrace(req,resp);
        
    } else {
        //
        // Note that this means NO servlet supports whatever
        // method was requested, anywhere on this server.
        //

        String errMsg = lStrings.getString("http.method_not_implemented");
        Object[] errArgs = new Object[1];
        errArgs[0] = method;
        errMsg = MessageFormat.format(errMsg, errArgs);
        
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
    }
}
这里职责主要是先拿到一个请求,然后又做了一个判断请求方式。
FrameworkServlet 类中的 Service 方法 处理请求

![在这里插入图片描述](https://img-blog.****img.cn/20190327114043821.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI1OTkyMTc5,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.****img.cn/20190327115143206.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI1OTkyMTc5,size_16,color_FFFFFF,t_70)
DEBUG 可以看到,Service 最终调用了super.servic
 在HttpServlet中,最开始是按类型将请求分开的。但是,SpringMVC又将他们统一用  processRequest  来处理,是因为SpringMVC是将不同类型请求使用不同的Handler来处理。

调用doGet 方法

@Override
	protected final void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		processRequest(request, response);
	}

调用 processRequest() 方法

SpringMVC DispatcherServles分析
在processRequest() 再调用 doService()
SpringMVC DispatcherServles分析
在doService () 再调用doDispatch()
SpringMVC DispatcherServles分析
查询handler来处理请求

	@Nullable
	protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
		if (this.handlerMappings != null) {
			for (HandlerMapping mapping : this.handlerMappings) {
				HandlerExecutionChain handler = mapping.getHandler(request);
				if (handler != null) {
					return handler;
				}
			}
		}
		return null;
	}

SpringMVC DispatcherServles分析
决定使用那个适配器处理请求(适配器模式)
SpringMVC DispatcherServles分析

执行预处理方法
SpringMVC DispatcherServles分析
使用适配器执行目标方法,返回ModleAndView
SpringMVC DispatcherServles分析
执行目标后处理方法
SpringMVC DispatcherServles分析
业务逻辑执行以后的返回结果,调用本类的processDispatchResult方法
SpringMVC DispatcherServles分析
SpringMVC DispatcherServles分析
渲染视图

SpringMVC DispatcherServles分析
调用AbstractView类中的render方法
SpringMVC DispatcherServles分析
调用InternalResourceView类中的renderMergedOutputModel方法进行渲染模型、转发操作:
SpringMVC DispatcherServles分析