一般我看设置拦截器,都是继承HandlerInterceptor类,而老师您却采用到的是继承HandlerInterceptorAdapter,我看了下源码发现HandlerInterceptorAdapter是实现了AsyncHandlerInterceptor接口,而这接口也是继承了HandlerInterceptor类,就是相比于HandlerInterceptor多了一个afterConcurrentHandlingStarted方法,我大体看了下英文注释,貌似主要用于异步请求处理的场景。该方法注释如下:
<p>When a handler starts an asynchronous request, the {@link DispatcherServlet}
* exits without invoking {@code postHandle} and {@code afterCompletion} as it
* normally does for a synchronous request, since the result of request handling
* (e.g. ModelAndView) is likely not yet ready and will be produced concurrently
* from another thread. In such scenarios, {@link #afterConcurrentHandlingStarted}
* is invoked instead, allowing implementations to perform tasks such as cleaning
* up thread-bound attributes before releasing the thread to the Servlet container.
这段话,自己翻译后,还是感觉理解得不透彻,比如“请求处理结果(例如ModelAndView)可能还没有准备好,且将同从另一个线程里产生,在这样的情况下使用afterConcurrentHandlingStarted方法”就总感觉自己翻译的特别扭也理解不顺。特问老师两个问题:
afterConcurrentHandlingStarted这方法的使用场景
HandlerInterceptorAdapter相比于HandlerInterceptor的优势是不是就在于上述这个方法。