SpringMVC的执行流程

SpringMVC的执行流程

一.前言

刚学完SpringMVC的基本操作,但是对SpringMVC的执行流程源码很好奇,今天通过尚硅谷杨博超老师的教程视频学习并记录一下。

1.流程图

详细l流程图:

在这里插入图片描述

2.基于版本

 <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>

二.SpringMVC执行流程

1.SpringMVC常用组件

在分析流程之前,我们需要了解SpringMVC的常用组件及其作用,有助于我们理解源码。

  • DispatcherServlet:前端控制器,不需要工程师开发,由框架提供
    • 作用:统一处理请求和响应,整个流程控制的中心,由它调用其它组件处理用户的请求
  • HandlerMapping:处理器映射器,不需要工程师开发,由框架提供
    • 作用:根据请求的url、method等信息查找Handler,即控制器方法
  • Handler:处理器,需要工程师开发
    • 作用:在DispatcherServlet的控制下Handler对具体的用户请求进行处理
  • HandlerAdapter:处理器适配器,不需要工程师开发,由框架提供
    • 作用:通过HandlerAdapter对处理器(控制器方法)进行执行
  • ViewResolver:视图解析器,不需要工程师开发,由框架提供
    • 作用:进行视图解析,得到相应的视图,例如:ThymeleafView、InternalResourceView、 RedirectView
  • View:视图
    • 作用:将模型数据通过页面展示给用户

2、DispatcherServlet初始化过程

  • DispatcherServlet 本质上是一个 Servlet,所以天然的遵循 Servlet 的生命周期。

    所以宏观上是 Servlet 生命周期来进行调度。

(1)DispatcherServlet的继承和实现关系

由于我们的发送的请求和接受浏览器的响应都是由DispatcherServlet统一处理的,所以我们先要观察DispatcherServle类的特点。

所在类:org.springframework.web.servlet.DispatcherServlet

public class DispatcherServlet extends FrameworkServlet{
    
}

这里我们看出DispatcherServlet继承FrameworkServlet,我们点进FrameworkServlet类查看。

所在类 org.springframework.web.servlet.FrameworkServlet

