gpt4 book ai didi

java - 应该实现和使用哪个HandlerInterceptorAdapter 方法来清除ThreadLocal?

转载 作者:行者123 更新时间:2023-12-04 04:00:04 26 4
gpt4 key购买 nike

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

最佳答案

使用afterCompletion。它将根据任何结果调用

Callback after completion of request processing, that is, after rendering the view. Will be called on any outcome of handler execution, thus allows for proper resource cleanup.

引用

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

注意

  • 如果您要从 preHandle 返回 false,您应该清理 pre handle 本身的本地线程,因为 false 意味着您有处理了响应,因此 postHanlde()afterCompletion 将不会被调用

关于java - 应该实现和使用哪个HandlerInterceptorAdapter 方法来清除ThreadLocal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63208163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com