Calling an enterprise bean is the process of getting a reference to the enterprise bean so that you can call its methods. To call an enterprise bean's methods, you generally need to create an instance of the bean's local or remote interface, then call methods of the interface. See Calling an Enterprise Bean for more information.
The process of calling an enterprise bean differs for Java EE 5/Java EE 6 applications and J2EE 1.4 applications.
@EJB private MyEJBInterface newMyEJB;
If you are calling an enterprise bean in a different project, you also have to add the EJB project to the classpath of the project that is calling the enterprise bean.
In Java EE 6 applications, local bean interfaces are optional when calling the bean because local clients can access an EJB via a no-interface view.
You use a lookup method similar to the following to locate the enterprise bean:
private MySessionRemote lookupMySessionBean() { try { Context c = new InitialContext(); Object remote = c.lookup("java:comp/env/ejb/MySessionBean"); MySessionRemoteHome rv = (MySessionRemoteHome) PortableRemoteObject.narrow(remote, MySessionRemoteHome.class); return rv.create(); } catch exceptions
You then have to get an instance of the interface using code similar to the following:
private MySessionRemote newMySessionRemote = lookupMySessionBean();
Finally, you register the EJB reference in the deployment descriptor of the module from which you are calling the enterprise bean. If and where the reference is added depends on which of the following applies: