SpringMVC 拦截器实现原理

1.SpringMVC 拦截器的原理图

单个拦截器方法执行顺序

多个拦截器方法执行顺序

  Spring MVC 也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能 ,自定义的拦截器必须实现HandlerInterceptor接口 
  • preHandle():这个方法在业务处理器处理请求之前被调用,在该方法中对用户请求 request 进行处理。如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;如果程序员决定不需要再调用其他的组件去处理请求,则返回false。
  • postHandle()这个方法在业务处理器处理完请求后,是在DispatcherServlet 向客户端返回响应前被调用,在该方法中对用户请求request进行处理。
  • afterCompletion():这个方法在 DispatcherServlet 完全处理完请求后被调用,可以在该方法中进行一些资源清理的操作。

2.代码测试

springmvc.xml配置 (此处演示多个拦截器的执行)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<context:component-scan base-package="com.hbsi"></context:component-scan>
	<!-- 配置视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
		<property name="order" value="1000"></property>
	</bean>
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
		<property name="order" value="100"></property>

	</bean>
	<mvc:annotation-driven />
	<!-- 配置拦截器 会对客户端所有的请求进行拦截 <mvc:exclude-mapping> 配置不拦截的请求 -->
	<mvc:interceptors>
		<!--配置拦截器具体要拦截的请求 
		    <mvc:interceptor> <mvc:mapping path="/testInterceptor"/> 拦截的请求 
		    <mvc:exclude-mapping path="/testInterceptor"/> 不拦截的请求 <bean class="com.hbsi.interceptor.MyfristInterceptor"></bean> 
			</mvc:interceptor> -->
		<bean class="com.hbsi.interceptor.MyfristInterceptor"></bean>
		<bean class="com.hbsi.interceptor.MysecondInterceptor">
		</bean>
	</mvc:interceptors>
</beans>

自定义拦截器

MyfristInterceptor

package com.hbsi.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.PortableInterceptor.Interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

//自定义拦截器
public class MyfristInterceptor implements HandlerInterceptor {
	// 在业务处理器处理请求之前被调用
	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("MyFirstInterceptor prehandle.......");
		return true;
	}

	// 在业务处理器处理完请求后,dispatcherServlet向客户端返回响应前被调用
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("MyFirstInterceptor posthandle.......");
	}

	// 在dispatcherServlet完全处理请求后被调用,可以在该方法中进行一些资源清理的操作
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("MyFirstInterceptor afterCompletion.......");
	}

}

MysecondInterceptor

//自定义拦截器
public class MysecondInterceptor implements HandlerInterceptor {
	// 在业务处理器处理请求之前被调用
	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("MysecondInterceptor prehandle.......");
		return true;
	}

	// 在业务处理器处理完请求后,dispatcherServlet向客户端返回响应前被调用
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("MysecondInterceptor2 posthandle.......");
	}

	// 在dispatcherServlet完全处理请求后被调用,可以在该方法中进行一些资源清理的操作
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("MysecondInterceptor2 afterCompletion.......");
	}

}

controller


@Controller
public class TestInterceptor {
	@RequestMapping("/testInterceptor")
  public String testInterceptor(){
	  System.out.println("testInterceptor.......");
	  return "forward:/success.jsp";
  }
}

① 多个拦截器的执行顺序:  拦截器的执行顺序跟在配置文件中配置的顺序有关,配置越靠前,执行越靠前.

          [1]. preHandle: 根据拦截器的指定顺序来执行.

          [2].postHandle: 根据拦截器的执行顺序 逆序执行.

          [3].afterCompletion; 根据拦截器的执行顺序逆序执行

执行结果:

MyFirstInterceptor prehandle.......
MysecondInterceptor prehandle.......
testInterceptor.......
MysecondInterceptor2 posthandle.......
MyFirstInterceptor posthandle.......
MysecondInterceptor2 afterCompletion.......
MyFirstInterceptor afterCompletion.......

  ②某个拦截器的preHandle方法返回false后,后续的请求处理方法,postHandle方法就不在执行了。

     当前拦截器之前的拦截器的afterCompletion方法会执行.

      此时把MysecondInterceptor的preHandel方法返回值改为false

演示图

执行结果为:

MyFirstInterceptor prehandle.......
MysecondInterceptor prehandle.......
MyFirstInterceptor afterCompletion.......

 

 

   

 

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值