cannot find my bean using the InitialContext.lookup() method

拟墨画扇 提交于 2019-12-01 12:14:02
Preston

@EJB won't work in a standard pojo it can only be done in a managed object (i.e. another session bean)

So...

Here's your bean

@Stateless(mappedName="beanName")
public class beanName implements beanNameRemote {

Here's your lookup

Context context = new InitialContext();  //default lookup pulls from jndi properties file
context.lookup("beanName");

You can do some further reading on the mappedName to see if you want to use it or not.

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 {.....

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!