SpringMVC拦截器详解
拦截器是每个Web框架必备的功能,也是个老生常谈的主题了。 本文将分析SpringMVC的拦截器功能是如何设计的,让读者了解该功能设计的原理。 重要接口及类介绍 1. HandlerExecutionChain类 由HandlerMethod和Interceptor集合组成的类,会被HandlerMapping接口的getHandler方法获取。 2. HandlerInterceptor接口 SpringMVC拦截器基础接口。 3. AbstractHandlerMapping HandlerMapping的基础抽象类。 4. AsyncHandlerInterceptor 继承HandlerInterceptor的接口,额外提供了afterConcurrentHandlingStarted方法,该方法是用来处理异步请求。当Controller中有异步请求方法的时候会触发该方法。 楼主做过测试,异步请求先支持preHandle、然后执行afterConcurrentHandlingStarted。异步线程完成之后执行preHandle、postHandle、afterCompletion。 有兴趣的读者可自行研究。 5. HandlerInterceptorAdapter 实现AsyncHandlerInterceptor接口的抽象类