小编典典

作用域“会话”对当前线程无效;IllegalStateException:找不到线程绑定的请求

spring

我有一个控制器,希望每个会话都唯一。根据spring文档,实现有两个细节:

1.初始Web配置

为了支持在请求,会话和全局会话级别(Web范围的Bean)的Bean范围界定,在定义Bean之前,需要一些较小的初始配置。

web.xml如文档所示,我已经添加了以下内容:

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

2.范围豆作为依赖项

如果要将(例如)HTTP请求范围的bean注入另一个bean,则必须注入AOP代理来代替范围的bean。

我通过@Scope提供proxyMode如下所示对bean进行了注释:

@Controller
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class ReportBuilder implements Serializable {
    ...
    ...
}

问题

尽管进行了上述配置,我还是收到以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.reportBuilder': 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.

更新1

以下是我的组件扫描。我在以下方面web.xml:

<context-param>
  <param-name>contextClass</param-name>
  <param-value>
    org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  </param-value>
</context-param>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>org.example.AppConfig</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

以及以下内容AppConfig.java:

@Configuration
@EnableAsync
@EnableCaching
@ComponentScan("org.example")
@ImportResource("classpath:applicationContext.xml")
public class AppConfig implements AsyncConfigurer {
  ...
  ...
}

更新2

我创建了一个可重现的测试用例。这是一个较小的项目,因此存在差异,但是会发生相同的错误。有相当多的文件,所以我上传它作为一个tar.gz以megafileupload。


阅读 1034

收藏
2020-04-11

共1个答案

小编典典

问题不在于你的Spring注释,而在于你的设计模式。你将不同的作用域和线程混合在一起:

  • singleton
  • session (or request)
  • thread pool of jobs

单身人士可以在任何地方使用,没关系。但是,会话/请求范围在附加到请求的线程之外不可用。

即使请求或会话不再存在,异步作业也可以运行,因此无法使用依赖于请求/会话的bean。同样也没有办法知道,如果你正在另一个线程中运行作业,那么哪个线程是发起者请求(这意味着aop:proxy在这种情况下没有帮助)。

我觉得你的代码看起来像要作合同 ReportController,报表制作,UselessTask和ReportPage之间。有没有办法只使用一个简单的类(POJO)从UselessTask中存储数据并在ReportController或ReportPage中读取它,而不再使用ReportBuilder?

2020-04-11