@javax.annotation.Resource: Difference between revisions
(→lookup) |
|||
(11 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
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. | 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. | 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. The annotation is defined by [[JEE#jsr_250|JSR 250 "Common Annotations for the Java Platform"]]. | ||
=Example= | |||
<syntaxhighlight lang='java'> | |||
@Resource(name="java:/JmsXA") | |||
private ConnectionFactory connectionFactory; | |||
</syntaxhighlight> | |||
=Elements= | =Elements= | ||
Line 23: | Line 22: | ||
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" | |||
===name and external contexts=== | |||
"name" works with external JNDI contexts. For example, if "java:global/remote-hornetq" is the name of an [[WildFly_JNDI_Concepts#External_JNDI_Context|external JNDI context]] imported locally, and "/jms/queue/playground" is the JNDI name of a remote destination bound in "java:jboss/exported" JNDI context by the remote server, then the queue can be injected locally as follows: | |||
<pre> | |||
@Remote(name="java:global/remote-hornetq/jms/queue/remote-inbound") | |||
private Queue remoteQueue; | |||
</pre> | |||
@Resource and external JNDI context example: {{External|https://github.com/NovaOrdis/playground/blob/master/jboss/messaging/sending-and-receiving-from-injected-remote-destinations/src/main/java/io/novaordis/playground/jboss/messaging/WrapperServlet.java}} | |||
==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''. | ||
==mappedName== | |||
A product-specific name that the resource should map to. | |||
The mapped name could be of any form. Application servers are not required to support any particular form or type of mapped name, nor the ability to use mapped names. The mapped name is product dependent and often installation dependent. No use of mapped name is portable. |
Latest revision as of 14:50, 25 September 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. The annotation is defined by JSR 250 "Common Annotations for the Java Platform".
Example
@Resource(name="java:/JmsXA")
private ConnectionFactory connectionFactory;
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"
name and external contexts
"name" works with external JNDI contexts. For example, if "java:global/remote-hornetq" is the name of an external JNDI context imported locally, and "/jms/queue/playground" is the JNDI name of a remote destination bound in "java:jboss/exported" JNDI context by the remote server, then the queue can be injected locally as follows:
@Remote(name="java:global/remote-hornetq/jms/queue/remote-inbound") private Queue remoteQueue;
@Resource and external JNDI context example:
lookup
The JNDI name of a resource that the resource being defined will be bound to.
mappedName
A product-specific name that the resource should map to.
The mapped name could be of any form. Application servers are not required to support any particular form or type of mapped name, nor the ability to use mapped names. The mapped name is product dependent and often installation dependent. No use of mapped name is portable.