我有一个Web应用程序,该应用程序具有在单独的线程中运行的Spring Integration逻辑。问题是,在某些时候,我的Spring Integration逻辑尝试使用请求范围的Bean,然后出现以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.tenantContext': 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.
Caused by: 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.
我设置了ContextLoaderListener:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
我的作用域Bean就是这样注释的(因为我听说对我的bean进行编程会有所帮助):
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TenantContext implements Serializable {
我正在做什么?
如果是,我在这里想念什么?
如果没有,关于如何实现该目标的其他建议?
最佳答案
您只能在运行请求的Web容器线程上使用请求(和 session )作用域的bean。
我认为线程正在等待来自您的SI流的异步回复?
如果是这样,您可以将请求范围的Bean绑定(bind)到消息,可能在 header 中,或者在有效负载中的某处。