在实际的Web请求之外使用请求范围的Bean [英] Using a request scoped bean outside of an actual web request

查看:178
本文介绍了在实际的Web请求之外使用请求范围的Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,该应用程序具有在单独的线程中运行的Spring Integration逻辑.问题是,在某些时候,我的Spring Integration逻辑尝试使用请求范围的Bean,然后出现以下错误:

I have a web application that has a Spring Integration logic running with it in a separated thread. The problem is that at some point my Spring Integration logic tries to use a request scoped bean and then i get the following errors:

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:

I have the ContextLoaderListener set:

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

我的作用域Bean就是这样注释的(因为我听说对我的bean进行编程会有所帮助):

My Scoped Bean is annotated like that(since I heard that proxing my bean would help):

@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)  
public class TenantContext  implements Serializable {

我正在做什么? 如果是,我在这里想念什么? 如果没有,关于如何实现该目标的其他建议?

Is what I'm doing possible? If yes, what am I missing here? If no, any other suggestions on how I can achieve that?

推荐答案

您只能在运行请求的Web容器线程上使用请求(和会话)作用域的bean.

You can only use request (and session) -scoped beans on the web container thread on which the request is running.

我认为线程正在等待您的SI流中的异步回复?

I presume that thread is waiting for an async reply from your SI flow?

如果是这样,则可以将请求范围的Bean绑定到消息,可能在标头中,或者在有效负载中的某处.

If so, you can bind the request-scoped bean to the message, perhaps in a header, or somewhere in the payload.

这篇关于在实际的Web请求之外使用请求范围的Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