HandlerInterceptorAdapter中afterConcurrentHandlingStarted方法的疑惑

来源:6-7 Http请求前后监听工具-HttpInterceptor开发

Echo鑫

2017-12-10

一般我看设置拦截器,都是继承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方法”就总感觉自己翻译的特别扭也理解不顺。特问老师两个问题:

  1.  afterConcurrentHandlingStarted这方法的使用场景

  2. HandlerInterceptorAdapter相比于HandlerInterceptor的优势是不是就在于上述这个方法。

写回答

1回答

Jimin

2017-12-10

你好,如果实现HandlerInterceptor接口的话,三个方法必须实现,不管你需不需要,而HandlerInterceptorAdapter允许我们只实现需要的回调方法

当Controller中有异步请求方法的时候会触发该方法, 可以认为, afterConcurrentHandlingStarted是返回异步结果时调用(异步结果里不需要有数据)

0
1
Echo鑫
理解了,谢谢老师!!!!
2017-12-10
共1条回复

Java开发企业级权限管理系统

源于企业真实Java项目,涉及大量高级技巧,覆盖权限管理开发技术

2227 学习 · 1334 问题

查看课程