2012-11-25 68 views
10

是否可以將請求作用域bean自動裝入到應用程序作用域bean中。即將請求作用域bean自動裝入應用程序作用域bean

我有一個類RequestScopedBean:

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

和一類的應用範圍的bean,其中請求範圍的bean的自動裝配。

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

和彈簧的配置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> 

,當我嘗試運行該應用程序的bean創建applicationScopedBean的失敗,出現以下錯誤:

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 

回答

8

的上面的異常表明你沒有正確配置Spring來提供請求作用域bean。

您需要按照文檔描述here這個添加到你的web.xml:

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

但是,還有更多你的問題不僅僅是配置。您正在嘗試將請求範圍的bean注入到一個單例範圍的bean中。 Spring解決了依賴關係,並在DI容器啓動時實例化單例。這意味着ApplicationScopedBean只會被創建一次(此時不會有任何請求發生,因此自動裝配很可能會失敗)。

如果您使用的是原型scoped bean而不是request scoped,那麼您不得不考慮一種在每次使用它的情況下使用新實例來提供singleton scoped bean的方法。這些方法在Spring文檔的Method Injection一章中有描述。

16

您必須將requestScopedBean標記爲作用域代理,這樣Spring纔會爲requestScopedBean注入代理,並在後臺適當地管理作用域。

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

更多here

相關問題