title: Spring bean is not injected into JSF managed bean (which is an abstract superclass)
body: <p>I have an abstract super class bean called FooBean and all of my other beans extend this class. I have wired up my dao in applicationContext files and I want to inject it into this superclass bean so that every time I am inside of a bean, I have access to the dao. The problem is, I get a NullPointerException whenever I try to access the dao in a subclass. The following files show only the relevant config details for spring setup:</p>
  
  <p><strong>web.xml:</strong></p>
  
  <pre><code>&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;context-param&gt;
     &lt;description&gt;Spring configuration files location.&lt;/description&gt;
     &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
     &lt;param-value&gt;/WEB-INF/config/applicationContext-*.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  </code></pre>
  
  <p><strong>faces-config.xml:</strong></p>
  
  <pre><code>&lt;application&gt;
     &lt;variable-resolver&gt;org.springframework.web.jsf.DelegatingVariableResolver&lt;/variable-resolver&gt;
     &lt;el-resolver&gt;org.springframework.web.jsf.el.SpringBeanFacesELResolver&lt;/el-resolver&gt;
  ...
  &lt;/application&gt;
  </code></pre>
  
  <p><strong>FooBean.java:</strong></p>
  
  <pre><code>public abstract class FooBean implements Serializable {
     protected transient FooService dao;
  
     public void setFooService(FooService dao){
        this.dao = dao;
     }
  }
  </code></pre>
  
  <p><strong>applicationContext-service.xml:</strong></p>
  
  <pre><code>&lt;bean id="serviceTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"&gt;
          &lt;property name="transactionManager" ref="transactionManager"/&gt;
          &lt;property name="transactionAttributes"&gt;
              &lt;props&gt;
                  &lt;prop key="*"/&gt;
                  &lt;!--
                  &lt;prop key="save*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                  &lt;prop key="delete*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                  &lt;prop key="remove*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                  &lt;prop key="find*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;
                  &lt;prop key="get*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;
                  --&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Definition of service targets --&gt;
      &lt;bean id="fooServiceTarget" class="com.foo.service.FooService"&gt;
          &lt;property name="fooDAO" ref="fooDAO"/&gt;
          &lt;property name="adminDAO" ref="adminDAO"/&gt;
          &lt;property name="channelsDAO" ref="channelsDAO"/&gt;
      &lt;/bean&gt;
      &lt;bean id="fooService" parent="serviceTemplate"&gt;
          &lt;property name="target" ref="fooServiceTarget"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p><strong>applicationContext-hibernate.xml:</strong></p>
  
  <pre><code>&lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
          &lt;property name="jndiName" value="jdbc/FooDataSource"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Hibernate SessionFactory --&gt;
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
          &lt;property name="mappingResources"&gt;
              &lt;list&gt;
                  &lt;!--value&gt;com/Foo/model/General.hbm.xml&lt;/value--&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.bytecode.use_reflection_optimizer"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.search.default.directory_provider"&gt;filesystem&lt;/prop&gt;
                  &lt;prop key="hibernate.search.default.indexBase"&gt;/usr/lucene/indexes&lt;/prop&gt;
                  &lt;!--prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.SwarmCacheProvider&lt;/prop--&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Spring Data Access Exception Translator Defintion --&gt;
      &lt;bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator"/&gt;
  
      &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
          &lt;property name="jdbcExceptionTranslator" ref="jdbcExceptionTranslator"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Add DAO's here and create separate application context for service --&gt;
      &lt;bean id="FooDAO" class="com.foo.service.dao.impl.FooDAOHibernate"&gt;
          &lt;property name="hibernateTemplate" ref="hibernateTemplate"/&gt;
      &lt;/bean&gt;
      &lt;bean id="adminDAO" class="com.foo.service.dao.impl.AdminDAOHibernate"&gt;
          &lt;property name="hibernateTemplate" ref="hibernateTemplate"/&gt;
      &lt;/bean&gt;
      &lt;bean id="channelsDAO" class="com.foo.service.dao.impl.ChannelsDAOHibernate"&gt;
          &lt;property name="hibernateTemplate" ref="hibernateTemplate"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p><strong>Things I already tried with no success:</strong></p>
  
  <ul>
  <li>Tried using @ManagedProperty(value="#{FooService}") on the FooBean class field.</li>
  <li><p>Tried defining a bean in faces-config.xml to inject the spring bean:</p>
  
  <p>
          com.foo.beans.FooBean
          none
          
              fooService
              #{fooService}
          
  </p></li>
  <li><p>Defined the subclass bean that had the null pointer as a spring bean and injecting the property via the spring beans xml file.</p></li>
  </ul>
  
  <p>Ironically, I am able to place the following code in the constructor of the FooBean abstract class and the dao is injected (but I don't want to use this code, I want spring to inject it):</p>
  
  <pre><code>WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getRequest().getServletContext());
          dao = (FooService)ctx.getBean("fooService");
  </code></pre>