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,后面就是做了一些锦上添花的事,最后调用该方法了。

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


知九千
4 声望1 粉丝

让技术,变得简单