Java Reference
In-Depth Information
Listing 5.2. javax.ejb.EJB annotation
@Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME)
public @interface EJB {
String name() default "";
Class beanInterface() default Object.class;
String beanName() default "";
String lookup() default "";
All three of the @EJB elements are optional, and for the most part, the EE server will be
able to figure what bean to inject without any trouble simply by type. The elements are
there to handle edge cases that aren't encountered very often. Table 5.6 describes the ele-
Table 5.6. @EJB annotation elements
Besides performing injection, the @EJB annotation implicitly creates a binding referring to the in-
jected EJB in the java:comp namespace. This is primarily done for backward compatibility. This at-
tribute allows you to specify the name that's used for the implicit binding. This is equivalent to the
<ejb-ref-name> element in deployment descriptors used extensively in EJB 2.
This helps narrow the type of EJB references when needed, which can be a local business interface,
a remote business interface, or the bean class if it implements no interfaces.
This refers to either the name element of the @Stateless or @Stateful annotations or the <ejb-
name> tag of the deployment descriptor for the bean. Like beanInterface, this helps to narrow down
the specific bean to inject in cases where multiple beans implement the same interface.
This is the actual JNDI lookup name of the EJB to be injected. This is likely the attribute you'll use
the most.
A vendor-specific name for the bean.
A description of the EJB.
5.2.4. When to use EJB injection
It's a good idea to always use EJB injection when you can. But the @EJB annotation is spe-
cifically intended for injecting session beans into client code and injection is only possible
within managed environments. This annotation only works inside another EJB, in code
running inside an application-client container (ACC), or in components registered with the
web container (such as a Servlet or JSF-backing bean). If you need an EJB in any other
code, you'll need to use JNDI directly and perform a lookup.
Search WWH ::

Custom Search