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 = FooRemote.class)
public class MyEjbRemoteRefBean implements MyBeanInterface {
@EJB
private BarRemote 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();
FooRemote fooRemote = (FooRemote) context.lookup("java:comp/env/myFooEjb");
BarRemote barRemote = (BarRemote) context.lookup("java:comp/env/org.superbiz.refs.MyEjbRemoteRefBean/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.FooRemote
org.superbiz.refs.MyEjbRemoteRefBean/myBarEjb
org.superbiz.refs.BarRemote
org.superbiz.refs.MyEjbRemoteRefBean
myBarEjb
{code}