You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 15 Next »

Getting Stuff from the Container

Generally speaking the only way to get a Container-Managed Resource is via dependency injection or lookup from within a Container-Managed Component.

The unbreakable rules. Read these over and over again when things don't work.

  1. java:comp/env is the spec defined namespace for lookup of any Container-Managed Resource
  2. java:comp/env is empty by default
  3. java:comp/env is read-only at runtime
  4. java:comp/env is populated by Declaring References to Container-Managed Resource via xml or annotation
  5. only Container-Managed Components, not their libraries, can Declare References via xml or annotation
  6. only Container-Managed Components, not their libraries, can get dependency injection of Container-Managed Resources
  7. only Container-Managed Components, and their libraries, may lookup from java:comp/env
  8. you must use the no-arg 'new InitialContext()' constructor to lookup something from java:comp/env
  9. the annotations and xml for Declaring References are identical in functionality, both always configure lookup with optional dependency injection

Common mistakes/misunderstandings/myths

  • "I tried it via annotation and it didn't work, so I used xml and then it did work"

See rule 9. If one form worked and the other didn't, it means you simply made a mistake in using one versus the other. Use what works for you, but understand both annotations or xml will work for either lookup or injection if used correctly.

  • "I need to use lookups, so I can't use the annotation"

See rule 9. Annotations are not just for injection, that is just how they are typically used. Know that when you use an annotation for injection, it will always create an entry in java:comp/env. As well you can use the annotation at the class level and it will cause no dependency injection and only the entry creation in java:comp/env.

  • "I don't want injection, so I can't use the annotation"

See rule 9 and the above. You can use the annotation at the class level and it will cause no dependency injection and only the entry creation in java:comp/env.

InitialContext Lookups

  • Server side beans should access other beans on the server via the default no args constructor (EJB 3 spec 15.3.1 Bean Provider's Responsibilities).
  • Container side lookup names must be preceded with java:comp/env, as in java:comp/env/myBean. e.g.
    initialContext.lookup("java:comp/env/beanName")
  • note that java:comp/env/ is empty by default, and you need to declare your references, whether via xml or annotations
    • Declared via annotations
      @EJB(name="myBean", beanInterface = IMyBean.class)
      public class DependentBean ....
    • Declared via xml INCOMPLETE
  • Clients is a good reference for more complex lookups, including authentication (only for non-standard clients not running in an EJB container).

Injection

Some important things to realize about dependency injections.

  • @EJB IMyBean myBean is syntactic sugar for lookups (as follows), and is overridden by xml
    initialContext = new InitialContext();
    initialContext.lookup("java:comp/env/beanName")
  • EJB Refs is a good reference for injection via xml or annotations
  • No labels