spring中过滤器(filter)、拦截器(interceptor)和切面(aop)的执行顺序

1.程序执行的顺序是先进过滤器,再进拦截器,最后进切面。注意:如果拦截器中preHandle方法返回的为false时,则无法进入切面,例子如下

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/{id:\\d+}")
	@JsonView(User.UserDetailView.class)
	public User getInfo(@PathVariable String id) {
		User user = new User();
		user.setUsername("tom");
		return user;
	}
}

编写方法的过滤器:

@Component
public class TimeFilter implements Filter {

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("time filter init");

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("time filter start");
		long start = new Date().getTime();
		chain.doFilter(request, response);
		System.out.println("time filter:"+ (new Date().getTime() - start));
		System.out.println("time filter finish");

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
		System.out.println("time filter destroy");

	}

}

编写方法的拦截器:

@Component
public class TimeInterceptor implements HandlerInterceptor {

	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("preHandle");
		System.out.println(((HandlerMethod)handler).getBean().getClass().getName());
		System.out.println(((HandlerMethod)handler).getMethod().getName());
		request.setAttribute("startTime", new Date().getTime());
		return true;
	}

	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("postHandle");
		Long start = (Long)request.getAttribute("startTime");
		System.out.println("time interceptor 耗时:"+ (new Date().getTime() - start));
	}

	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("afterCompletion");
		Long start = (Long) request.getAttribute("startTime");
		System.out.println("time interceptor 耗时:"+ (new Date().getTime() - start));
		System.out.println("ex is "+ex);
	}

}

编写方法的切面:

@Aspect
@Component
public class TimeAspect {

	@Before("execution(public * com.hu.web.controller.UserController.*(..))")
	public Object handlerControllerMethod() throws Throwable {
		System.out.println("time aspect start");			
		return new Object();
	}
}

运行结果:1、当拦截器中preHandle的方法返回为true时:

time filter start
preHandle
com.hu.web.controller.UserController$$EnhancerBySpringCGLIB$$b0bb0dcf
getInfo
time aspect start
postHandle
time interceptor 耗时:76
afterCompletion
time interceptor 耗时:76
ex is null
time filter:92
time filter finish
time filter start
time filter:46
time filter finish

2、当拦截器中preHandle的方法返回为false时:

time filter start
preHandle
com.hu.web.controller.UserController$$EnhancerBySpringCGLIB$$b0bb0dcf
getInfo
time filter:12
time filter finish
time filter start
time filter:23
time filter finish

2.当程序有异常抛出时,回先进入切面,然后在进入自定义的ControllerAdvice中。

欢迎关注本人公众号,一起学习,交流。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值