2012-11-25 3 views
10

요청 범위 Bean을 응용 프로그램 | 위 Bean으로 autowire 할 수 있습니까?요청 범위가있는 bean을 응용 프로그램 범위 Bean으로 자동 채우기

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

및 클래스 응용 프로그램 요청 autowire가되는 콩을 범위있는 콩을 범위 :

즉 나는 클래스 RequestScopedBean 있습니다.

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> 

나는 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을 올바르게 구성하지 않았다고 제안합니다.

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

그러나, 단지 구성보다 더 귀하의 질문에 더있다 :

당신은 워드 프로세서 here에 설명 된대로 web.xml에이를 추가해야합니다. 요청 범위 콩을 싱글 톤 범위 bean에 주입하려고 시도하고 있습니다. Spring은 DI 컨테이너가 시작될 때 의존성을 해결하고 싱글 톤을 인스턴스화합니다. 이는 ApplicationScopedBean이 한 번만 생성된다는 것을 의미합니다 (이 시점에서 요청이 없으므로 자동 와이어 링이 실패 할 가능성이 큽니다).

요청 범위 대신 프로토 타입 범위 Bean을 사용하는 경우, 사용될 때마다 새로운 인스턴스로 단일 범위 bean을 제공하는 방법을 고려해야합니다. 이에 대한 접근법은 Spring 문서의 Method Injection 장에 설명되어 있습니다.

16

또한 requestScopedBean을 범위 프록시로 표시해야합니다. 그러면 Spring은 requestScopedBean의 프록시에 삽입하고 백그라운드에서 적절하게 범위를 관리합니다.

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

here

관련 문제