Describe the purposes and uses of annotations and deployment descriptors, including how the two mechanisms interact, how overriding is handled, and how these mechanisms function at the class, method, and field levels.

One of the key enabling technologies introduced by J2SE 5.0 is the program annotation facility defined by JSR-175. This facility allows developers to annotate program elements in Java programming language source files to control the behavior and deployment of an application.

Metadata annotations are a key element in the simplification of the development of EJB 3.0 applications.

Annotations available for:

Annotations: Pros

Annotations: Cons

Best Uses for Annotations


Best Uses for Deployment Descriptors

NOTE. Although it is not anticipated as a typical use case, it is possible for the application developer to combine the use of metadata annotations and deployment descriptors in the design of an application. When such a combination is used, the rules for the use of deployment descriptors as an overriding mechanism apply.

Guidelines for .xml Overriding

Don't Forget Spec-Defined Defaults

Default values can be easier than annotations and .xml

Component Dependency Annotations

Annotation example:

@Resource(name="Foo") private DataSource ds;

Deployment Descriptor example:


Dependency Injection

Injected Field/Method Access Modifiers

Spec allows public, package, protected, private

Which should you use?

Exception: Overriding of environment dependencies within a class hierarchy

Which Is Best: Field, Method, or Class-level?

  1. Field-level: Easiest

    • e.g., @EJB Converter converter

    • Takes fewest characters to declare

    • Supports injection

  2. Method-level

    • Useful for logic tied to a specific dependency injection

    • But Field-level + @PostConstruct would work too

  3. Class-level

    • Useful for dependency declaration WITHOUT injection

    • Declare environment dependency for use by non container-managed classes

    Class-Level Dependency Example:

    @EJB(name="ejb/bar", beanInterface=Bar.class)
    public class FooBean implements Foo { 

    Bar bar = (Bar) context.lookup("java:comp/env/ejb/bar");

    Another Class-Level Dependency Example:

    Scenario: Stateful Session Bean creation

    • EJB 3.0 SFSBs are created as a side-effect of injection/lookup

    • Common need : many instances of same SFSB

    • Using Field-based dependency + injection:

      @EJB Cart cart1;
      @EJB Cart cart2;
      @EJB Cart cart3;

      is too static.

      Alternative: Class-level dependency + lookup

      @EJB(name="ejb/Cart", beanInterface=Cart.class)
      public class CartClient {
      	Cart[] carts = new Cart[numCarts];
      	for(int i = 0; i < carts.length; i++) {
      		carts[i] = (Cart)

Concurrency and Injection

Injection does not solve concurrency issues

If an object obtained through lookup() is non-sharable, it's non-sharable when injected

Be careful with Servlet instance injection:

public class MyServlet ... {
	private @EJB StatelessEJB stateless; // OK
	private @EJB StatefulEJB stateful;   // dangerous!

Most common issues: Stateful Session Beans, PersistenceContexts

Recommended alternative: lookup() and store in HttpSession:

@PersistenceContext(name="pc", type=EntityManager.class)

public class MyServlet ... {
	EntityManager em = ctx.lookup("java:comp/env/pc");
	httpSession.setAttribute("entityManager", em);

Performance and Injection

Use of injection is unlikely to cause performance issues

Injection is essentially a ctx.lookup() + one reflective operation

Injection occurs at instance creation-time

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine