我的下一个流程将在我的Spring MVC Web应用程序中实现:
我是如何做到的:
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
return super.getSession();
}
@Autowired
SomeDBRepo repo;
@Async
doSomeAsync() {
repo.findAll();
}
我收到此错误:
Error creating bean with name 'scopedTarget.getSession': Scope 'session' 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.
我已经阅读了一些关于这个问题的文章,但没有任何帮助。这个问题怎么解决?
答案 0 :(得分:0)
Spring中的会话和请求范围是线程绑定的。
当您使用@Async
注释方法时,执行将在另一个线程中进行,并且无法访问调用方的范围(以及会话)。
尝试在您的异步bean中注入SessionFactory
以获取会话。