Field-Based Injection
Field-Based Injection
The type of resource is determined by one of the following:
The type of the field the @Resource annotation is decorating for field-based injection
The type of the JavaBeans property the @Resource annotation is decorating for
method-based injection
The type element of @Resource
For class-based injection, the type element is required.
The authenticationType element is used only for connection factory resources, and can be set
to one of the javax.annotation.Resource.AuthenticationType enumerated type values:
CONTAINER
, the default, and APPLICATION.
The shareable element is used only for ORB instance resources or connection factory resource.
It indicates whether the resource can be shared between this component and other components,
and may be set to true, the default, or false.
The mappedName element is a non-portable, implementation-specific name that the resource
should be mapped to. Because the name element, when specified or defaulted, is local only to the
application, many Java EE servers provide a way of referring to resources across the application
server. This is done by setting the mappedName element. Use of the mappedName element is
non-portable across Java EE server implementations.
The description element is the description of the resource, typically in the default language of
the system on which the application is deployed. It is used to help identify resources, and to help
application developers choose the correct resource.
Field-Based Injection
To use field-based resource injection, declare a field and decorate it with the @Resource
annotation. The container will infer the name and type of the resource if the name and type
elements are not specified. If you do specify the type element, it must match the field's type
declaration.
package com.example;
public class SomeClass {
@Resource
private javax.sql.DataSource myDB;
...
}
In the code above, the container infers the name of the resource based on the class name and the
field name: com.example.SomeClass/myDB. The inferred type is
javax.sql.DataSource.class
.
Resource Injection
The Java EE 5 Tutorial · September 2007
1012