单例服务/控制器中的Spring Boot autowire字段由请求范围的bean

时间:2019-06-19 19:09:39

标签: java spring spring-boot spring-mvc

我有一个具有自动连接字段的单例服务类:

@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的字段只能在请求中初始化(一次)?

3 个答案:

答案 0 :(得分:1)

您将收到此异常,因为您试图将请求范围注入到单例中。单例将创建一次,依赖项将注入一次。您可能正在使用某些取决于请求的功能,并且在单例的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

答案 1 :(得分: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 :(得分:0)

在POJO类中 尝试 @Scope(“ prototype”) 在上课之前