簡體   English   中英

如何在EJB3中注入Spring bean?

[英]How to inject a Spring bean in an EJB3?

我正在嘗試訪問EJB3中的Spring bean,但似乎沒有注入,因為我收到了NullPointerException。

我認為我不了解beanRefContext.xml的作用以及如何很好地使用它。

以下EJB和XML位於WAR的WEB-INF / lib中的服務JAR中。 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>

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>

編輯

我閱讀了“將Spring bean注入EJB3中”一文 ,現在我向web.xml添加了一個context-param,但是問題仍然存在。

web.xml

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

我顯然需要更多幫助和解釋。

您可能已經注意到,但是如果萬一您錯過了, beanRefContext.xml需要將beanRefContext.xml作為參數傳遞給您的services-context.xml ,但是在您的代碼中缺少它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM