java - Spring框架请求范围和单例 Autowiring

标签 java spring spring-mvc

我正在将 OncePerRequestFilter 用于“请求”范围内的服务 bean,该服务 bean 自动连接到 Controller 中。但我仍然收到此错误。

Error creating bean with name 'fileProcessingService': 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?

请求范围内的 bean 可以用单例 bean 注入(inject)吗?或者问题出在其他地方?

最佳答案

如果您需要在 DispatcherServlet 之外使用请求和 session 范围的 Bean,请将以下内容添加到 web.xml 中:

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

关于java - Spring框架请求范围和单例 Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5508651/

相关文章:

java - 通过反射初始化字符串常量

java - Spring 致动器 '/auditevents'端点返回404

spring-boot - 如何应对 Spring Boot 2.2.0 中的 x-forwarded-headers? (反向代理背后的Spring Web MVC)

linux - Java 剪贴板不工作

Java - 普通正负数的正则表达式

java - 如何在Android Fragment中设置自动完成文本

java - 无法在数据库中使用 BCryptPassword 保存加密密码

java - 如何设置属性排序,当我使用Jpa时Example.of

java - 为什么即使 web.xml 存在于 WEB-INF 下,我也会收到错误 "web.xml is missing and <failOnMissingWebXml> is set to true"?

Spring boot - Application.properties 中的用户定义变量