package com.foo.ejb;2. Use setter method injection in bean class. You can use either method or field injection for a particular field or property, but not both. Those methods can have any access qualifiers (e.g., private, public, protected, package default).
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
@Stateless
public class HelloBean implements HelloRemote {
@Resource
private SessionContext sctx;
public void hello() {
System.out.println("SessionContext from field injection: " + sctx);
}
@StatelessOr,
public class HelloBean implements com.foo.ejb.HelloRemote {
private SessionContext sctx;
@Resource
private void setSessionContext(SessionContext sctx) {
this.sctx = sctx;
}
}
@Stateless3. Look up the injected resource based on the name() (or default name, if not specified) field of @Resource
public class HelloBean implements com.foo.ejb.HelloRemote {
private SessionContext sctx;
@Resource
private void setSctx(SessionContext sctx) {
this.sctx = sctx;
}
}
@StatelessOr using default name if the name field of @Resource is not specified. Note that the default name of an injected resource is: fully-qalified-class-name/variable-name:
public class HelloBean implements com.foo.ejb.HelloRemote {
@Resource(name="sessionContext")
private SessionContext sctx;
public void hello() {
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext) ic.lookup("java:comp/env/sessionContext");
System.out.println("look up injected sctx: " + sctxLookup);
} catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
@Stateless4. Look up by the standard name java:comp/EJBContext (note that there is no /env)
public class HelloBean implements com.foo.ejb.HelloRemote {
@Resource
private SessionContext sctx;
public void hello() {
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext) ic.lookup("java:comp/env/com.foo.ejb.HelloBean/sctx");
System.out.println("look up injected sctx by default name: " + sctxLookup);
} catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
@StatelessThe above examples use Stateless Session beans, and they also work with Stateful Session beans, Singleton Session beans (introduced in EJB 3.1), and message driven beans. EJBContext are a special kind of resource so you don't need to configure them in ejb-jar.xml, or any vendor-specific configuration files (sun-ejb-jar.xml, jboss.xml, etc). You can choose to also declare references to EJBContext/SessionContext/MessageDrivenContext in descriptors, but you are more likely to get javax.naming.NameNotFoundException.
public class HelloBean implements com.foo.ejb.HelloRemote {
public void hello() {
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext) ic.lookup("java:comp/EJBContext");
System.out.println("look up EJBContext by standard name: " + sctxLookup);
} catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
These techniques are well defined in EJB 3 spec and should work in all JavaEE 5 compilant application servers. I tested above examples on JavaEE SDK 5/SJSAS 9/Glassfish.
Tags: