为什么我的 @EJB 没有被注入到我的 Web 服务中?
我在 IBM RAD(又名 Eclipse 3.4)和 WebSphere 7.0 中有一个基本的 EJB 3.0(Java EE 5)项目,
我创建了一个 EAR 项目、一个 EJB 项目(用于 EJB)、一个 JPA 项目(用于域对象)和一个动态 Web 项目(用于我的 JAX-RS Web 服务)。
EJB 项目包含一个如下所示的 EJB:
@Stateless
public TestBean implements TestBeanLocal {
/* ... */
@Local
public TestBeanLocal {
/* ... */
Web 项目包含一个如下所示的 JAX-RS Web 服务:
@Path(value="/mywebservice")
public MyWebService implements Application {
@EJB
private TestBeanLocal myBean;
@GET
@Produces({"text/plain"})
public String getList() {
return myBean.getList();
/* ... */
我的问题是 MyWebService 中的“myBean”属性从未被注入。它始终为空。
我错过了什么想法吗?
谢谢, 抢
I have a basic EJB 3.0 (Java EE 5) project in IBM RAD (aka Eclipse 3.4) and WebSphere 7.0
I created an EAR project, an EJB project (for the EJBs), a JPA project (for the domain objects), and a Dynamic Web project (for my JAX-RS web service).
The EJB project contains an EJB like so:
@Stateless
public TestBean implements TestBeanLocal {
/* ... */
@Local
public TestBeanLocal {
/* ... */
The Web project contains a JAX-RS web service like so:
@Path(value="/mywebservice")
public MyWebService implements Application {
@EJB
private TestBeanLocal myBean;
@GET
@Produces({"text/plain"})
public String getList() {
return myBean.getList();
/* ... */
My problem is the "myBean" attribute in MyWebService is never injected. It's always null.
Any ideas what I missed?
Thanks,
Rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java EE 容器只会对其管理的组件执行 @EJB 注入。对于 Java EE 5,这将包括 servlet、Web 服务和 EJB。在您的情况下,该组件由 Wink 管理,因此问题是 Wink 是否应该解释@EJB(我猜不是)。 Java EE 6 注入可以工作,因为 JAX-RS 是该 Java EE 版本的一部分,并且该组件由容器管理,而不是由第三方框架管理。
The Java EE container will only perform @EJB injection on components it manages. For Java EE 5, this would include servlets, Web services and EJBs. In your case the component is managed by Wink, so the question is whether Wink is expected to interpret @EJB (I guess not). With Java EE 6 injection works because JAX-RS is part of that Java EE version and the component is managed by the container, not by a 3rd party framework.
经过一番折腾后,我创建了一个干净的新项目(下面的类名略有不同)...我注意到在WebSphere的启动日志中EJB具有JNDI名称,所以我尝试通过构造函数中的JNDI来查找EJB网络服务的......
并且有效。
我现在的问题是,我真的必须这样做才能看到我的豆子吗?或者我错过了什么?在另一个使用 Java EE 6 版本的 GlassFish 的项目中,我所要做的就是放入 @EJB 中,这很好。这在 Java EE 5 / WebSphere 7 中不起作用吗?或者这可能是 IBM RAD 7(又名 Eclipse 3.4)的限制?
谢谢,
抢
After much hair-pulling, I created a clean new project (the following has slightly different classnames) ... I noticed in WebSphere's start up log the EJB had the JNDI name, so I tried looking up the EJB by it's JNDI in the constructor of the web service ...
... and that worked.
My question now is, do I really have to do this to see my beans? Or is there something I missed? On another project using a Java EE 6 version of GlassFish all I had to do was put in @EJB and it was fine. Does that simply not work in Java EE 5 / WebSphere 7? Or maybe it's a limitation of IBM RAD 7 (aka Eclipse 3.4)?
Thanks,
Rob