将请求范围的bean自动装配到应用程序范围的bean中

时间:2012-11-25 22:05:10

标签: spring scopes

是否可以将请求范围的bean自动装配到应用程序范围的bean中。即

我有一个RequestScopedBean类:

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

以及一个应用程序作用域bean,其中请求范围bean是自动装配的。

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

和spring-config xml如下:

<?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创建失败,并出现以下错误:

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

3 个答案:

答案 0 :(得分:15)

您还必须将requestScopedBean标记为作用域代理,这样Spring将在requestScopedBean的代理中注入,并在后台适当地管理范围。

<bean id="requestScopedBean" class="RequestScopedBean" scope="request">  
    <aop:scoped-proxy/>
</bean>

更多here

答案 1 :(得分:8)

上面的例外表明您没有正确配置Spring以提供请求范围的bean。

您需要按照文档here

中的说明将其添加到您的web.xml
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

但是,除了配置之外,您的问题还有很多。您正在尝试将请求范围的bean注入到单例范围的bean中。当DI容器启动时,Spring解析依赖关系并实例化单例。这意味着ApplicationScopedBean只会被创建一次(此时将不会有飞行中的请求,因此自动装配很可能会失败)。

如果您使用的是原型范围的bean而不是请求范围,那么每次使用它时都必须考虑使用新实例来提供单例范围的bean。有关此方法的方法在Spring文档的Method Injection章节中进行了描述。

答案 2 :(得分:0)

@Airwavezx的等效注解如下:

@Scope( value = SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS )