Kotlin协程与Spring请求范围内的bean

时间:2019-04-07 08:08:06

标签: spring kotlin

我有一些代码在协程的函数调用中利用了请求范围的bean。看来spring不知道我仍然在同一个请求中。

即使调用来自异步Kotlin协程,是否也可以使spring理解我仍然希望请求范围内的bean?

代码运行时出现以下错误消息:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'scopedTarget.wsContext': 
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: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

1 个答案:

答案 0 :(得分:1)

协程独立于主线程,因此您需要在协程范围内手动提供所有数据