springmvc 代码初看

 

 

 

看了看springmvc代码,断点记录了一下。

trigger  http://localhost:8080/mvc/hi

 

 

 

 

 

一开始进入FrameworkServlet   

 

 

 

 

 

 

DispatcherServlet extends FrameworkServlet 

    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 = 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;
                    }
                }

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

                // Actually invoke the 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, we're 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);
                }
            }
        }
    }

 

进这里看看 

 

 

 

 

 

 

 

这里为什么要加锁

 

 

 

 

 

 

看到有一个map有项目中所有url和controller.method的映射,  拿到我们要访问的那个 :

 

 

 

 

 上面加锁是为了这里吧

 

 进这里看,

 原来是去spring 容器中拿到HiController 作为Handler,   

所以我们对springmvc而言,Hander就是我们写的Controller

 

 

 最后return的HanderMethod就是给它绑定了一堆东西, 主要还是这个Hander-HiController

 

 

 

 

 

 

所以回到前面, 这里的目的就是通过request里的url, 在spring容器中找到对应的Controller 作为Hander , 包装给HanderMethod返回:

 

 

 

 

这里留个问题,这里不知道Inteceptor是什么:

 

 

 这里将一些默认的interceptor加入到chain, 这些interceptor怎么自定义,在哪里执行,留个问题

 

 

 

 

回到DispatcherServlet, 这里就很简单,拿到对应的Adaptor

 

 

 

 

 拿到的是这个:

 

 

 

 

 

 在这里调用到实际的方法:

 

 

 处理方法并返回ModelAndView:

 

 

 跳过很多实际在这里调用

 

 

 

 

 

 

 

 

 

 进到这里就看到用反射调用到了HiController的方法

 

 

 

 

 

 

回到DispatcherServlet,和注释一样,果然实际在这里调用Handler:

 

下面processDispatchResult  是为了调用Interceptor的afterComplete方法。

 

 来看这里:


 

 

前面不理解的interceptor在这里用了:

 

 

 

 

 

在这里用了,而且它有preHandle, postHandle, afterCompletion 这些方法。前面的方法是什么时候调用的。interceptor是什么时候调用的,怎么调用的,需要再看看。

 自己定义的话,就是继承HandlerInterceptor重写方法吧。

回到DispatcherServlet, 后面没什么了。走完了。

回到frameServlet。 

 

 

留下两个问题, interceptor 怎么弄的,怎么用,FrameServlet是怎么调用的。

 

posted @ 2020-10-15 00:53  圣金巫灵  阅读(151)  评论(0编辑  收藏  举报