写入响应后如何清理线程局部变量?
我有一个使用 ThreadLocal 的 Grails 应用程序。
当前 ThreadLocal 填充和清除由过滤器处理(before:填充,after:清除)。
问题是过滤器的 after 调用在模板渲染之前运行,因此我需要的变量过早地全部为空。 是否有一些简单的方法可以在响应完全写入后触发 ThreadLocal 的清除?
一种方法是在填充 ThreadLocal 之前清除它,从而将所有工作留给过滤器之前-呼叫。但我不喜欢在变量不再用于任何用途后让 ThreadLocal“携带”变量,因此需要其他东西。
I have a Grails application that uses ThreadLocal.
Currently ThreadLocal populating and clearing is handled by a filter (before: populates, after: clears).
The problem is that the filter's after-call is run before template is rendered so my needful variables are all nulls prematurely. Is there some simple way to trigger the clearing of ThreadLocals after the response is fully written?
One approach would be to clear the ThreadLocal before populating it, thus leaving all the work for filter's before-call. But I don't like the idea of leaving the ThreadLocal "carrying" the vars after they're no longer used for anything so something else is needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
after
在控制器操作完成后调用 - 您希望清理工作在afterView
中进行,它在视图渲染后调用。after
is called after the controller action finishes - you want the cleanup to be inafterView
which is called after the view is rendered.