0

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.

I think I'm not understanding the role of beanRefContext.xml and how it's used very well.

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.

EJB:

@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class TimetrackingServiceBean implements TimetrackingService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> getAllUsers() {
        return this.userDao.findAll(); // <-- NPE
    }
}

beanRefContext.xml:

<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">

        <bean name="serviceContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"></bean>

</beans>

services-context.xml:

<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">

    <context:annotation-config />

</beans>

EDIT

I read the post "Inject Spring beans into EJB3" and now I added a context-param to web.xml but the problem remains.

web.xml:

...
<context-param>
  <param-name>parentContextKey</param-name>
  <param-value>serviceContext</param-value>
 </context-param>
...

I apparently need more help and explanation.

Community
  • 1
  • 1
Koohoolinn
  • 1,427
  • 6
  • 20
  • 29

1 Answers1

0

You may have already noticed but if in case you missed, beanRefContext.xml need to be passed your services-context.xml as argument, however it is missing in your code

Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71
earthling
  • 69
  • 3