spring中過濾器(filter)、攔截器(interceptor)和切面(aop)的執行順序
阿新 • • 發佈:2018-11-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