Getting Stuff from the Container
Generally speaking the only way to get a container controlled resource is via dependency injection or lookup from within a container managed component. Dependency injection can be configure equally via xml or annotations
InitialContext Lookups
- Server side beans should access other beans on the server via the default no args constructor, in order to propagate the security . 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
- Declared via annotations
- Clients is a good reference for more complex lookups, including authentication.
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