spring

Spring MVC源码解读四

2017-03-08  本文已影响0人  测试你个头

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

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

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

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

    /**
     * Return the HandlerExecutionChain for this request.
     * <p>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<HandlerInterceptor> 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个回调接口:

上一篇下一篇

猜你喜欢

热点阅读