广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringMVC源码分析(3)Dis
  • 350
分享到

SpringMVC源码分析(3)Dis

源码SpringMVCDis 2023-01-31 07:01:24 350人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

<springMVC源码分析(1)标签解析>:介绍了解析过程中,初始化若干组件。<springmvc源码分析(2)DispatcherServlet的初始化>:初始化DispatcherServlet的多个组件。本文

<springMVC源码分析(1)标签解析>:介绍了解析过程中,初始化若干组件。

<springmvc源码分析(2)DispatcherServlet的初始化>:初始化DispatcherServlet的多个组件。

本文继续分析DispatcherServlet解析请求的过程。

概览

231546366285136.jpg

  ①:DispatcherServlet是springmvc中的前端控制器(front controller),负责接收request并将request转发给对应的处理组件.

  ②:HanlerMapping是springmvc中完成url到controller映射的组件.DispatcherServlet接收request,然后从HandlerMapping查找处理request的controller.

  ③:Cntroller处理request,并返回ModelAndView对象,Controller是springmvc中负责处理request的组件(类似于struts2中的Action),ModelAndView是封装结果视图的组件.

  ④ ⑤ ⑥:视图解析器解析ModelAndView对象并返回对应的视图给客户端.

要点

  1. 维护url和controller的映射

    这部分工作由DefaultAnnotationHandlerMapping.setApplicationContext的父类

    org.springframework.WEB.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext实现。具体方法为detectHandlers

protected void detectHandlers() throws BeansException {
   if (logger.isDebugEnabled()) {
      logger.debug("Looking for URL mappings in application context: " + getApplicationContext());
   }
   String[] beanNames = (this.detectHandlersInAncestorContexts ?
         BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
         getApplicationContext().getBeanNamesForType(Object.class));

   // Take any bean name that we can determine URLs for.
   for (String beanName : beanNames) {
      String[] urls = determineUrlsForHandler(beanName);
      if (!ObjectUtils.isEmpty(urls)) {
         // URL paths found: Let's consider it a handler.
         reGISterHandler(urls, beanName);
      }
      else {
         if (logger.isDebugEnabled()) {
            logger.debug("Rejected bean name '" + beanName + "': no URL paths identified");
         }
      }
   }
}

2.准确定位处理请求的具体方法(在AnnotationMethodHandlerAdapter中实现)

protected ModelAndView invokeHandlerMethod(httpservletRequest request, HttpServletResponse response, Object handler)
      throws Exception {

   ServletHandlerMethodResolver methodResolver = getMethodResolver(handler);
   Method handlerMethod = methodResolver.resolveHandlerMethod(request);//具体实现方法的匹配
   ServletHandlerMethodInvoker methodInvoker = new ServletHandlerMethodInvoker(methodResolver);
   ServletWebRequest webRequest = new ServletWebRequest(request, response);
   ExtendedModelMap implicitModel = new BindingAwareModelMap();

   Object result = methodInvoker.invokeHandlerMethod(handlerMethod, handler, webRequest, implicitModel);
   ModelAndView mav =
         methodInvoker.getModelAndView(handlerMethod, handler.getClass(), result, implicitModel, webRequest);
   methodInvoker.updateModelAttributes(handler, (mav != null ? mav.getModel() : null), implicitModel, webRequest);
   return mav;
}





1.请求入口

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

   proce***equest(request, response);
}


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

   proce***equest(request, response);
}

