h1. Via annotation
{code:title=Usable by EJB, Interceptor, Servlet, Filter, or Listener}
package org.superbiz.refs;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
@Stateless
@EJB(name = "myFooEjb", beanInterface = FooLocal.class)
public class MyEjbLocalRefBean implements MyBeanInterface {
@EJB
private BarLocal myBarEjb;
public void someBusinessMethod() throws Exception {
if (myBarEjb == null) throw new NullPointerException("myBarEjb not injected");
// Both can be looked up from JNDI as well
InitialContext context = new InitialContext();
FooLocal fooLocal = (FooLocal) context.lookup("java:comp/env/myFooEjb");
BarLocal barLocal = (BarLocal) context.lookup("java:comp/env/org.superbiz.refs.MyEjbLocalRefBean/myBarEjb");
}
}
{code}
h1. Via xml
The above @EJB annotation usage is 100% equivalent to the following xml.
{code:xml|title=ejb-jar.xml or web.xml}
myFooEjb
org.superbiz.refs.FooLocal
org.superbiz.refs.MyEjbLocalRefBean/myBarEjb
org.superbiz.refs.BarLocal
org.superbiz.refs.MyEjbLocalRefBean
myBarEjb
{code}