【问题标题】:Spring Boot autowire field in singleton service/controller by request scoped bean通过请求范围 bean 在单例服务/控制器中的 Spring Boot 自动装配字段
【发布时间】:2019-11-02 14:08:56
【问题描述】:

我有一个带有自动装配字段的单例服务类,例如:

@Service
public class MyService{

     @Autowired
     private List<POJO> listWithObjectsForRequest;

}

listWithObjectsForRequest 用于我的 Spring Boot 应用程序的多个服务和组件,并且需要进行大量计算才能创建此列表。它还取决于正在运行的当前请求。所以我想我可以写一个请求范围的bean,每次请求进入我的应用程序时,Spring都会注入它:

@Configuration
public class MyServiceConfiguration{

    @Bean
    @RequestScope
    public List<POJO> listWithObjectsForRequest(){
        return heavyCalculations() // signature: public List<POJO> heavyCalculations()...
    }

}

但我在应用程序启动时收到以下错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myService': Unsatisfied dependency expressed through field 'listWithObjectsForRequest'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.listWithObjectsForRequest': 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.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    ... 31 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.listWithObjectsForRequest': 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.
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:365)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:193)
    at com.sun.proxy.$Proxy84.equals(Unknown Source)
    at java.util.concurrent.ConcurrentHashMap.containsValue(ConcurrentHashMap.java:985)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.determineAutowireCandidate(DefaultListableBeanFactory.java:1501)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1222)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    ... 44 common frames omitted
Caused by: 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.
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
    at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:42)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:353)
    ... 53 common frames omitted

我以为我在这里找到了答案:Inject request scoped bean into another bean,但我的应用程序仍然失败并显示此错误消息。

我知道listWithObjectsForRequest只能在请求范围内计算,但是如何告诉spring MyService的字段应该只在请求中初始化(一次)?

【问题讨论】:

  • 其实MyService中的字段只会被初始化一次,因为它是一个单例。
  • 所以你是说在你的配置中用@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) 注释bean之后你得到同样的异常?

标签: java spring spring-boot spring-mvc


【解决方案1】:

正如在其他答案中已经提到的,您收到此错误是因为将请求范围的 bean 注入单例范围的 bean 也就是窄 bean DI 问题

你只需要使用

@Autowired // provider from javax.inject.Provider; private Provider<List<POJO>> listWithObjectsForRequest;

参考文献 When to use javax.inject.Provider in Spring?

【讨论】:

    【解决方案2】:

    您将收到此异常,因为您试图将请求范围注入单例。单例将被创建一次,依赖项将被注入一次。您可能正在使用一些依赖于请求的功能,并且在单例的 bean 初始化期间,spring 无法找到它。

    如果你想将请求范围 bean 注入到单例中,你可以这样做

    将 ApplicationContext 注入 MyService 就像 -

            @Autowired
            private ApplicationContext context;
    

    然后每次都从应用程序上下文中获取 bean 引用,而不是自动装配 listWithObjectsForRequest。虽然这种方法可以解决问题,但会将您的代码与 Spring 绑定。如果你对它没问题,你可以使用它。

    或者

    您可以使用这里提到的方法注入 - https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-method-injection

    【讨论】:

      【解决方案3】:

      在 POJO 类中 尝试 @Scope("原型") 在类声明之前

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-07
        • 1970-01-01
        • 2012-11-13
        • 2013-01-21
        • 2013-01-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多