简体   繁体   English

Spring Boot如何在Async方法中简化Scope Request Bean?

[英]Spring Boot How can i easy Scope Request bean in Async method?

Spring boot How can i easy use bean with Request Scope in async method. Spring Boot如何在异步方法中轻松地将bean与Request Scope一起使用。

When i try execute got exception: 当我尝试执行时出现异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean ! org.springframework.beans.factory.BeanCreationException:创建名称为'bean的bean时出错。 class': Scope 'request' is not active for the current thread; class':作用域'request'对于当前线程无效; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; 如果您打算从单例中引用它,请考虑为此bean定义作用域代理。 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? 嵌套异常为java.lang.IllegalStateException:未找到线程绑定的请求:您是在实际Web请求之外引用请求属性,还是在原始接收线程之外处理请求? 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. 如果您实际上是在Web请求中操作并且仍然收到此消息,则您的代码可能在DispatcherServlet / DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter公开当前请求。

I found a simple solution to this error. 我找到了解决此错误的简单方法。

I've initialized the requestScope bean. 我已经初始化了requestScope bean。 which contains @Async method. 其中包含@Async方法。

Now every request(rest) call with controller receives a new instance of bean. 现在,每个使用控制器的请求(休息)调用都会收到一个新的bean实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM