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

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,前面就是做了一些精益求精的事,最初调用该办法了。

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理