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;

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);
}
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).
@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
private SessionContext sctx;

@Resource
private void setSessionContext(SessionContext sctx) {
this.sctx = sctx;
}
}
Or,
@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
private SessionContext sctx;

@Resource
private void setSctx(SessionContext sctx) {
this.sctx = sctx;
}
}
3. Look up the injected resource based on the name() (or default name, if not specified) field of @Resource
@Stateless
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);
}
}
Or 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:
@Stateless
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);
}
}
4. Look up by the standard name java:comp/EJBContext (note that there is no /env)
@Stateless
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);
}
}
The 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.

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.

8

View comments

When writing javadocs, IntelliJ automatically adds a closing tag for html elements. For instance, after typing <lt>, it automaticaly adds </lt>, or after typing <p>, it adds </p>. It can be annoying since simple html elements like those used in javadocs don't really need ending tags.
To disable javadoc automatic closing tags in IntelliJ, simply go to IntelliJ Preferences -> Editor -> Smart Keys, then on the right panel, uncheck Automatically insert closing tag.

Intellij 14 screenshot:


 Intellij 15 screenshot:

A related note, JDK 8 has tightened javadoc syntax check, and as a result self-closing elements like <p/>, or <br/> are deemed invalid and will cause failures. See JDK-8020619. However, this checking can be disabled by passing nonstandard option -Xdoclint:none to javadoc tool. For official javadoc guidelines, see How to Write Doc Comments for the Javadoc Tool .
0

Add a comment

Labels
Archive
Popular Posts
Popular Posts
  • Two JVM options are often used to tune JVM heap size: -Xmx for maximum heap size, and -Xms for initial heap size. Here are some common mi...
  • Simple enum . The ; after the last element is optional, when this is the end of enum definition. public enum Color { WHITE, BLACK, RED, ...
  • How to set project classpath in Eclipse and NetBeans are similar: just right-click the project name, choose Properties to bring up the Prope...
  • Let's say I need to spawn multiple threads to do the work, and continue to the next step only after all of them complete. I will need t...
  • This is a sample web.xml based on Servlet 2.5 (part of Java EE 5) that declares common elements. All top-level elements are optional, and c...
  • The default string value for java enum is its face value, or the element name. However, you can customize the string value by overriding toS...
  • Prior to JDK 6, we can check if a string is empty in 2 ways: if(s != null && s.length() == 0) if(("").equals(s)) Checking ...
  • When writing javadocs, IntelliJ automatically adds a closing tag for html elements. For instance, after typing <lt>, it automaticaly a...
  • StringBuilder was introduced in JDK 1.5. What's the difference between StringBuilder and StringBuffer? According to javadoc , StringBu...
  • With array, we can easily declare and initialize it at the same time: String[] favorites = new String[] {"EJB", "JPA", ...
Loading