public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {

FrameworkServlet又继承于HttpServletBean,我们 继续查看HttpServletBean类的继承关系。

所在类 org.springframework.web.servlet.HttpServletBean


public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware

这里我们看到,HttpServletBean继承于我们JavaWeb阶段经常使用的HttpServlet类。

所在类 org.springframework.web.servlet.HttpServlet


public abstract class HttpServlet extends GenericServlet

HttpServlet又继承了GenericServlet。

所在类 org.springframework.web.servlet.HttpServlet.GenericServlet

 
public abstract class GenericServlet 
    implements Servlet, ServletConfig, java.io.Serializable
{

GenericServlet最终实现了Servlet接口。

(2)分析继承方法
  • 由于我们的DispatcherServlet本来就是一个Servlet,所以它遵循Servlet的生命周期,所以他初始化的方法就是init,所以我们需要分析DispatcherServlet的这些父类把init方法继承给它的过程中都是怎么来执行的就可以了。

在这里插入图片描述

由于接口中的init方法是抽象方法,所以我们看看他的实现类GenericServlet中的重写方法。
在这里插入图片描述

GenericServlet中重写了init方法,这里将局部变量和成员变量config赋值,成员变量config就是ServletConfig,并且调用了init()无参构造方法。
在这里插入图片描述

这里我们看到这是一个空方法,那么这就是留给子类重写的。所以子类就在继承的过程中继承了有参和无参init(),而有参init()同时也调用了无参init()。
在这里插入图片描述

HttpServlet没有重写init初始化方法,所以我们要观察HttpServletBean类。

在这里插入图片描述

HttpServletBean重写了GenericServlet中的无参的init()方法

这里最重要的就是调用了initServletBean()方法,但是我们点进去又是一个空方法,留给子类重写。
在这里插入图片描述

在FrameworkServlet中我们找到了initServletBean()方法,这里初始化了webApplicationContext,这是我们在Web中使用的Ioc容器

this.webApplicationContext = this.initWebApplicationContext();
①下面我们分析initWebApplicationContext方法。

所在类:org.springframework.web.servlet.FrameworkServlet

  protected WebApplicationContext initWebApplicationContext() {
        WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        WebApplicationContext wac = null;
        if (this.webApplicationContext != null) {
            wac = this.webApplicationContext;
            if (wac instanceof ConfigurableWebApplicationContext) {
                ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)wac;
                if (!cwac.isActive()) {
                    if (cwac.getParent() == null) {
                        cwac.setParent(rootContext);
                    }

                    this.configureAndRefreshWebApplicationContext(cwac);
                }
            }
        }

        if (wac == null) {
            wac = this.findWebApplicationContext();
        }

        if (wac == null) {
            //创建WebApplicationContext
            wac = this.createWebApplicationContext(rootContext);
        }

        if (!this.refreshEventReceived) {
            synchronized(this.onRefreshMonitor) {
                
                // 刷新WebApplicationContext
                this.onRefresh(wac);
            }
        }

        if (this.publishContext) {
            String attrName = this.getServletContextAttributeName();
            // Publish the context as a servlet context attribute.
			// 将IOC容器在应用域共享
            this.getServletContext().setAttribute(attrName, wac);
        }

        return wac;
    }
②创建WebApplicationContext
    protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) {
        Class<?> contextClass = this.getContextClass();
        if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
            throw new ApplicationContextException("Fatal initialization error in servlet with name '" + this.getServletName() + "': custom WebApplicationContext class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext");
        } else {
            // 通过反射创建 IOC 容器对象
            ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);
            wac.setEnvironment(this.getEnvironment());    
            // 设置父容器,这里的父容器就是Spring的IOC容器,在ssm整合时会使用。
            wac.setParent(parent);
            String configLocation = this.getContextConfigLocation();
            if (configLocation != null) {
                wac.setConfigLocation(configLocation);
            }

            this.configureAndRefreshWebApplicationContext(wac);
            return wac;
        }
    }


③DispatcherServlet初始化策略

FrameworkServlet创建WebApplicationContext后,刷新容器,调用onRefresh(wac),此方法在 DispatcherServlet中进行了重写,调用了initStrategies(context)方法,初始化策略,即初始化 DispatcherServlet的各个组件。

所在类:org.springframework.web.servlet.DispatcherServlet

 protected void initStrategies(ApplicationContext context) {
        this.initMultipartResolver(context);
        this.initLocaleResolver(context);
        this.initThemeResolver(context);
        this.initHandlerMappings(context);
        this.initHandlerAdapters(context);
        this.initHandlerExceptionResolvers(context);
        this.initRequestToViewNameTranslator(context);
        this.initViewResolvers(context);
        this.initFlashMapManager(context);
    }

3.DispatcherServlet调用组件处理请求

①processRequest()

FrameworkServlet重写HttpServlet中的service()和doXxx(),这些方法中调用了 processRequest(request, response)

所在类:org.springframework.web.servlet.FrameworkServlet

  protected final void processRequest(HttpServletRequest request,
                                        HttpServletResponse response)
            throws ServletException, IOException {
        long startTime = System.currentTimeMillis();
        Throwable failureCause = null;
        LocaleContext previousLocaleContext =
                LocaleContextHolder.getLocaleContext();
        LocaleContext localeContext = buildLocaleContext(request);
        RequestAttributes previousAttributes =
                RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes requestAttributes = buildRequestAttributes(request,
                response, previousAttributes);
        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
        asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(),
                new RequestBindingInterceptor());
        initContextHolders(request, localeContext, requestAttributes);
        try {
				// 执行服务,doService()是一个抽象方法,在DispatcherServlet中进行了重写
            doService(request, response);
        } catch (ServletException | IOException ex) {
            failureCause = ex;
            throw ex;
        } catch (Throwable ex) {
            failureCause = ex;
            throw new NestedServletException("Request processing failed", ex);
        } finally {
            resetContextHolders(request, previousLocaleContext, previousAttributes);
            if (requestAttributes != null) {
                requestAttributes.requestCompleted();
            }
            logResult(request, response, failureCause, asyncManager);
            publishRequestHandledEvent(request, response, startTime, failureCause);
        }
    }


