SpringMVC_执行流程

四、SpringMVC执行流程

1.SpringMVC 常用组件

  • DispatcherServlet:前端控制器,用于对请求和响应进行统一处理
  • HandlerMapping:处理器映射器,根据 url/method可以去找到具体的 Handler(Controller)
  • Handler:具体处理器(程序员,以后开发这一部分需要)
  • HandlerAdapter:处理器适配器,进行处理器方法的执行
  • ViewResolver:处理视图相关的

2.处理流程图

SpringMVC_执行流程_第1张图片

3.执行流程原理分析

3.1DispatcherServlet

  • 初始化操作

    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);
        }
    
  • 具体处理请求的方法

    protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //删掉了一系列没用方法
    
            try {
                //具体执行方法
                this.doService(request, response);
            } catch (IOException | ServletException var16) {
                failureCause = var16;
                throw var16;
            } catch (Throwable var17) {
                failureCause = var17;
                throw new NestedServletException("Request processing failed", var17);
            } finally {
                this.resetContextHolders(request, previousLocaleContext, previousAttributes);
                if (requestAttributes != null) {
                    requestAttributes.requestCompleted();
                }
    
                this.logResult(request, response, (Throwable)failureCause, asyncManager);
                this.publishRequestHandledEvent(request, response, startTime, (Throwable)failureCause);
            }
    
        }
    
    protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
            //删掉一系列干扰代码
    
            try {
                //具体执行
                this.doDispatch(request, response);
            } finally {
                if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted() && attributesSnapshot != null) {
                    this.restoreAttributesAfterInclude(request, attributesSnapshot);
                }
    
            }
    
        }
    
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
            HttpServletRequest processedRequest = request;
            HandlerExecutionChain mappedHandler = null;
            boolean multipartRequestParsed = false;
            WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    
            try {
                try {
                    ModelAndView mv = null;
                    Object dispatchException = null;
    
                    try {
                        processedRequest = this.checkMultipart(request);
                        multipartRequestParsed = processedRequest != request;
                        //获取处理器就是具体的需要执行的 Tontroller
                        mappedHandler = this.getHandler(processedRequest);
                        if (mappedHandler == null) {
                            this.noHandlerFound(processedRequest, response);
                            return;
                        }
    					//获取处理器适配器
                        HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
                        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;
                            }
                        }
    
                        if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                            return;
                        }
    					//具体调用 Controller 中的方法
                        mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
                        if (asyncManager.isConcurrentHandlingStarted()) {
                            return;
                        }
    
                        this.applyDefaultViewName(processedRequest, mv);
                        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);
                }
    
            }
        }
    
    //获取具体执行的
    protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
            if (this.handlerMappings != null) {
                Iterator var2 = this.handlerMappings.iterator();
    
                while(var2.hasNext()) {
                    HandlerMapping mapping = (HandlerMapping)var2.next();
                    HandlerExecutionChain handler = mapping.getHandler(request);
                    if (handler != null) {
                        return handler;
                    }
                }
            }
    
            return null;
        }
    

3.2HandlerMapping

  • dbug 的图示

    SpringMVC_执行流程_第2张图片

你可能感兴趣的:(状态模式,spring,mvc,后端,java)