protected final void proce***equest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

   long startTime = System.currentTimeMillis();
   Throwable failureCause = null;

   // Expose current LocaleResolver and request as LocaleContext.
   LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
   LocaleContextHolder.setLocaleContext(buildLocaleContext(request), this.threadContextInheritable);
   // Expose current RequestAttributes to current thread.
   RequestAttributes previousRequestAttributes = RequestContextHolder.getRequestAttributes();
   ServletRequestAttributes requestAttributes = null;
   if (previousRequestAttributes == null || previousRequestAttributes.getClass().equals(ServletRequestAttributes.class)) {
      requestAttributes = new ServletRequestAttributes(request);
      RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
   }

   if (logger.isTraceEnabled()) {
      logger.trace("Bound request context to thread: " + request);
   }

   try {
      doService(request, response);
   }
   catch (ServletException ex) {
      failureCause = ex;
      throw ex;
   }
   catch (IOException ex) {
      failureCause = ex;
      throw ex;
   }
   catch (Throwable ex) {
      failureCause = ex;
      throw new NestedServletException("Request processing failed", ex);
   }

   finally {
      // Clear request attributes and reset thread-bound context.
      LocaleContextHolder.setLocaleContext(previousLocaleContext, this.threadContextInheritable);
      if (requestAttributes != null) {
         RequestContextHolder.setRequestAttributes(previousRequestAttributes, this.threadContextInheritable);
         requestAttributes.requestCompleted();
      }
      if (logger.isTraceEnabled()) {
         logger.trace("Cleared thread-bound request context: " + request);
      }

      if (failureCause != null) {
         this.logger.debug("Could not complete request", failureCause);
      }
      else {
         this.logger.debug("Successfully completed request");
      }
      if (this.publishEvents) {
         // Whether or not we succeeded, publish an event.
         long processingTime = System.currentTimeMillis() - startTime;
         this.webApplicationContext.publishEvent(
               new ServletRequestHandledEvent(this,
                     request.getRequestURI(), request.getRemoteAddr(),
                     request.getMethod(), getServletConfig().getServletName(),
                     WebUtils.getSessionId(request), getUsernameForRequest(request),
                     processingTime, failureCause));
      }
   }
}

proce***equest方法主要做4项工作。

  1. 得到当前线程的LocaleContext和RequestAttributes,创建新的LocaleContext和RequestAttributes并重新绑定到当前线程。

  2. 调用子类实现的doService()

  3. 重置当前线程的LocaleContext和RequestAttributes

  4. 执行成功后,发布ServletRequestHandledEvent事件。

2.DispatcherServlet自定义的doService方法

protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
   if (logger.isDebugEnabled()) {
      String requestUri = urlPathHelper.getRequestUri(request);
      logger.debug("DispatcherServlet with name '" + getServletName() + "' processing " + request.getMethod() +
            " request for [" + requestUri + "]");
   }

   // 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)) {
      logger.debug("Taking snapshot of request attributes before include");
      attributesSnapshot = new HashMap<String, Object>();
      Enumeration attrNames = request.getAttributeNames();
      while (attrNames.hasMoreElements()) {
         String attrName = (String) attrNames.nextElement();
         if (this.cleanupAfterInclude || attrName.startsWith("org.springframework.web.servlet")) {
            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());

   try {
      doDispatch(request, response);
   }
   finally {
      // Restore the original attribute snapshot, in case of an include.
      if (attributesSnapshot != null) {
         restoreAttributesAfterInclude(request, attributesSnapshot);
      }
   }
}

主要做两部分工作

  1. 如果是include请求,先保存一份request域数据的快照,doDispatch执行过后,将会用快照数据恢复。

  2. 调用doDispatch方法,完成请求转发。

