Java Reference
In-Depth Information
Element
Description
This is the actual JNDI lookup name of the resource to be injected. This is likely the at-
tribute you'll use the most.
lookup
The type of the resource. If the @Resource annotation is on a field, the default is the type
of the field. If the annotation is on a setter method, the default is the type of the method's
element.
type
This can have the value AuthenticationType.CONTAINER or Authentica-
tionType.APPLICATION. This is only used for connection factory-type resources like
data sources.
authenticationType
This indicates if this resource can be shared between other components. This is only for
connection factory type resources like data sources.
shareable
mappedName
A vendor-specific name for the bean.
description
A description of the EJB.
The @Resource annotation attributes are used in the same fashion as in the @EJB annota-
tion.
5.2.7. When to use resource injection
The @Resource annotation is used to inject container-managed resources into code that
the container also manages. This annotation works only inside an EJB, an MDB, 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). Resources range from a simple in-
teger value to a complex DataSource , but as long as the resource is container-managed,
the @Resource annotation can be used to inject it into your code.
5.2.8. @Resource annotation in action
Let's take a brief look at injecting DataSource , JMS, EJBContext , email, timer, and
environment entry resources. We'll start with a familiar JDBC DataSource example to
explain the basic features of the @Resource annotation before moving on to the more in-
volved cases.
Injecting DataSource
The following code injects a DataSource into the DefaultBidService bean:
Search WWH ::




Custom Search