2.2.3 @Resource

Organization of this subsection
(1) Description
(2) Element

(1) Description

Declare the resource reference. You can specify in a class, method, and field. When specified in a method or field, the annotation becomes a target for Dependency Injection. However, the method must be the set method.

(2) Element

The following table lists the elements of @Resource:

Element nameFunction
nameSpecify the name of resource reference. The specified name is used as a JNDI name. You can omit the element description if the annotation is specified in a method or field.
typeSpecify the Java type of a resource. You can omit the element description if the annotation is specified in a method or field.
authenticationTypeSpecify the authentication type used in the resource.
shareableSpecify whether the resource is to be shared.
mappedNameSpecify the resource display name and queue name for specifying the referenced resource.
lookupSpecify the Portable Global JNDI name of any other resource that you reference or a resource alias.
descriptionSpecify the resource description.

The details of each element are as follows:

(a) name element
Type
String
Description
Specify the name of resource reference. The specified name is used as a JNDI name. You can omit the element description if the annotation is specified in a method or field.
You can also specify a resource alias. For details on specifying a J2EE resource alias, see 2.6.6 Setting the optional names for the J2EE resources in the uCosminexus Application Server Common Container Functionality Guide.
Default value
  • When set in a method
    Property of the class name or set method in which the annotation is specified
  • When set in a field
    Class name or field name in which the annotation is specified
(b) type element
Type
Class
Description
Specify the Java type of a resource. You can omit the element description if the annotation is specified in a method or field.
Default value
  • When specified in a method
    Argument type of the method
  • When set in a field
    Field type
type element and the corresponding DD
As the type element differs from J2EE specifications, the corresponding DD changes depending on the set value (Java Type). The following table describes the corresponding DD that changes depending on the Java Type:

Table 2-28 Table for the corresponding DD depending on the type element

Type elementDD tag corresponding to J2EE specificationsDD tag supported with Cosminexus Application Server specifications#1
java.lang.String#2Env-entryenv-entry
java.lang.Character#2env-entryenv-entry
java.lang.Integer#2env-entryenv-entry
java.lang.Boolean#2env-entryenv-entry
java.lang.Double#2env-entryenv-entry
java.lang.Byte#2env-entryenv-entry
java.lang.Short#2env-entryenv-entry
java.lang.Long#2env-entryenv-entry
java.lang.Float#2env-entryenv-entry
javax.xml.rpc.Serviceservice-refException #3
javax.xml.ws.Serviceservice-refException #3
javax.jws.WebServiceservice-refException #3
javax.sql.DataSourceresource-refresource-ref
javax.jms.ConnectionFactoryresource-refresource-ref
javax.jms.QueueConnectionFactoryresource-refresource-ref
javax.jms.TopicConnectionFactoryresource-refresource-ref
javax.mail.Sessionresource-refresource-ref
java.net.URLresource-refException #3
javax.resource.cci.ConnectionFactoryresource-refresource-ref
org.omg.CORBA_2_3.ORBresource-refresource-ref
Other connection factories defined by resource adapterresource-refresource-env-ref
javax.jms.Queuemessage-destination-refresource-env-ref
javax.jms.Topicmessage-destination-refresource-env-ref
javax.resource.cci.InteractionSpecresource-env-refException #3
javax.transaction.UserTransactionresource-env-refresource-env-ref
javax.xml.ws.WebServiceContextUndefinedresource-env-ref#4
All types other than those mentioned above#5resource-env-refresource-env-ref
#1
If !# is included in the mappedName element, correspond to <resource-env-ref>, irrespective of the Java Type.
#2
You cannot acquire a value from a standard DD, as a result, the value is displayed in the element file but DI is not performed.
#3
Exception in the case of import.
#4
With Application Server version 08-70 or earlier versions, this element is handled same as all types other than those mentioned above.
#5
With Application Server version 09-00, subclasses of the java.lang.Class and java.lang.Enum are not handled in the <env-entry> tag.

(c) authenticationType element
Type
AuthenticationType
Description
Specify the authentication type used in the resource.
Default value
CONTAINER
(d) shareable element
Type
boolean
Description
Specify whether the resource is to be shared.
Default value
true
(e) mappedName element
Type
String
Description
Specify the resource display name and queue name for specifying the referenced resource.
When characters other than single--byte alphanumeric characters and underscores (_) are to be included in the resource display name, replace them with underscores (_).
Default value
""
Setting conditions of the mappedName element
The setting conditions of the mappedName element change based on the type element. The following table describes the setting conditions of the mappedName element in @Resource:

Table 2-29 Setting conditions of mappedName() in @Resource

Setting condition (Java Type, resource)Availability#1
java.lang.StringN
java.lang.CharacterN
java.lang.IntegerN
java.lang.BooleanN
java.lang.DoubleN
java.lang.ByteN
java.lang.ShortN
java.lang.LongN
java.lang.FloatN
javax.xml.rpc.ServiceN
javax.sql.DataSourceY
javax.jms.ConnectionFactoryY
javax.jms.QueueConnectionFactoryY
javax.jms.TopicConnectionFactoryY
javax.mail.SessionY
java.net.URLN
javax.resource.cci.ConnectionFactoryY
org.omg.CORBA_2_3.ORBN
javax.jms.Queue#2Y
javax.jms.TopicY
javax.resource.cci.InteractionSpecN
javax.transaction.UserTransactionN
javax.ejb.EjbContextN
javax.ejb.SessionContextN
javax.ejb.TimerServiceN
JavaBeans resourceY
Legend:
Y: Can be used.
N: Cannot be used.
#1
Mapping to the object to be managed is established with the mappedName element, irrespective of the Java Type. Use !# to demarcate the display name of the resource adapter and the name of the object to be managed.
#2
If you use javax.jms.Queue when using TP1/Message Queue - Access or Cosminexus Reliable Messaging, use # as the delimiter of the resource adapter display name and queue display name.

(f) lookup attribute
Type
String
Description
Specify the Portable Global JNDI name of any other resource or a resource alias that you want to reference.
Default value
""
(g) description element
Type
String
Description
Specify the resource description.
Default value
""