使用 InitialContext.lookup() 方法找不到我的 bean [英] cannot find my bean using the InitialContext.lookup() method

查看:40
本文介绍了使用 InitialContext.lookup() 方法找不到我的 bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 struts 1.3 API 来制作带有 EJB 3.0 的小型应用程序.不幸的是,我无法使用 @EJB 批注从我的操作类内部调用我的 bean 对象.我已经使用不同的解决方法解决了这个问题(第一个是使用我的 bean 的全局 jndi 名称,另一个是首先调用另一个类并使用来自该类的 @EJB 注释).这两种解决方法仍然有明显的缺点.我想直接从我的操作类调用我的 EJB.我已经阅读了大量使用java:comp/env/beanName"JNDI 名称的示例,但仍然没有弄清楚如何做到这一点并获取名称未找到 axception.让本地 EJB 类的全名是 com.ejb.myEjbPackage.MyEJBLocal,我如何使用上下文查找来调用它?(我可以不修改任何 web.xml 和 sun-web.xml 描述符吗?)我正在使用 glassfish 服务器和 Netbeans IDE.

I have tried to use struts 1.3 API to make a small application with EJB 3.0. Unfortunatelly i cannot use the @EJB annotation to call my bean object from inside my action class. I have solved this problem using different workarounds ( the first one is to use my global jndi name of my bean and the other is to call another class first and use the @EJB annotation from that class). Still these two workarounds have significant disadvantages. I would like to call my EJB directly from my action class. I have read plenty examples using the "java:comp/env/beanName" JNDI name but still haven't figure out how to do it and get name not found axception. Let the full name of the local EJB class be the com.ejb.myEjbPackage.MyEJBLocal, how can i call it using the context lookup? (can i do it without modifying any of the web.xml and sun-web.xml descriptors?) I am using glassfish server and Netbeans IDE.

提前致谢

推荐答案

我找到了答案:如果您不能在要调用 bean 的类中使用 EJB 注释,则:如果您不想使用 XML 描述符来定义您的 bean,那么您必须在 bean 类本身中进行.因此我在 GameBean 类中使用了以下注释

I found the answer : If you cannot use the EJB annotation in the class you want to call the bean then : If you don't want to mess with XML descriptors to define your bean , you have to do it in the bean class itself. Hence i used the following annotation in the GameBean class

     @Stateless
     @EJB(name="ejb/GameBean",beanInterface=GameBeanLocal.class,beanName="GameBean")
     public class GameBean implements GameBeanLocal {.....

beanName 是可选的.注释必须在类声明上方的行中声明.然后,为了从其他类调用bean,你可以这样做

The beanName is optional. The annotation must be declared in the line ABOVE the declaration of the class. Then, in order to call the bean from the other class you can do

     InitialContext ic = new InitialContext();
     ic.lookup("java:comp/env/ejb/GameBean");

这篇关于使用 InitialContext.lookup() 方法找不到我的 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