4 Ways to Get EJBContext in EJB 3
1. Use field injection in bean class. Those fields can have any access qualifiers (e.g., private, public, protected, package default).
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:
13 comments:
Thanks for the post I have been looking for the solution for my problem for about 2.5 hours and this post solved my problem.
Bence
Thanks a lot.
I was looking for the 4th point.
It is a standard way of obtaining the EJBContext (I found it in the EJB 3.0 specs too).
Thanks alot, very very helpfull
excellent demonstration. Got through idea from it.
Excellent !!!!!!
Very helpful post! Thanks a a lot
Thanks mate... just dropped by. Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.
very very useful! Thank you
Very interesting, even though I remain with original question unanswered: should it work in any of these approaches? If not, which could be the reason?
Regards,
Pierluigi
Thanks a lot for this post.
-- Sachin Patil
Great and Useful Article.
Online Java Course
Java Online Training
Java Course Online
J2EE training
online J2EE training
Best Recommended books for Spring framework
Java Interview Questions
Java Training Institutes in Chennai
Java Training in Chennai
J2EE Training in Chennai
java j2ee training institutes in chennai
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
http://trainingsinvelachery.in/sap-hr-training-in-velachery/
http://trainingsinvelachery.in/sap-mm-training-in-velachery/
http://trainingsinvelachery.in/sap-sd-training-in-velachery/
http://trainingsinvelachery.in/sap-fico-training-in-velachery/
http://trainingsinvelachery.in/sap-abap-training-in-velachery/
http://trainingsinvelachery.in/sap-hana-training-in-velachery/
Can we inject SessionContext anotated with Resource in a non ejb class. It does not give any error but returning anonymous for getCallerPrincipal alwayas.
Post a Comment