前提:拦截器加载顺序是 1 2 3.
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
private Test1neInterceptor test1neInterceptor;
@Autowired
private Test2neInterceptor test2neInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(test1neInterceptor).addPathPatterns("/**");
registry.addInterceptor(test2neInterceptor).addPathPatterns("/**");
}
}
执行顺序如图所示。一个请求从web发起,在请求处理之前被拦截器1的preHandle调用
依次1preHandle--->2preHandle--->3preHandle--->controller中目标方法
3postHandle--->2postHandle--->1postHandle--->页面渲染ModelAndView
3afterCompletion--->2afterCompletion--->1afterCompletion
preHandle要返回都为true 才能依次执行,否则直接 3afterCompletion--->2afterCompletion--->1afterCompletion
@Component
public class Test1neInterceptor extends HandlerInterceptorAdapter {
public void HandlerInterceptorAdapter() {
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//预处理,在业务处理之前被调用,返回true继续执行,返回false中断执行
System.out.println("*******************preHandle1****************************");
System.out.println("Thread preHandle1:"+Thread.currentThread().getId());
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// 在业务处理之后,视图生成之前被调用
System.out.println("*******************postHandle1****************************");
System.out.println("Thread postHandle1:"+Thread.currentThread().getId());
System.out.println("postHandle1 controller....处理后....视图生成之前");
System.out.println("modelAndView:"+modelAndView);
modelAndView.addObject("name", "Hello World!!!!!");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
// 在视图生成之后被调用
System.out.println("*******************afterCompletion1****************************");
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("*******************afterConcurrentHandlingStarted1 有异步调用****************************");
System.out.println("Thread afterConcurrentHandlingStarted1:"+Thread.currentThread().getId());
}
}
@Component
public class Test2neInterceptor extends HandlerInterceptorAdapter {
public void HandlerInterceptorAdapter() {
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//预处理,在业务处理之前被调用,返回true继续执行,返回false中断执行
System.out.println("*******************preHandle2****************************");
System.out.println("Thread preHandle2:"+Thread.currentThread().getId());
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// 在业务处理之后,视图生成之前被调用
System.out.println("*******************postHandle2****************************");
System.out.println("Thread postHandle2:"+Thread.currentThread().getId());
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
// 在视图生成之后被调用
System.out.println("*******************afterCompletion2****************************");
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("*******************afterConcurrentHandlingStarted2 有异步调用****************************");
System.out.println("Thread afterConcurrentHandlingStarted2:"+Thread.currentThread().getId());
}
}
@RequestMapping("/show2")
public AsyncResult<String> test2(Model model) throws InterruptedException {
System.out.println("进入 controller1......");
System.out.println("Thread controller"+Thread.currentThread());
String name = "mary";
model.addAttribute("name",name);
return new AsyncResult<>("show");
}
@RequestMapping("/show3")
public StreamingResponseBody test3(Model model) throws InterruptedException {
System.out.println("进入 controller1......");
System.out.println("Thread controller"+Thread.currentThread());
String name = "mary";
model.addAttribute("name",name);
return (OutputStream outputStream) -> {
outputStream.write("streaming".getBytes());
outputStream.flush();
outputStream.close();
};
}
@RequestMapping("/show4")
public String test4(ModelAndView modelAndView) throws InterruptedException {
System.out.println("进入 controller1......");
System.out.println("Thread controller"+Thread.currentThread());
String name = "mary";
System.out.println("进入 modelAndView11......");
modelAndView.addObject("name",name);
System.out.println("进入 modelAndView112......");
return "show";
}