title: Spring dynamic object inject - @Autowired
body: <p>imagine one dao class and one service(BO) class . how to automatically inject FwObject  to setFwObject method , because FwObject having user related information and these piece of code should be written  inside framework </p>
  
  <pre><code>Class MyAction exdends ActionSupport{
       @Autowired
       private SomeService someService;
  
       @Autowired
       FwBean fwObj;  
  
       execute(){
            fwObj.set(fromRequest()); //build this object using request  and session values 
       }     
  }   
  
  
  @Service
  class PersonServiceImpl {        
      @Autowired
      private SomeDAO someDao;            
  }
  
  
  
  @Repository
      class PersonDAO {
        @Autowired
        public void setFwBean(FwBean obj) {
          System.out.println(obj);
          this.fwObject = obj  //object  will be come from MyAction
       }   
   }
  </code></pre>
  
  <p>
          </p>
  
  <pre><code>         &lt;filter&gt;
              &lt;filter-name&gt;struts2&lt;/filter-name&gt;
              &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/filter-class&gt;
          &lt;/filter&gt;
  
          &lt;filter-mapping&gt;
              &lt;filter-name&gt;struts2&lt;/filter-name&gt;
              &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
          &lt;/filter-mapping&gt;
  
          &lt;context-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
          &lt;/context-param&gt;
  
          &lt;listener&gt;
              &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
          &lt;/listener&gt;
          &lt;listener&gt;
              &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
          &lt;/listener&gt;
  
      &lt;/web-app&gt;
  
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:p="http://www.springframework.org/schema/p"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:jdbc="http://www.springframework.org/schema/jdbc"
         xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/aop
              http://www.springframework.org/schema/aop/spring-aop.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
              http://www.springframework.org/schema/jdbc
              http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;!-- scans the classpath for annotated components (including @Repostory 
      and @Service  that will be auto-registered as Spring beans  --&gt;      
       &lt;bean id="fwBean" class="com.sriseshaa.fw.helper.FwBean" scope="request"&gt;
  
            &lt;!-- this next element effects the proxying of the surrounding bean --&gt;
            &lt;aop:scoped-proxy/&gt;
      &lt;/bean&gt;
  
      &lt;context:component-scan base-package="com.sriseshaa" /&gt;
  
      &lt;!-- methods or classes needing to run in a complete transaction will
      be annotated with Transactional --&gt;
      &lt;tx:annotation-driven /&gt;
  
      &lt;!-- Creates a data source that can provide a connection to in-memory embedded database populated 
      with test data
      see: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/ch12s08.html   --&gt;
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"  &gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost/fwtest" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt;
  
  
      &lt;!-- This will ensure that hibernate or jpa exceptions are automatically translated into
           Spring's generic DataAccessException hierarchy for those classes annotated with Repository
           For example see PersonDaoJpa--&gt;
      &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
      &lt;!-- JPA Entity Manager Factory --&gt;
      &lt;bean id="entityManagerFactory" 
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
            p:dataSource-ref="dataSource"
            p:persistenceXmlLocation="META-INF/persistence.xml" 
            p:persistenceUnitName="springJpaPersistenceUnit" /&gt;          
  
  
      &lt;!-- bean post-processor for JPA annotations --&gt;
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  
      &lt;!-- Transaction Config --&gt;
      &lt;bean id="transactionManager"
            class="org.springframework.orm.jpa.JpaTransactionManager"
            p:entityManagerFactory-ref="entityManagerFactory"/&gt;
  
      &lt;!-- use declarative transaction management  --&gt;
      &lt;tx:annotation-driven  transaction-manager="transactionManager"/&gt;
  
  
  
  
   &lt;/beans&gt;
  </code></pre>
  
  <p>Exception </p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.fwBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
      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$1.getObject(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
      at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4765)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5260)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1525)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1515)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
      at java.util.concurrent.FutureTask.run(FutureTask.java:138)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:619)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.fwBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:339)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
      at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33)
      at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.getTarget(Cglib2AopProxy.java:653)
      at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:604)
      at com.sriseshaa.fw.helper.FwBean$$EnhancerByCGLIB$$79187402.toString(&lt;generated&gt;)
      at com.sriseshaa.fw.helper.dao.AbstractDAO.setFwObject(AbstractDAO.java:86)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:582)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
      ... 23 more
  Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
      at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
      at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:40)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:325)
      ... 36 more
  </code></pre>
  
  <p>how to set <strong>request level scope</strong> for FwBean, because carefully we have to create new FwBean instance for every request or <strong>every request having own FwBean instance</strong></p>