如何在EJB3中注入Spring bean?

时间:2013-06-25 14:20:34

标签: java spring ejb-3.0

我正在尝试访问EJB3中的Spring bean,但它似乎没有被注入,因为我收到了NullPointerException。

我想我不理解beanRefContext.xml的作用以及如何使用它。

以下EJB和XML位于服务JAR中,驻留在WAR的WEB-INF / lib中。 Spring bean(DAO)也位于WEB-INF / lib中的单独JAR中。

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>

服务-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>

修改

我阅读了帖子"Inject Spring beans into EJB3",现在我向web.xml添加了一个上下文参数,但问题仍然存在。

的web.xml

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

我显然需要更多的帮助和解释。

1 个答案:

答案 0 :(得分:0)

您可能已经注意到了,但如果您错过了, beanRefContext.xml 需要通过 services-context.xml 作为 参数 ,但 代码

中缺少它
相关问题