HandlerInterceptor 在这里看到这个HandlerExecutionChain对interceptor的调用,在这里深入看一下。

HandlerExecutionChain 就是一个类,绑定了Handler( 对应的Controller) 和 Interceptors , 所以作用就是对Controller前后执行interceptors, 类似Filter

几个问题:

1. interceptor调用位置?

Spring HandlerInterceptor实现原理代码解析

1 2 3 对应调用 handlerInterceptor preHandle, postHandle, afterCompletion

先看第一行的:

Spring HandlerInterceptor实现原理代码解析

进到对应的interceptor实现类,preHandler返回true, 如果返回false, 就会到DispatcherServlet就直接return了。

Spring HandlerInterceptor实现原理代码解析

看第二行没有特别的:

Spring HandlerInterceptor实现原理代码解析

由于它只重写了preHandle

Spring HandlerInterceptor实现原理代码解析

所以走到它父类的postHandle了。方法是空的。

Spring HandlerInterceptor实现原理代码解析

第三行和第二行一样不赘述了。

以上就是执行位置。

所以如果实现HandlerInterceptor , preHandle 应该返回true, 具体要拦截的内容写在return true之前就行。 postHandle和afterCompletion 都是void方法,直接在里面写需要拦截的内容。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持码农网。

您可能感兴趣的文章:
  • springboot themaleaf 第一次进页面不加载css的问题
  • SpringBoot配置绑定方法详解
  • SpringBoot本地磁盘映射问题
  • spring boot国际化之MessageSource的使用方法
  • SpringBoot参数校验与国际化使用教程
  • Spring Boot Maven Plugin打包异常解决方案
  • Spring Aop如何给Advice传递参数
  • spring boot拦截器注入不了java bean的原因