HandlerInterceptor与MethodInterceptor
HandlerInterceptor是springMVC项目中的拦截器,它拦截的目标是请求的地址,比MethodInterceptor先执行。实现一个HandlerInterceptor拦截器可以直接实现HandlerInterceptor接口,也可以继承HandlerInterceptorAdapter类。这两种方法殊途同归,其实HandlerInterceptorAdapter也就是声明了HandlerInterceptor接口中所有方法的默认实现,而我们在继承他之后只需要重写必要的方法。下面就是HandlerInterceptorAdapter的代码,可以看到一个方法只是默认返回true,另外两个是空方法
- public abstract class HandlerInterceptorAdapter implements HandlerInterceptor {
- /**
- * This implementation always returns <code>true</code>.
- */
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
- throws Exception {
- return true;
- }
- /**
- * This implementation is empty.
- */
- public void postHandle(
- HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
- throws Exception {
- }
- /**
- * This implementation is empty.
- */
- public void afterCompletion(
- HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
- throws Exception {
- }
- }
- MethodInterceptor是AOP项目中的拦截器,它拦截的目标是方法,即使不是controller中的方法。实现MethodInterceptor拦截器大致也分为两种,一种是实现MethodInterceptor接口,另一种利用AspectJ的注解或配置
- public class MethodInvokeInterceptor implements MethodInterceptor {
- @Override
- public Object invoke(MethodInvocation methodInvocation) throws Throwable {
- System.out.println("before method invoke");
- Object object = methodInvocation.proceed();
- System.out.println("after method invoke");
- return object;
- }
- }
下面是基于注解的AspectJ方式
- @Component
- @Aspect
- public class AutoAspectJInterceptor {
- @Around("execution (* com.test.controller..*.*(..))")
- public Object around(ProceedingJoinPoint point) throws Throwable{
- System.out.println("AutoAspectJInterceptor begin around");
- Object object = point.proceed();
- System.out.println("AutoAspectJInterceptor end around");
- return object;
- }
- }
下面是一个用于支持AspectJ方式拦截的普通的bean,当然你也可以在配置文件中声明这个bean
- @Component
- public class AspectJInterceptor {
- public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
- System.out.println("AspectJInterceptor around before");
- Object object = proceedingJoinPoint.proceed();
- System.out.println("AspectJInterceptor around after");
- return object;
- }
- }
当然,这一切都离不开配置,具体看配置中的注释
- <!-- 自定义拦截器 ,先过mvc:interceptors-->
- <bean id="methodInvokeInterceptor" class="com.test.interceptor.MethodInvokeInterceptor"/>
- <bean id="aspectInterceptor" class="com.test.interceptor.AspectJInterceptor"/>
- <aop:config>
- <!--切入点,controlller -->
- <aop:pointcut id="pointcut_test" expression="execution(* com.test.controller..*.*(..))" />
- <!--在该切入点使用自定义拦截器 ,按照先后顺序执行 -->
- <aop:advisor pointcut-ref="pointcut_test" advice-ref="methodInvokeInterceptor" />
- <aop:aspect ref="aspectInterceptor">
- <aop:around method="around" pointcut="execution(* com.test.controller..*.*(..))"/>
- </aop:aspect>
- </aop:config>
- <!-- 自动扫描使用了aspectj注解的类 -->
- <aop:aspectj-autoproxy/>
HandlerInterceptor与MethodInterceptor的更多相关文章
- 干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结
目录 C# VS JAVA 基础语法类比篇: 一.匿名类 二.类型初始化 三.委托(方法引用) 四.Lambda表达式 五.泛型 六.自动释放 七.重写(override) ASP.NET CORE ...
- ASP.NET CORE(C#)与Spring Boot MVC(JAVA)
干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结 目录 C# VS JAVA 基础语法类比篇: 一.匿名类 二.类型初始化 三.委托(方 ...
- springboot拦截器总结
Springboot 拦截器总结 拦截器大体分为两类 : handlerInterceptor 和 methodInterceptor 而methodInterceptor 又有XML 配置方法 和A ...
- (转)spring中的拦截器(HandlerInterceptor+MethodInterceptor)
1. 过滤器跟拦截器的区别 在说拦截器之前,不得不说一下过滤器,有时候往往被这两个词搞的头大. 其实我们最先接触的就是过滤器,还记得web.xml中配置的<filter>吗~ 你应该知道 ...
- Spring 拦截器实现+后台原理(MethodInterceptor)
MethodInterceptor MethodInterceptor是AOP项目中的拦截器(注:不是动态代理拦截器),区别与HandlerInterceptor拦截目标时请求,它拦截的目标是方法. ...
- Spring 拦截器实现+后台原理(HandlerInterceptor)
过滤器跟拦截器的区别 spring mvc的拦截器是只拦截controller而不拦截jsp,html 页面文件的.这就用到过滤器filter了,filter是在servlet前执行的,你也可以理解成 ...
- springmvc(3)拦截器HandlerInterceptor源码的简单解析
其实拦截器就是我们的AOP编程.拦截器在我们的实际项目中实用性比较大的,比如:日志记录,权限过滤,身份验证,性能监控等等.下面就简单的来研究一下拦截器: public interface Handle ...
- Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例
利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器 ...
- Spring3 报org.aopalliance.intercept.MethodInterceptor问题解决方法
原文:Spring3 报org.aopalliance.intercept.MethodInterceptor问题解决方法 一 开发环境:JDK5+Spring3.0.5+Myeclipse6.6+T ...
随机推荐
- [Oracle]Oracle数据库数据被修改或者删除恢复数据
1.SELECT * FROM CT_FIN_RiskItem--先查询表,确定数据的确不对(cfstatus第一行缺少)2.select * from CT_FIN_RiskItem as of t ...
- Kubernetes 集群安装部署
etcd集群配置 master节点配置 1.安装kubernetes etcd [root@k8s ~]# yum -y install kubernetes-master etcd 2.配置 etc ...
- 和我一起学python
近来python越来越火,很多人都出了教程,我也来出一个凑凑热闹吧. pycharm激活 http://idea.lanyus.com/ https://blog.csdn.net/u01404481 ...
- GO学习笔记 - Go 只有一种循环结构—— for 循环。
一,Go 只有一种循环结构—— for 循环. 官方教程:https://tour.go-zh.org/flowcontrol/1 Go 只有一种循环结构—— for 循环. 基本的 for 循环包含 ...
- ocp认证052最新题库-收集整理中-1
1..Which two are true about the Archive (ARCn) processes?❑ A) They automatically delete obsolete arc ...
- Python的科学计算包matplotlib setup
回想起大学四年 专业一直使用matlab,然而我却没在PC上装成功过,以前懒于思考这种数学工具的作用,直到最近,大学同学研究生要毕业了,几经交流,和自己阅读了一些机器学习的教材之后,发觉科学计算包和画 ...
- string类型介绍
一.前言 int,float,char,C++标准库提供的类型:string,vector. string:可变长字符串的处理:vector一种集合或者容器的概念. 二.string类型简介 C++标 ...
- css元素垂直居中的8中方法
1. 通过vertical-align:middle实现CSS垂直居中 通过vertical-align:middle实现CSS垂直居中是最常使用的方法,但是有一点需要格外注意,vertical生效的 ...
- ArrayList分析
ArrayList概述 ArrayList继承了AbstractList,实现了List接口,底层基于动态数组,容量大小可以动态变化,ArrayList中可以添加null元素,另外,ArrayList ...
- 基于nightmare的美团美食商家爬虫实践
前言美团商家页分析需要爬取的数据有(这里没有按人数爬)爬虫工具选取pysipderscrapynightmare同步任务js动态加载中断继续爬坑总结示例代码 前言 上学的时候自己写过一些爬虫代码,比较 ...