如何在异步任务执行器中启用请求范围
如何在异步任务执行器中启用请求范围
在我的应用程序中,我有一些异步的网络服务。服务器接受请求,返回OK响应,并使用AsyncTaskExecutor开始处理请求。我的问题是如何在这里启用请求范围,因为在这个处理过程中,我需要获取一个被注释为:
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
现在我得到了异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.requestContextImpl': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
因为它在SimpleAsyncTaskExecutor
中运行,而不是在DispatcherServlet
中。
我的异步请求处理如下:
taskExecutor.execute(new Runnable() { @Override public void run() { asyncRequest(request); } });
其中taskExecutor为: