title: How to inject a Spring bean in an EJB3?
body: <p>I'm trying to access a Spring bean in an EJB3 but it doesn't seem to get injected because I'm getting a NullPointerException.</p>
  
  <p>I think I'm not understanding the role of beanRefContext.xml and how it's used very well.</p>
  
  <p>The following EJB and XMLs are in a services JAR that resides in the WEB-INF/lib of a WAR. The Spring bean (the DAO) is in a separate JAR also in WEB-INF/lib.</p>
  
  <p><strong>EJB</strong>:</p>
  
  <pre><code>@Stateless
  @Interceptors(SpringBeanAutowiringInterceptor.class)
  public class TimetrackingServiceBean implements TimetrackingService {
  
      @Autowired
      private UserDao userDao;
  
      @Override
      public List&lt;User&gt; getAllUsers() {
          return this.userDao.findAll(); // &lt;-- NPE
      }
  }
  </code></pre>
  
  <p><strong>beanRefContext.xml</strong>:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"&gt;
  
          &lt;bean name="serviceContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"&gt;&lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>services-context.xml</strong>:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I read the post <a href="https://stackoverflow.com/questions/5251385/inject-spring-beans-into-ejb3">"Inject Spring beans into EJB3"</a> and now I added a context-param to web.xml but the problem remains.</p>
  
  <p><strong>web.xml</strong>:</p>
  
  <pre><code>...
  &lt;context-param&gt;
    &lt;param-name&gt;parentContextKey&lt;/param-name&gt;
    &lt;param-value&gt;serviceContext&lt;/param-value&gt;
   &lt;/context-param&gt;
  ...
  </code></pre>
  
  <p>I apparently need more help and explanation. </p>