将请求范围的Bean自动装配到应用程序范围的Bean中 [英] Autowiring request scoped beans into application scoped beans

查看:86
本文介绍了将请求范围的Bean自动装配到应用程序范围的Bean中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能将请求范围的Bean自动连接到应用程序范围的Bean.即

Is it possible to autowire a request scoped bean into an application scoped bean. i.e

我有一个RequestScopedBean类:

I have a class RequestScopedBean:

class RequestScopedBean {
   ....
   ....
   ....
}

和一个类Application范围的Bean,在其中自动绑定了请求范围的Bean.

and a class Application scoped bean in which the request scoped bean is autowired.

class ApplicationScopedBean {
   @Autowire
   private RequestScopedBean requestScopedBean;
   ....
   ....
   ....
}

和spring-config xml如下:

and the spring-config xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
              http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<bean id="applicationScopedBeans" class="ApplicationScopedBean" />
<bean id="requestScopedBean" class="RequestScopedBean" scope="request">  
</bean>
</beans>

当我尝试运行此应用程序时,applicationScopedBean的bean创建失败,并出现以下错误:

when I try to run this application the bean creation of applicationScopedBean fails with the following error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ApplicationScopedBean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not       autowire field: private RequestScopedBean requestScopedBean; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'request'
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
    at com.amazon.coral.reflect.instantiate.SpringInstantiatorFactory$1.newInstance(SpringInstantiatorFactory.java:168)
    ... 19 more

推荐答案

上面的异常表明您没有正确配置Spring以提供请求范围的Bean.

The exception above suggests that you have not correctly configured Spring for the provision of request scoped beans.

您需要按照docs

You need to add this to your web.xml as described in the docs here:

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

但是,您的问题不仅仅是配置.您试图将请求范围的Bean注入到单例范围的Bean中.当DI容器启动时,Spring解析依赖关系并实例化单例.这意味着ApplicationScopedBean仅创建一次(此时将没有任何请求在进行中,因此自动装配很可能会失败).

However, there is more to your question than just configuration. You are attempting to inject a request scoped bean into a singleton scoped bean. Spring resolves dependencies and instantiates singletons when the DI container starts. This means that ApplicationScopedBean will only be created once (at this point there will be no request in flight and so the autowiring will most likely fail).

如果您使用原型范围的Bean而不是请求范围的Bean,则必须考虑一种在每次使用时为单例范围的bean提供一个新实例的方法. 一章.

If you were using a prototype scoped bean instead of request scoped you'd have to consider a way of suppling the singleton scoped bean with a fresh instance everytime it was used. The approaches for this are described in the Method Injection chapter of the Spring docs.

这篇关于将请求范围的Bean自动装配到应用程序范围的Bean中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