Spring WebMVC执行Controller流程

第一篇了解到Spring Web MVC是在Servlet API上构建的原始Web框架。

而核心处理器 DispatcherServlet 最终继承的父类 是 HttpServlet。所以所有请求都会进入doGet或者是doPost方法的调用链。

 我们看第一个概念 HandlerMapping 此处有两个 这个应该在上一篇中介绍的 查找 Handler(可以理解为对应的Controller对象)

1.@Controller注解是用到的 HandlerMapping 是 RequestMappingHandlerMapping

2.通过实现接口的 HandlerMapping 是 BeanNameUrlHandlerMapping

 doGet 和 doPost 的调用链至此

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;
                mappedHandler = this.getHandler(processedRequest);//1.通过url 获取对应的handler 对象 包含了要执行的方法
                if (mappedHandler == null) {
                    this.noHandlerFound(processedRequest, response);
                    return;
                }

                HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());//2.通过handler对象来获取执行方法的适配器 即 该controller 对象 (执行一个方法需要 实例对象.method 和 目标参数)
                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;
                }

                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());//3.执行方法
                if (asyncManager.isConcurrentHandlingStarted()) {
                    return;
                }

                this.applyDefaultViewName(processedRequest, mv);
                mappedHandler.applyPostHandle(processedRequest, response, mv);
            } 

            this.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException);
        }  

    } finally {
        

    }
}

 1.通过url 获取对应的handler 对象 包含了要执行的方法

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    if (this.handlerMappings != null) {
        Iterator var2 = this.handlerMappings.iterator();

        while(var2.hasNext()) {
            HandlerMapping mapping = (HandlerMapping)var2.next();
            //处理注解类的,此处的HandlerMapping == RequestMappingHandlerMapping handler才不为空
            HandlerExecutionChain handler = mapping.getHandler(request); 
            if (handler != null) {
                return handler;
            }
        }
    }

    return null;
}
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    Object handler = this.getHandlerInternal(request);//通过url 进行匹配
    if (handler == null) {
        handler = this.getDefaultHandler();
    }

    if (handler == null) {
        return null;
    } else {
        if (handler instanceof String) {
            String handlerName = (String)handler;
            handler = this.obtainApplicationContext().getBean(handlerName);
        }

        HandlerExecutionChain executionChain = this.getHandlerExecutionChain(handler, request);//最后封装返回
        //有删减
        return executionChain;
    }
}
//通过url 进行匹配
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
    String lookupPath = this.getUrlPathHelper().getLookupPathForRequest(request);//获取路径
    this.mappingRegistry.acquireReadLock();

    HandlerMethod var4;
    try {
        HandlerMethod handlerMethod = this.lookupHandlerMethod(lookupPath, request);//获取方法
        var4 = handlerMethod != null ? handlerMethod.createWithResolvedBean() : null;
    } finally {
        this.mappingRegistry.releaseReadLock();
    }

    return var4;
}

//2.通过handler对象来获取执行方法的适配器 即 该controller 对象 (执行一个方法需要 实例对象.method 和 目标参数)

适配器有3种

1.RequestMappingHandlerAdapter -- 注解注册的 @controller 适配器 -- 通过反射方法执行我们主要看这个

2.HttpRequestHandlerAdapter -- 实现 HttpRequestHandler 接口注册的 controller 适配器 -- 直接强转调用重写的方法执行

3.SimpleControllerHandlerAdapter -- 实现 controller 接口注册的 controller 适配器 -- 直接强转调用重写的方法执行

//3.执行方法

猜你喜欢

转载自blog.csdn.net/qq_38108719/article/details/103443255
今日推荐