关于springboot:SpringBoot源码mvc工作流程中

39次阅读

共计 3755 个字符,预计需要花费 10 分钟才能阅读完成。

DispatcherServlet 解决流程

上一节讲了 Spring 容器启动,会把 url 与类办法的映射关系保存起来,这一节,就能看到它的作用啦。

DispatcherServlet 是整个 SpringMVC 的外围,负责申请解决以及返回响应的工作。直奔主题,找到 DispatcherServlet 类,再找到该类下的 doService() 办法,要害代码:

try {doDispatch(request, response);
}

点击这个孤单的办法,进去,上外围马:

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpServletRequest processedRequest = request;
    // 定义 handler 执行链,重要
    HandlerExecutionChain mappedHandler = null;
    boolean multipartRequestParsed = false;

    ModelAndView mv = null;
    // 查看是否是文件申请
    processedRequest = checkMultipart(request);
    multipartRequestParsed = (processedRequest != request);

    // 获取以后申请的 handler
    mappedHandler = getHandler(processedRequest);
    if (mappedHandler == null) {noHandlerFound(processedRequest, response);
        return;
    }

    // 获取以后申请的适配器
    HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

    // 判断 get 办法是否有批改,没有间接返回
    String method = request.getMethod();
    boolean isGet = HttpMethod.GET.matches(method);
    if (isGet || HttpMethod.HEAD.matches(method)) {long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
        if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {return;}
    }
    // 前置解决
    if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}

    // hander 理论执行
    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

    applyDefaultViewName(processedRequest, mv);
    // handler 后置解决
    mappedHandler.applyPostHandle(processedRequest, response, mv);
    // 解决散发后的后果
    processDispatchResult(processedRequest, response, mappedHandler, mv,     
                          dispatchException);

}

看到那个闪亮的办法没 – getHandler(processedRequest),点击进去,下马:

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {if (this.handlerMappings != null) {for (HandlerMapping mapping : this.handlerMappings) {
            // 从 handlerMappings 获取并构建申请的执行链
            HandlerExecutionChain handler = mapping.getHandler(request);
            if (handler != null) {return handler;}
        }
    }
    return null;
}

打个 debug,看下 handlerMappings 外面是啥

看到没,这不就是上一节讲到的东东咩?

再点击 mapping.getHandler(request) 办法进去,看看有啥:

public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    // 获取 handler
    Object handler = getHandlerInternal(request);
    ……
        
    // 是 beanName 获取对应的 bean
    if (handler instanceof String) {String handlerName = (String) handler;
        handler = obtainApplicationContext().getBean(handlerName);
    }
    ……
        
    // 构建 handler 的执行链
    HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);    
    ……

    return executionChain;
}

getHandlerInternal(request) 是重点,点进去:

protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
    // 获取拜访门路,如 /test/hello
    String lookupPath = initLookupPath(request);
    this.mappingRegistry.acquireReadLock();
    try {
        // 查找拜访门路对应的解决办法
        HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
        return (handlerMethod != null ? handlerMethod.createWithResolvedBean() : null);
    }
    finally {this.mappingRegistry.releaseReadLock();
    }
}

快到了!持续点击 lookupHandlerMethod(lookupPath, request),进入:

protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {List<Match> matches = new ArrayList<>();
    // 依据拜访门路匹配申请办法
    List<T> directPathMatches = this.mappingRegistry.getMappingsByDirectPath(lookupPath);
    if (directPathMatches != null) {
        // 依据找到的信息,封装 matches
        addMatchingMappings(directPathMatches, matches, request);
    }
    if (matches.isEmpty()) {addMatchingMappings(this.mappingRegistry.getRegistrations().keySet(), matches, request);
    }
    
    // 省略查找最近匹配内容
    ……
    
    // 返回 handlerMethod
    request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, bestMatch.getHandlerMethod());
    handleMatch(bestMatch.mapping, lookupPath, request);
    return bestMatch.getHandlerMethod();}

点击 this.mappingRegistry.getMappingsByDirectPath(lookupPath),进入:

public List<T> getMappingsByDirectPath(String urlPath) {return this.pathLookup.get(urlPath);
}

看到了没,pathLookup 退场了,T 类型是 RequestMappingInfo。

这一步,就能找到 handlerMethod 了,也就是拜访门路对应的解决办法。所以下面的 getHandler() 办法就能返回对应的 HandlerExecutionChain。

回到 doDispatch() 办法中,既然拿到了 handlerMethod,前面就是做了一些精益求精的事,最初调用该办法了。

不讲了,口渴,饮杯开水去~~

正文完
 0