3.doDispatch方法

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
   HttpServletRequest processedRequest = request;
   HandlerExecutionChain mappedHandler = null;
   int interceptorIndex = -1;

   try {
      ModelAndView mv;
      boolean errorView = false;

      try {
      // 1.检查是否是文件上传的请求
         processedRequest = checkMultipart(request);

         // Determine handler for the current request.
          // 2.取得处理当前请求的controller,这里也称为hanlder,处理器,第一个步骤的意义就在这里体现了.
          //这里并不是直接返回controller,而是返回的HandlerExecutionChain请求处理器链对象,该对象封装了handler和interceptors.
         mappedHandler = getHandler(processedRequest, false);
         if (mappedHandler == null || mappedHandler.getHandler() == null) {
            noHandlerFound(processedRequest, response);
            return;
         }

         // Determine handler adapter for the current request.
         //3. 获取处理request的处理器适配器handler adapter 
         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()) {
               String requestUri = urlPathHelper.getRequestUri(request);
               logger.debug("Last-Modified value for [" + requestUri + "] is: " + lastModified);
            }
            if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
               return;
            }
         }

         // Apply preHandle methods of registered interceptors.
          // 4.拦截器的预处理方法
         HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
         if (interceptors != null) {
            for (int i = 0; i < interceptors.length; i++) {
               HandlerInterceptor interceptor = interceptors[i];
               if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {
                  triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
                  return;
               }
               interceptorIndex = i;
            }
         }

         // Actually invoke the handler.
         // 5.实际的处理器处理请求,返回结果视图对象
         mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

         // Do we need view name translation?
         if (mv != null && !mv.hasView()) {
            mv.setViewName(getDefaultViewName(request));
         }

         // Apply postHandle methods of registered interceptors.
         // 6.拦截器的后处理方法
         if (interceptors != null) {
            for (int i = interceptors.length - 1; i >= 0; i--) {
               HandlerInterceptor interceptor = interceptors[i];
               interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);
            }
         }
      }
      catch (ModelAndViewDefiningException ex) {
         logger.debug("ModelAndViewDefiningException encountered", ex);
         mv = ex.getModelAndView();
      }
      catch (Exception ex) {
         Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
         mv = processhandlerException(processedRequest, response, handler, ex);
         errorView = (mv != null);
      }

      // Did the handler return a view to render?
      if (mv != null && !mv.wasCleared()) {
         render(mv, processedRequest, response);
         if (errorView) {
            WebUtils.clearErrorRequestAttributes(request);
         }
      }
      else {
         if (logger.isDebugEnabled()) {
            logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName() +
                  "': assuming HandlerAdapter completed request handling");
         }
      }

      // Trigger after-completion for successful outcome.
      triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
   }

   catch (Exception ex) {
      // Trigger after-completion for thrown exception.
      triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
      throw ex;
   }
   catch (Error err) {
      ServletException ex = new NestedServletException("Handler processing failed", err);
      // Trigger after-completion for thrown exception.
      triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
      throw ex;
   }

   finally {
      // Clean up any resources used by a multipart request.
      if (processedRequest != request) {
         cleanupMultipart(processedRequest);
      }
   }
}

很明显这儿是SpringMVC核心。

1.根据请求的路径找到HandlerMethod(带有Method反射属性,也就是对应Controller中的方法)(DispatcherServlet.getHandler完成)

2.匹配路径对应的拦截器(DispatcherServlet.getHandler完成)

3.获得HandlerExecutionChain对象(DispatcherServlet.getHandler完成)

4.通过HandlerAdapter对象进行处理得到ModelAndView对象(HandlerAdapter.handle)

5.调用HandlerInterceptor.preHandle

6.调用HandlerInterceptor.postHandle

7. 渲染


4.总结

wKioL1g-153C8LZpAACyG_bLg7c573.png

简单粗暴的总结

step1-6: 获取controller

step5-15 :调用controller方法

step17-20:渲染view

其他:aop方式处理拦截统一处理。

--结束END--

本文标题: SpringMVC源码分析(3)Dis

