当前位置:首页 > Spring > 正文内容

SpringMVC通过url请求到Controller的过程

淙嶙7年前 (2018-10-10)Spring1006
>org.springframework.web.servlet.FrameworkServlet#doGet
  >org.springframework.web.servlet.FrameworkServlet#processRequest
    >org.springframework.web.servlet.DispatcherServlet#doService
      >org.springframework.web.servlet.DispatcherServlet#doDispatch

        >org.springframework.web.servlet.DispatcherServlet#getHandler //找到处理当前请求的handler
          >org.springframework.web.servlet.handler.AbstractHandlerMapping#getHandler //获取HandlerExecutionChain
            >org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#getHandlerInternal
              >org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#lookupHandlerMethod //查找一个最合适的handler method
                >org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#getMappingsByUrl  //获取到url中的controller访问的 patternsCondition
                  >org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#addMatchingMappings
                  //matches.add(new Match(match,this.mappingRegistry.getMappings().get(mapping)));   Match(pattern,controller) 
                  //最终返回一个最合适的handler

        >org.springframework.web.servlet.DispatcherServlet#getHandlerAdapter //找到处理当前请求的handler adapter

        >org.springframework.web.servlet.HandlerAdapter#handle //使用handler去处理请求
          >org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#handleInternal
            >org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#invokeHandlerMethod
              >org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod#invokeAndHandle
                >org.springframework.web.method.support.InvocableHandlerMethod#invokeForRequest
                  >org.springframework.web.method.support.InvocableHandlerMethod#doInvoke


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. 找到处理当前请求的handler
                mappedHandler = getHandler(processedRequest);
                if (mappedHandler == null || mappedHandler.getHandler() == null) {
                    noHandlerFound(processedRequest, response);
                    return;
                }

                // Determine handler adapter for the current 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()) {
                        logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
                    }
                    if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
                        return;
                    }
                }

                if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                    return;
                }

                // Actually invoke the handler. 实际调用的handler,使用handler去处理请求
                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

                if (asyncManager.isConcurrentHandlingStarted()) {
                    return;
                }

                applyDefaultViewName(processedRequest, mv);
                mappedHandler.applyPostHandle(processedRequest, response, mv);
            }
            catch (Exception ex) {
                dispatchException = ex;
            }
            catch (Throwable err) {
                // As of 4.3, were 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) {
                    cleanupMultipart(processedRequest);
                }
            }
        }
    }

相关文章

Spring WebFlux WebSocket 推送

Spring WebFlux WebSocket 推送

关于websocket的资料一大堆,但是关于webflux websocket的有用的太少了,由于项目中需要使用,花了几天时间终于搞通了,今天把主要关于推送的部分总结出来,便于以后参考 1. 服务端...

什么是Web Hook

什么是Web Hook

什么是web hook?webhook是一个api概念,是为服务API的使用范式之一,也被成为反向api,是一种通过通常的callback,去增加或者改变web page或者web app行为的方法....

Dubbo和Spring结合配置文件内容解析为bean的过程

Dubbo和Spring结合配置文件内容解析为bean的过程

Dubbo 现在已经被很多公司广泛的使用,Dubbo的使用和特性本篇不做讲解,本篇讲解一下Dubbo和Spring结合配置文件内容解析为bean的过程! Dubbo是结合Spring来进行使用的,其中...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。