Java Reference
In-Depth Information
Isn't that great? The @EJB annotation transparently “injects” the HelloUserBean
EJB into the annotated variable. The @EJB annotation reads the type and name of the
EJB and looks it up from JNDI under the hood. All EJB components are automatically re-
gistered with JNDI while being deployed. Note that you can still use JNDI lookups where
they're unavoidable. For example, to dynamically look up your bean, you could use code
like this:
Context context = new InitialContext();
HelloUserBean helloUser = (HelloUserBean)
context.lookup("java:module/HelloUserBean");
helloUser.sayHello("Curious George");
We'll talk in detail about EJB injection and lookup in chapter 5 .
1.5.5. CDI versus EJB injection
EJB-style injection predates CDI. Naturally, this means that CDI injection adds a number
of improvements over EJB injection. Most importantly, CDI can be used to inject almost
anything. EJB injection, on the other hand, can only be used with objects stored in JNDI,
such as EJB, as well as some container-managed objects like the EJB context. CDI is far
more type-safe than EJB. Generally speaking, CDI is a superset of EJB injection. For ex-
ample, you can use CDI to inject the EJB as follows:
Search WWH ::




Custom Search