2012-11-25 9 views
10

リクエストスコープのBeanをアプリケーションスコープのBeanに自動配線することは可能ですか?リクエストスコープのBeanをアプリケーションスコープのBeanに自動配線する

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

とクラスのアプリケーションが要求をautowiredされるBeanをスコープするBeanをスコープ:

すなわち、私はクラスRequestScopedBeanを持っています。

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

と、以下のように、ばねの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の豆の作成が次のエラーで失敗し、このアプリケーションを実行しようとすると:

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

上記の例外は、要求スコープBeanの提供のためにSpringを正しく構成していないことを示しています。あなたはドキュメントhereで説明したように、あなたのweb.xmlにこれを追加する必要が

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

しかし、ただの設定よりもあなたの質問に多くがあります。要求スコープBeanをシングルトンスコープBeanに挿入しようとしています。 Springは依存関係を解決し、DIコンテナが起動するとシングルトンをインスタンス化します。これは、ApplicationScopedBeanが1回だけ作成されることを意味します(この時点では、飛行中にリクエストがないため、オートワイヤリングが失敗する可能性が高い)。

リクエストスコープではなくプロトタイプスコープBeanを使用していた場合は、使用するたびに新しいインスタンスでシングルトンスコープBeanを提供する方法を検討する必要があります。これに関するアプローチは、SpringドキュメントのMethod Injectionの章に説明されています。

16

また、requestScopedBeanをスコープ付きプロキシとしてマークする必要があります。これにより、SpringはrequestScopedBeanのプロキシに挿入され、バックグラウンドでスコープを適切に管理します。

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

もっとhere

関連する問題