本文链接: https://www.lsjlt.com/news/191237.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringMVC源码分析(3)Dis
    <SpringMVC源码分析(1)标签解析>:介绍了解析过程中,初始化若干组件。<SpringMVC源码分析(2)DispatcherServlet的初始化>:初始化DispatcherServlet的多个组件。本文...
    99+
    2023-01-31
    源码 SpringMVC Dis
  • wifidog 源码初分析(3)
    上一篇分析了 接入设备 在接入路由器,并发起首次 HTTP/80 请求到路由器上时,wifidog 是如何将此 HTTP 请求重定向至 auth-server 的流程。 之后 接入设备 的浏览器接收到 wifidog 返回的 302 重定向...
    99+
    2023-01-31
    源码 wifidog
  • SpringMVC源码分析6:SpringMVC的视图解析原理
    转自 SpringMVC视图机制详解[附带源码分析]...
    99+
    2023-06-02
  • SpringMVC异常处理源码分析
    今天小编给大家分享一下SpringMVC异常处理源码分析的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。背景我们的代码中,总是...
    99+
    2023-07-05
  • SpringBoot中Tomcat和SpringMVC整合源码分析
    目录概述一、自动装配原理二、内嵌式Tomcat注入2.1自动注入配置类分析1.ServletWebServerFactoryAutoConfiguration 配置类分析2.2注入逻...
    99+
    2022-11-13
  • SpringMVC请求流程源码解析
    目录一、SpringMVC使用1.工程创建2.工程配置3.启动工程二、SpringMVC启动过程1.父容器启动过程2.子容器启动过程(SpringMvc容器)3.九大组件的初始化1....
    99+
    2022-11-13
  • SpringMVC源码分析4:DispatcherServlet如何找到正确的Controller
    SpringMVC是目前主流的Web MVC框架之一。...
    99+
    2023-06-02
  • 源码分析SpringMvc日志打印被忽略输出问题
    目录1.写在前面2.问题引出3.截取源码分析4.截取问题处理1.写在前面 在java的开发过程中,涉及到java web的开发,基本上都是走spring这一套了。 我们之前一般来说,...
    99+
    2022-11-13
  • PostgreSQL 源码解读(176)- 查询#94(语法分析:gram.y)#3
    本节继续介绍PostgreSQL...
    99+
    2022-10-18
  • RateLimiter 源码分析
    俗话说得好,缓存,限流和降级是系统的三把利剑。刚好项目中每天早上导出数据时因调订单接口频率过高,订单系统担心会对用户侧的使用造成影响,让我们对调用限速一下,所以就正好用上了。 常用的限流算法有2种:漏桶算法和令牌桶算法。漏桶算法漏...
    99+
    2023-05-31
    ratelimiter 源码 mi
  • SocketServer 源码分析
    Creating network servers. contents SocketServer.py contents file head BaseServer BaseServer.serve_forever BaseServ...
    99+
    2023-01-31
    源码 SocketServer
  • CesiumJS源码分析
    这篇文章主要介绍“CesiumJS源码分析”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“CesiumJS源码分析”文章能帮助大家解决问题。1. 有什么光CesiumJS 支持的光的类型比较少,默认场...
    99+
    2023-07-06
  • Spring源码剖析3:懒加载的单例Bean获取过程分析
    spring ioc 容器的加载流程...
    99+
    2023-06-02
  • Java从源码角度解析SpringMVC执行流程
    SpringMVC执行流程在面试中经常会被问到,本篇文章通过源码的方式简单的了解一下SpringMVC执行流程。 先看流程 先看一下SpringMVC执行流程再看源码,有助理解: ⽤...
    99+
    2023-05-16
    Spring MVC SpringMVC执行流程
  • Android LayoutInflater.inflate源码分析
    LayoutInflater.inflate源码详解 LayoutInflater的inflate方法相信大家都不陌生,在Fragment的onCreateView中或者在Ba...
    99+
    2022-06-06
    layoutinflater Android
  • Android AsyncTask源码分析
    Android中只能在主线程中进行UI操作,如果是其它子线程,需要借助异步消息处理机制Handler。除此之外,还有个非常方便的AsyncTask类,这个类内部封装了Handl...
    99+
    2022-06-06
    asynctask Android
  • Nebula Graph源码分析
    本篇内容介绍了“Nebula Graph源码分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!导读对于一些...
    99+
    2022-10-19
  • Kafka源码分析(一)
    Apache Kafka® 是 一个分布式流处理平台. 这到底意味着什么呢 我们知道流处理平台有以下三种特性: 可以让你发布和订阅流式的记录。这一方面与消息队列或者企业消息系统类似。 可以储存流式的记录,并且有较好的容错性。 可...
    99+
    2019-10-17
    Kafka源码分析(一)
  • Pythonkeras.metrics源代码分析
    目录前言metrics原理解析(以metrics.Mean为例)创建自定义metrics创建无状态 metrics通过继承Metric创建有状态metricsadd_metric()...
    99+
    2022-11-13
    Python keras.metrics Python keras.metrics方法 Python keras.metrics示例
  • django源码分析 LazySetti
    一、django中通过LazySetting对象来获取项目的配置,LazySetting对象有什么特性?为什么使用这个对象? LazySetting顾名思义,就是延迟获取配置内容。比如,我们定义了一个对象A,并对其添加了一些属性,对A初始...
    99+
    2023-01-31
    源码 django LazySetti
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作