②doService()

所在类:org.springframework.web.servlet.DispatcherServlet

 @Override
    protected void doService(HttpServletRequest request, HttpServletResponse
            response) throws Exception {
        logRequest(request);
		// Keep a snapshot of the request attributes in case of an include
        	// to be able to restore the original attributes after the include.
        Map<String, Object> attributesSnapshot = null;
        if (WebUtils.isIncludeRequest(request)) {
            attributesSnapshot = new HashMap<>();
            Enumeration<?> attrNames = request.getAttributeNames();
            while (attrNames.hasMoreElements()) {
                String attrName = (String) attrNames.nextElement();
                if (this.cleanupAfterInclude ||
                        attrName.startsWith(DEFAULT_STRATEGIES_PREFIX)) {
                    attributesSnapshot.put(attrName,
                            request.getAttribute(attrName));
                }
            }
        }
		// Make framework objects available to handlers and view objects.
        request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                getWebApplicationContext());
        request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);
        request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);
        request.setAttribute(THEME_SOURCE_ATTRIBUTE, getThemeSource());
        if (this.flashMapManager != null) {
            FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request,
                    response);
            if (inputFlashMap != null) {
                request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE,
                        Collections.unmodifiableMap(inputFlashMap));
            }
            request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
            request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);
        }
        RequestPath requestPath = null;
        if (this.parseRequestPath &&
                !ServletRequestPathUtils.hasParsedRequestPath(request)) {
            requestPath = ServletRequestPathUtils.parseAndCache(request);
        }
        try {
		// 处理请求和响应
            doDispatch(request, response);
        } finally {
            if
            (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
		// Restore the original attribute snapshot, in case of an include.
                if (attributesSnapshot != null) {
                    restoreAttributesAfterInclude(request, attributesSnapshot);
                }
            }
            if (requestPath != null) {
                ServletRequestPathUtils.clearParsedRequestPath(request);
            }
        }
③doDispatch()

所在类:org.springframework.web.servlet.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 {
                processedRequest = checkMultipart(request);
                multipartRequestParsed = (processedRequest != request);
            // Determine handler for the current request.
            /*
            mappedHandler:调用链
            包含handler、interceptorList、interceptorIndex
            handler:浏览器发送的请求所匹配的控制器方法
            interceptorList:处理控制器方法的所有拦截器集合
            interceptorIndex:拦截器索引,控制拦截器afterCompletion()的执行
            */
                 mappedHandler = getHandler(processedRequest);
                if (mappedHandler == null) {
                    noHandlerFound(processedRequest, response);
                    return;
                }
        // Determine handler adapter for the current request.
		// 通过控制器方法创建相应的处理器适配器,调用所对应的控制器方法
                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 (new ServletWebRequest(request,
                            response).checkNotModified(lastModified) && isGet) {
                        return;
                    }
                }
				// 调用拦截器的preHandle()
                if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                    return;
                     }

                // Actually invoke the handler.
				// 由处理器适配器调用具体的控制器方法,最终获得ModelAndView对象

                    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
                    if (asyncManager.isConcurrentHandlingStarted()) {
                        return;
                    }
              	
                    this.applyDefaultViewName(processedRequest, mv);
                // 调用拦截器的postHandle()
                    mappedHandler.applyPostHandle(processedRequest, response, mv);
                } catch (Exception var20) {
                    dispatchException = var20;
                } catch (Throwable var21) {
                    dispatchException = new NestedServletException("Handler dispatch failed", var21);
                }

            // 后续处理:处理模型数据和渲染视图
                this.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException);
            } catch (Exception var22) {
                this.triggerAfterCompletion(processedRequest, response, mappedHandler, var22);
            } catch (Throwable var23) {
                this.triggerAfterCompletion(processedRequest, response, mappedHandler, new NestedServletException("Handler processing failed", var23));
            }

        } finally {
            if (asyncManager.isConcurrentHandlingStarted()) {
                if (mappedHandler != null) {
                    mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
                }
            } else if (multipartRequestParsed) {
                this.cleanupMultipart(processedRequest);
            }

        }
    }



