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

Java 请求范围bean的单例服务/控制器中的Spring Boot autowire字段,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我有一个单例服务类,其中有一个自动连接字段,如: @服务 公共类MyService{ @自动连线 带有请求对象的私有列表; } 列表WithObjectsForRequest用于我的Spring Boot应用程序的多个服务和组件,创建此列表需要进行大量计算。它还取决于当前正在运行的请求。所以我想我可以编写一个请求范围的bean,每次请求进入我的应用程序时,Spring都会注入该bean: @配置 公共类MyServiceConfiguration{ @豆子 @请求范围 带有ObjectsFor

我有一个单例服务类,其中有一个自动连接字段,如:

@服务
公共类MyService{
@自动连线
带有请求对象的私有列表;
}
列表WithObjectsForRequest
用于我的Spring Boot应用程序的多个服务和组件,创建此列表需要进行大量计算。它还取决于当前正在运行的请求。所以我想我可以编写一个请求范围的bean,每次请求进入我的应用程序时,Spring都会注入该bean:

@配置
公共类MyServiceConfiguration{
@豆子
@请求范围
带有ObjectsForRequest()的公共列表{
return heavyCalculations()//签名:public List 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
我想我在这里找到了答案:但是我的应用程序仍然失败,并显示了这个错误消息

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

课堂上的POJO 尝试 @范围(“原型”)
在类声明之前

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

如果您想将请求范围bean注入到singleton中,您可以通过

将ApplicationContext注入MyService,就像-

        @Autowired
        private ApplicationContext context;
然后,每次都从应用程序上下文获取bean引用,而不是使用ObjectsForRequest自动连接listWithObjectsForRequest。虽然这种方法可以解决这个问题,但会将您的代码与Spring绑定在一起。如果你可以使用它,你可以使用它


您可以使用这里提到的方法注入-

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

你只需要使用

@Autowired
//来自javax.inject.provider的提供者;
私有提供者列表,包含请求的对象;

参考资料

实际上,
MyService
中的字段将只初始化一次,因为它是一个单例。所以您的意思是,在配置中使用
@Scope(value=“request”,proxyMode=ScopedProxyMode.TARGET\u CLASS)注释bean之后,会得到相同的异常情况。