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

后端 未结 2 1209
轮回少年
轮回少年 2021-01-15 21:47

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 cla

相关标签:
2条回答
  • 2021-01-15 22:34

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

    0 讨论(0)
  • 2021-01-15 22:43

    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");
    
    0 讨论(0)
提交回复
热议问题