④processDispatchResult()
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv, @Nullable Exception exception) throws Exception {
        boolean errorView = false;
        if (exception != null) {
            if (exception instanceof ModelAndViewDefiningException) {
                this.logger.debug("ModelAndViewDefiningException encountered", exception);
                mv = ((ModelAndViewDefiningException)exception).getModelAndView();
            } else {
                Object handler = mappedHandler != null ? mappedHandler.getHandler() : null;
                mv = this.processHandlerException(request, response, handler, exception);
                errorView = mv != null;
            }
        }
  		
        if (mv != null && !mv.wasCleared()) {      
             // 处理模型数据和渲染视图
            this.render(mv, request, response);
            if (errorView) {
                WebUtils.clearErrorRequestAttributes(request);
            }
        } else if (this.logger.isTraceEnabled()) {
            this.logger.trace("No view rendering, null ModelAndView returned.");
        }

        if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
            if (mappedHandler != null) {
                // 调用拦截器的afterCompletion()
                mappedHandler.triggerAfterCompletion(request, response, (Exception)null);
            }

        }
    }

4.SpringMVC的执行流程

1.用户向服务器发送请求,请求被SpringMVC 前端控制器 DispatcherServlet捕获。

2.DispatcherServlet对请求URL进行解析,得到请求资源标识符(URI),判断请求URI对应的映射:

​ a) 不存在

​ i. 再判断是否配置了mvc:default-servlet-handler

​ ii. 如果没配置,则控制台报映射查找不到,客户端展示404错误

​ iii. 如果有配置,则访问目标资源(一般为静态资源,如:JS,CSS,HTML),找不到客户端也会展示404 错误

​ b) 存在则执行下面的流程

  1. 根据该URI,调用HandlerMapping获得该Handler配置的所有相关的对象(包括Handler对象以及 Handler对象对应的拦截器),最后以HandlerExecutionChain执行链对象的形式返回。

  2. DispatcherServlet 根据获得的Handler,选择一个合适的HandlerAdapter。

  3. 如果成功获得HandlerAdapter,此时将开始执行拦截器的preHandler(…)方法【正向】

  4. 提取Request中的模型数据,填充Handler入参,开始执行Handler(Controller)方法,处理请求。 在填充Handler的入参过程中,根据你的配置,Spring将帮你做一些额外的工作:

​ a) HttpMessageConveter: 将请求消息(如Json、xml等数据)转换成一个对象,将对象转换为指定 的响应信息

​ b) 数据转换:对请求消息进行数据转换。如String转换成Integer、Double等

​ c) 数据格式化:对请求消息进行数据格式化。 如将字符串转换成格式化数字或格式化日期等

​ d) 数据验证: 验证数据的有效性(长度、格式等),验证结果存储到BindingResult或Error中

  1. Handler执行完成后,向DispatcherServlet 返回一个ModelAndView对象。
  2. 此时将开始执行拦截器的postHandle(…)方法【逆向】。
  3. 根据返回的ModelAndView(此时会判断是否存在异常:如果存在异常,则执行 HandlerExceptionResolver进行异常处理)选择一个适合的ViewResolver进行视图解析,根据Model 和View,来渲染视图。
  4. 渲染视图完毕执行拦截器的afterCompletion(…)方法【逆向】。
  5. 将渲染结果返回给客户端。

三.感谢:

尚硅谷杨博超老师

b站直达 SSM整合:

https://www.bilibili.com/video/BV1Ya411S7aT?p=177&share_source=copy_web&vd_source=8bcd4f03bb9c559b3851f96fa7fe4b11

SpringMVC:

https://www.bilibili.com/video/BV1Ry4y1574R?share_source=copy_web&vd_source=8bcd4f03bb9c559b3851f96fa7fe4b11

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值