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 ...
随机推荐
- ES6——内置对象的扩展
字符串的扩展 //模版字符串 let flag=true; let hml=`<ul> <li> <span></span> <span>& ...
- 'Install app for SharePoint': Sideloading of apps is not enabled on this site
http://blog.lekman.com/2012/11/sharepoint-2013-sideloading-of-apps-is.html Solution: You need to ena ...
- 「HNOI 2015」实验比较
\(Description\) 有\(n\)个元素,对于每个元素\(x_i\)最多知道一个形如\(x_j < x_i\)或\(x_j=x_i\)的条件,问有多少合法的序列.合法的序列满足每个元素 ...
- 【Oracle 12c】最新CUUG OCP-071考试题库(55题)
55.(13-3) choose the best answer: Which statement is true regarding the SESSION_PRIVS dictionary vie ...
- 【12c OCP】CUUG OCP认证071考试原题解析(34)
34.choose two View the Exhibit and examine the structure of the PRODUCT_INFORMATION and INVENTORIES ...
- 【Oracle 12c】CUUG OCP认证071考试原题解析(30)
30.choose the best answer Examine the commands used to create DEPARTMENT_DETAILS and COURSE_DETAILS: ...
- 操作日期时间类 Calendar类
使用Calendar类可以直接创建Calendar的子类GregorianCalendar 来直接实例化, GregorianCalendar calendar = new GregorianCal ...
- SQL中DateTime转换成Varchar样式
SQL中DateTime转换成Varchar样式语句及查询结果:Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect ...
- DataList用法总结
设计模版: 页眉<HeaderTemplate> </HeaderTemplate> 页脚<FooterTemplate> </FooterTemplat ...
- Eclipse署动态web项目方法
和MyEclipse不一样,在Eclipse中做的Web项目默认是不支持将项目发布到Web服务器上的,会发布到工作空间的某个目录,因此无法在外部启动Tomcat来运行Web项目,只有打开Eclipse ...