@javax.annotation.Resource: Difference between revisions
(→lookup) |
|||
Line 23: | Line 23: | ||
The JNDI name of the resource. | The JNDI name of the resource. | ||
If the annotation is applied to a field, the default value of the "name" element is the field name qualified by the class name. | If the annotation is applied to a field, the default value of the "name" element is the field name qualified by the class name. For example, in the following case: | ||
<pre> | |||
package com.example; | |||
public class MyClass { | |||
@Resource | |||
private DataSource ds; | |||
} | |||
</pre> | |||
the default name is "com.example.MyClass/ds" | |||
==lookup== | ==lookup== | ||
The JNDI name of a resource that the resource being defined will be bound to. | The JNDI name of a resource that the resource being defined will be bound to. |
Revision as of 17:03, 4 April 2017
Internal
Overview
The @Resource annotation marks a resource that is needed by the application. This annotation may be applied to a component class, or to fields or methods of the component class.
When the annotation is applied to a field or method, the container will inject an instance of the requested resource into the application component when the component is initialized. If the annotation is applied to the component class, the annotation declares a resource that the application will look up at runtime.
@Resource(lookup = "java:global/remote-hornetq") private InitialContext externalContext;
Defined by JSR 250 "Common Annotations for the Java Platform".
Elements
name
The JNDI name of the resource.
If the annotation is applied to a field, the default value of the "name" element is the field name qualified by the class name. For example, in the following case:
package com.example; public class MyClass { @Resource private DataSource ds; }
the default name is "com.example.MyClass/ds"
lookup
The JNDI name of a resource that the resource being defined will be bound to.