请求处理后 spring 启动应用程序的清理方法

Clean up method for spring boot application after request process

如何在 spring 引导应用程序中实现请求完成过程后的清理方法以清除线程本地数据等数据

我尝试 ServletRequestListener.requestDestroyed api,但在请求完成后它没有被命中

一个可能的答案是您忽略了注册侦听器。 Servlet javadocs 说:

In order to receive these notification events, the implementation class must be either declared in the deployment descriptor of the web application, annotated with WebListener, or registered via one of the addListener methods defined on ServletContext.

可能还有其他方法可以使用 Spring;例如使用处理程序拦截器;见 Remove ThreadLocal object within a Spring MVC website?

如果您使用普通 servlet(没有 Spring MVC 基础结构),另一种方法是在您的 servlet 的 service 方法或 doXxx 方法中进行清理。或者在servlet前面的一个Filter.