Autowiring request scoped beans into application scoped beans

Lydwin picture Lydwin · Nov 25, 2012 · Viewed 17k times · Source

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

I have a class RequestScopedBean:

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

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

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

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>

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

Answer

Biju Kunjummen picture Biju Kunjummen · Nov 25, 2012

You have to mark your requestScopedBean as a scoped proxy also, this way Spring will inject in a proxy for requestScopedBean and in the background manage the scope appropriately.

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

More here