java - Spring Boot 通过请求作用域 bean 在单例服务/ Controller 中 Autowiring 字段

标签 java spring spring-boot spring-mvc

我有一个带有 Autowiring 字段的单例服务类,例如:

@Service
public class MyService{

     @Autowired
     private List<POJO> listWithObjectsForRequest;

}

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

@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只能在request范围内计算,但是如何告诉spring MyService这个字段应该只在请求中初始化(一次)?

最佳答案

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

如果你想将请求作用域 bean 注入(inject)到单例中,你可以通过以下任一方法来实现

将 ApplicationContext 注入(inject) MyService,例如 -

        @Autowired
        private ApplicationContext context;

然后,每次都从应用程序上下文中获取 bean 引用,而不是 Autowiring listWithObjectsForRequest。虽然这种方法可以解决问题,但是会将您的代码与 Spring 绑定(bind)。如果您同意,则可以使用它。

或者

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

关于java - Spring Boot 通过请求作用域 bean 在单例服务/ Controller 中 Autowiring 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56674378/

相关文章:

java - 如何填写Mapper?

java - Spring Profile 包含 yaml 文件的问题

java - 如何导入第三方库

java - Spring mvc maven - 添加库

java - Spring 请求范围的 bean - 所有字段为空/空

java - 如何解决 com.fasterxml.jackson.databind.exc.MismatchedInputException?

spring - @autowired 与新 key ?

java - Thymeleaf StringTemplateResolver 所有变量为 null

java - 当我创建事件时,实现 ApplicationListener 接口(interface)的类不会启动

gradle - 来自 gradle 的 org.gradle.api.internal.LocationAwareException