是SpringFramework为咱们提供的拦截器,通常咱们能够用来鉴权或者日志记录等。java
它是一个interface,主要方法有:spring
/** * Intercept the execution of a handler. Called after HandlerMapping determined * an appropriate handler object, but before HandlerAdapter invokes the handler. * <p>DispatcherServlet processes a handler in an execution chain, consisting * of any number of interceptors, with the handler itself at the end. * With this method, each interceptor can decide to abort the execution chain, * typically sending a HTTP error or writing a custom response. * @param request current HTTP request * @param response current HTTP response * @param handler chosen handler to execute, for type and/or instance evaluation * @return {@code true} true表示继续流程(如调用下一个拦截器或处理器);false表示流程中断 *(如登陆检查失败),不会继续调用其余的拦截器或处理器,此时咱们须要经过response来产生响应 * @throws Exception in case of errors */ boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; /** * 后处理回调方法,实现处理器的后处理(但在渲染视图以前),此时咱们能够经过modelAndView(模型和视图对象)对模型数据进行处理或对视图进行处理,modelAndView也可能为null。 * Intercept the execution of a handler. Called after HandlerAdapter actually * invoked the handler, but before the DispatcherServlet renders the view. * Can expose additional model objects to the view via the given ModelAndView. * <p>DispatcherServlet processes a handler in an execution chain, consisting * of any number of interceptors, with the handler itself at the end. * With this method, each interceptor can post-process an execution, * getting applied in inverse order of the execution chain. * @param request current HTTP request * @param response current HTTP response * @param handler handler (or {@link HandlerMethod}) that started async * execution, for type and/or instance examination * @param modelAndView the {@code ModelAndView} that the handler returned * (can also be {@code null}) * @throws Exception in case of errors */ void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception; /** * Callback after completion of request processing, that is, after rendering * the view. Will be called on any outcome of handler execution, thus allows * for proper resource cleanup. * <p>Note: Will only be called if this interceptor's {@code preHandle} * method has successfully completed and returned {@code true}! * <p>As with the {@code postHandle} method, the method will be invoked on each * interceptor in the chain in reverse order, so the first interceptor will be * the last to be invoked. * @param request current HTTP request * @param response current HTTP response * @param handler handler (or {@link HandlerMethod}) that started async * execution, for type and/or instance examination * @param ex exception thrown on handler execution, if any * @throws Exception in case of errors */ void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception;
preHandle 预处理回调,在处理器处理以前回调设计模式
postHandle 处理器处理完毕,在渲染以前回调,能够计算处理器处理时间app
afterCompletion 请求渲染结束后回调 这边咱们能够计算渲染时间,或者整个请求的执行时间异步
有时候咱们可能只须要实现三个回调方法中的某一个,若是实现HandlerInterceptor接口的话,三个方法必须实现,无论你需不须要,此时spring提供了一个HandlerInterceptorAdapter适配器(适配器设计模式的实现),容许咱们只实现须要的回调方法。async
public abstract class HandlerInterceptorAdapter implements AsyncHandlerInterceptor { /** * This implementation always returns {@code true}. */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } /** * This implementation is empty. */ @Override public void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } /** * This implementation is empty. */ @Override public void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } /** * This implementation is empty. * 当Controller中有异步请求方法的时候会触发该方法。 楼主作过测试,异步请求先支持preHandle、而后执行afterConcurrentHandlingStarted */ @Override public void afterConcurrentHandlingStarted( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { } }
运行流程总结以下:ide
一、拦截器执行顺序是按照Spring配置文件中定义的顺序而定的。post
二、会先按照顺序执行全部拦截器的preHandle方法,一直遇到return false为止,好比第二个preHandle方法是return false,则第三个以及之后全部拦截器都不会执行。若都是return true,则按顺序加载完preHandle方法。测试
三、而后执行主方法(本身的controller接口),若中间抛出异常,则跟return false效果一致,不会继续执行postHandle,只会倒序执行afterCompletion方法。优化
四、在主方法执行完业务逻辑(页面还未渲染数据)时,按倒序执行postHandle方法。若第三个拦截器的preHandle方法return false,则会执行第二个和第一个的postHandle方法和afterCompletion(postHandle都执行完才会执行这个,也就是页面渲染完数据后,执行after进行清理工做)方法。(postHandle和afterCompletion都是倒序执行
其实从上面的HandlerInterceptor到HandlerInterceptorAdapter,咱们能够看出这是使用了接口适配器模式,接口Adapter对接口进行实现,可是又不作具体的逻辑实现。
这样咱们在使用的时候只要继承对应的Adapter复写须要的方法便可
上面那种在Java8以前常常会用到,但是在Java8新增default方法后,就不多用到了。前提最低支持Java8
Java8的新特性中有一个新特性为接口默认方法,该新特性容许咱们在接口中添加一个非抽象的方法实现,而这样作的方法只须要使用关键字default修饰该默认实现方法便可。该特性又叫扩展方法
public interface Formula { double calculate(int a); default double sqrt(int a){ return Math.sqrt(a); } }
经过该特性,咱们将可以很方便的实现接口默认实现类。这个特性在编译器实现的角度来讲更接近于Scala的trait
若是你们有用过SpringBoot2.0会发现有大量的方法或者类被从新设计,固然一部分是对1.0的接口优化或者功能优化。
其中一个很重要的是对JDK的要求,也就是说Spring Boot2.0的最低版本要求为JDK8。从代码中咱们能够看出,不少地方使用了default方法,对于老的接口Adapter进行了舍弃。
好比咱们之前在配置相应的HandlerInterceptor使用的WebMvcConfigurerAdapter,就被标记为Deprecated
/** * An implementation of {@link WebMvcConfigurer} with empty methods allowing * subclasses to override only the methods they're interested in. * * @author Rossen Stoyanchev * @since 3.1 * @deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made * possible by a Java 8 baseline) and can be implemented directly without the * need for this adapter */ @Deprecated public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer
接下来咱们来看下WebMvcConfigurer在SpringBoot2.0和SpringBoot1.0中的代码区别:
SpringBoot2.0中使用的SpringFramework5
public interface WebMvcConfigurer { default void configurePathMatch(PathMatchConfigurer configurer) { } default void configureContentNegotiation(ContentNegotiationConfigurer configurer) { } default void configureAsyncSupport(AsyncSupportConfigurer configurer) { } default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { } default void addFormatters(FormatterRegistry registry) { } default void addInterceptors(InterceptorRegistry registry) { } default void addResourceHandlers(ResourceHandlerRegistry registry) { } default void addCorsMappings(CorsRegistry registry) { } default void addViewControllers(ViewControllerRegistry registry) { } default void configureViewResolvers(ViewResolverRegistry registry) { } default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { } default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) { } default void configureMessageConverters(List<HttpMessageConverter<?>> converters) { } default void extendMessageConverters(List<HttpMessageConverter<?>> converters) { } default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) { } default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) { } @Nullable default Validator getValidator() { return null; } @Nullable default MessageCodesResolver getMessageCodesResolver() { return null; } }
SpringBoot1.0中使用的SpringFramework4
public interface WebMvcConfigurer { void addFormatters(FormatterRegistry registry); void configureMessageConverters(List<HttpMessageConverter<?>> converters); void extendMessageConverters(List<HttpMessageConverter<?>> converters); Validator getValidator(); void configureContentNegotiation(ContentNegotiationConfigurer configurer); void configureAsyncSupport(AsyncSupportConfigurer configurer); void configurePathMatch(PathMatchConfigurer configurer); void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers); void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers); void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers); void addInterceptors(InterceptorRegistry registry); MessageCodesResolver getMessageCodesResolver(); void addViewControllers(ViewControllerRegistry registry); void configureViewResolvers(ViewResolverRegistry registry); void addResourceHandlers(ResourceHandlerRegistry registry); void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer); }
其中惟一的区别就是在SpringFramework5中使用了default关键字,致使的结果就是WebMvcConfigurerAdapter能够被舍弃不使用了。
固然在Spring中的例子还有不少,好比HandlerInterceptor和HandlerInterceptorAdapter等,你们在使用的时候除了要看相应的接口变化,更重要的是要去了解为何会有这种变化
@Configuration public class WebConf implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new AsyncInterceptor()).addPathPatterns("/*"); } }