【问题标题】:Which HandlerInterceptorAdapter method should be implemented and used to clear ThreadLocal?应该实现并使用哪个 HandlerInterceptorAdapter 方法来清除 ThreadLocal?
【发布时间】:2020-08-01 17:32:19
【问题描述】:

我已经编写了 HandlerInterceptorAdapter 的实现。它将一些特定于请求的数据放在 ThreadLocal 中。因为线程被重新用于新请求,所以在处理请求时必须删除放在 ThreadLocal 中的数据。我应该实现哪个 HandlerInterceptorAdapter 方法来清除它?我看到有两个选项,postHandle 和 afterCompletion。我需要保证 ThreadLocal 中的数据被删除。

【问题讨论】:

    标签: java spring spring-boot thread-local


    【解决方案1】:

    使用afterCompletion任何结果

    都会调用它

    请求处理完成后的回调,即渲染视图后。将在处理程序执行的任何结果上调用,因此可以进行适当的资源清理

    参考

    https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html

    注意

    • 如果你打算从preHandle返回false,你应该在pre句柄本身清理线程,因为false意味着你已经处理了响应,结果postHanlde()afterCompletion将不叫

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 2012-07-27
      • 1970-01-01
      相关资源
      最近更新 更多