Spring MVC源码解读四

接上一篇:Spring MVC源码解析三

在上一篇对于DispatcherServlet中的doDispatch方法解读时,可以看到是通过以下的代码实现根据request获取handler的

// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);

我们可以看下getHandler是怎么处理的:

    /**
     * Return the HandlerExecutionChain for this request.
     * 

Tries all handler mappings in order. * @param request current HTTP request * @return the HandlerExecutionChain, or {@code null} if no handler could be found */ protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { for (HandlerMapping hm : this.handlerMappings) { if (logger.isTraceEnabled()) { logger.trace( "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'"); } HandlerExecutionChain handler = hm.getHandler(request); if (handler != null) { return handler; } } return null; }

可以看到通过循环调用this.handlerMappings中的HandlerMapping实例的getHandler方法返回HandlerExecutionChain实例,这里必然有几个问题需要解答:
为什么返回的是一个HandlerExecutionChain的实例,而不是一个“Handler”类? HandlerMapping的getHandler方法如何实现的?

首先看HandlerExecutionChain这个类:

/**
 * Handler execution chain, consisting of handler object and any handler interceptors.
 * Returned by HandlerMapping's {@link HandlerMapping#getHandler} method.
 *
 * @author Juergen Hoeller
 * @since 20.06.2003
 * @see HandlerInterceptor
 */
public class HandlerExecutionChain {
        ......
    private final Object handler;

    private HandlerInterceptor[] interceptors;

    private List interceptorList;

    private int interceptorIndex = -1;
        ......
}

通过类的注释可以看到,HandlerExecutionChain是由handler对象和handler拦截器对象(注意这里是复数)组成的:
HandlerExecutionChain中包含handler对象,这个好理解,getHandler原本的目的就是通过url获取到对应的handler,那么handler拦截器(handler interceptors是干嘛的)
HandlerExecutionChain中的field:interceptors和interceptorList便是handler拦截器的容器,拦截器对应的类是HandlerInterceptor,我们看下它做了什么事情
这里有个需要注意的地方,HandlerExecutionChain中为什么定义了interceptors和interceptorList2个属性,他们的区别是什么?不细讲了,有兴趣的可以自己去看HandlerExecutionChain的源码
HandlerInterceptor的源码:

public interface HandlerInterceptor {
    default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        return true;
    }

    default void postHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {
    }

    default void afterCompletion(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }

}

HandlerInterceptor是一个接口,定义了3个回调接口:

  • preHandle:在handler执行前调用
  • postHandle:在handler执行,View调用render接口前后调用
  • afterCompletion:在handler执行完成,切View调用render结束后调用
    这里其实拦截器的基本用途已经很清晰了,类似于proxy,在handler调用前后进行一些操作,如果用户想在controller的方法执行前实现一些通用的日志操作,就可以去实现HandlerInterceptor接口(具体代码如何写需要做什么配置,这里不细化了,搜下就清楚了)
    关于getHandler的返回值类型HandlerExecutionChain的解读到此结束了,下一章继续讲HandlerMapping和它的getHandler接口的逻辑处理

你可能感兴趣的:(Spring MVC源码解读四)