Reputation: 385
i have an issue of saving a reference to an EJB as a member from a non EJB class (which is used as static member in an EJB)
say i have
@EJB(...)
@Stateless
public Class SessionBean implements MySession{
protected static MyHelper helper = new MyHelper();
}
public Class MyHelper{
protected AnotherSessionBean ejb = lookup("jndi");
public void doSomething(){
ejb.foo();
}
}
since the helper class is not an EJB then i have a method for lookup called int the member instantiating. with this code i got in runtime an exception java.lang.NoClassDefFoundError: Could not initialize class on the SessionBean class.
when i changed MyHelper to this it worked :
public Class MyHelper{
protected AnotherSessionBean getEjb(){
return (AnotherSessionBean)lookup("jndi");
}
public void doSomething(){
getEjb().foo();
}
}
wondering why first way didn't work...
Upvotes: 0
Views: 243
Reputation: 68
This may be possible because AnotherSessionBean might not have been initialized when a JNDI look happened in the first code snippet. That means SessionBean was getting initialized first. While initializing SessionBean, MyHelper's constructor called. This is in turn called jndi for AnotherSessionBean which is not yet loaded.
This worked in second code snippet because by the time getEjb() was called, all the EJBs are already initialized. So JNDI could find AnotherSessionBean.
Upvotes: 1