Java Reference
In-Depth Information
@EJB
ShoppingCart cart1;
@EJB
ShoppingCart cart2;
. . .
cart1.equals(cart2); // this is required to be false
5.2.6. Resource injection using @Resource
The @Resource annotation is by far the most versatile mechanism for resource injection
in EJB 3. In most cases the annotation is used to inject JDBC data sources, JMS resources,
and EJB contexts. But the annotation can also be used for anything in the JNDI registry.
The following listing shows the @Resource annotation.
Listing 5.3. javax.annotation.Resource annotation
@Target({TYPE, FIELD, METHOD}) @Retention(RUNTIME)
public @interface Resource {
String name() default "";
String lookup() default "";
Class type() default java.lang.Object.class;
AuthenticationType authenticationType()
default AuthenticationType.CONTAINER;
boolean shareable() default true;
}
All of the @Resource elements are optional. Just as in the case of EJBs, the EE server
can usually figure out what resource to inject by the resource type. But this isn't always
the case, so the elements are there to help the container figure it out. For example, you
may want to inject a DataSource , but if your EE server is configured with multiple data
sources, you'll need to use the name or lookup elements to narrow down which one to
inject. Table 5.7 describes the elements.
Table 5.7. @Resource annotation elements
Element
Description
Besides performing an injection, the @Resource annotation implicitly creates a binding
referring to the injected resource in the java:comp namespace. This is primarily done for
backward compatibility. This attribute allows you to specify the name that's used for the
implicit binding. This is equivalent to the <res-ref-name> element in deployment
descriptors used extensively in EJB 2.
name
 
Search WWH ::




Custom Search