springmvc流程跟踪

 springmvc的执行流程前端控制器继承自servlet方法我们在dispatcherServlet里找到doService方法进入:
 this.doDispatch(request, response);
里面是定义一些参数,主要的是:
 mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); 大致是调用控制器方法,执行的是RequestMappingHandlerAdapter(是HandlerAdapter的实现类) 的HandlerInternal方法,进入handleInternal:
protected ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
        this.checkRequest(request);
        ModelAndView mav;
        if (this.synchronizeOnSession) {
            HttpSession session = request.getSession(false);
            if (session != null) {
                Object mutex = WebUtils.getSessionMutex(session);
                synchronized(mutex) {
                    mav = this.invokeHandlerMethod(request, response, handlerMethod);
                }
            } else {
                mav = this.invokeHandlerMethod(request, response, handlerMethod);
            }
        } else {
            mav = this.invokeHandlerMethod(request, response, handlerMethod);
        }

        if (!response.containsHeader("Cache-Control")) {
            if (this.getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
                this.applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
            } else {
                this.prepareResponse(response);
            }
        }

        return mav;
    }
此时执行的是mav = this.invokeHandlerMethod(request, response, handlerMethod);类中的方法进入其中此时执行的是ServletInvocableHandlerMethod类的invokeAndHandle方法,其实到了invocableMethod.invokeAndHandle(webRequest, mavContainer, new Object[0]);这句我们已经可以看出哪个方法执行的关系了
 Object returnValue = this.invokeForRequest(webRequest, mavContainer, providedArgs);执行的是InvocableHandlerMethod类中的invokeForRequest方法
Object returnValue = this.doInvoke(args);执行本类的doInvoke
return this.getBridgedMethod().invoke(this.getBean(), args);已经是反射调用了。

 从这些追踪我们可以知道浏览器发送请求到dispatcherServlet走到了处理器映射器适配器RequestMappingHandlerAdapter 处理控制方法InvocableHandlerMethod 执行方法反射的InvocableHandlerMethod拿到modelandvew

猜你喜欢

转载自www.cnblogs.com/xyhx/p/11441712.html
今日推荐