欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-15 15:39:19
...

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中。

参考:filter和拦截器的区别和执行顺序