There are two methods of resolving the resource references by using the @Resource annotation:
If you do not want to specify attributes in the @Resource annotation, the links must be resolved by using cosminexus.xml or property files other than cosminexus.xml. The following sections describe each of the methods.
Specify the optional name of the resource in the name attribute of the @Resource annotation.
The resources that can be specified in the name attribute of the @Resource annotation are as follows:
The examples of coding are as follows. In this example, the optional name jdbc/ds is set in the name attribute of the @Resource annotation.
package sample; |
If the optional name of the resource adapter is specified in the name attribute of the @Resource annotation, the reference resolution is implemented automatically. Therefore, the property file need not be edited. If the property file is obtained, the coding is as follows:
<resource-ref> |
Specify the resource display name and queue display name in the mappedName attribute of the @Resource annotation.
The resource display name coded in the mappedName attribute of the @Resource annotation is used for reference resolution.
The examples of coding are as follows. In this example, the resource display name DB_Connector_for_Oracle is set in the mappedName attribute of the @Resource annotation.
package sample; |
In this example, the name attribute of @Resource is omitted; therefore, the default value class-name/ field-name is used. The default value in this case is sample.MySessionBean/customerDB. If the property file is obtained, the coding is as follows:
<resource-ref> |
If the attributes of the @Resource annotation are not specified, the references must be resolved by using cosminexus.xml and the property files other than cosminexus.xml. Specify the resource display name in <linked-to> of cosminexus.xml or property files other than cosminexus.xml. If the resource at the reference destination is a queue, code the resource adapter display name and queue display name in <linked-queue>. If the resource at the reference destination is an Administered object, code the resource adapter display name and the Administered object name in <linked-adminobject>.
The examples of coding are as follows. In this example, the name attribute of the @Resource annotation is omitted; therefore, the default value class name/ field name is used as the optional name. The default value in this case is sample.MySessionBean/customerDB.
package sample; |
To resolve the references with cosminexus.xml, specify the resource display name in <link-to>, <linked-queue>, or <linked-adminobject> and then import the application. To resolve the references with the property files other than cosminexus.xml, import the application and then obtain the property file (such as HITACHI Application Integrated Property File). In <linked-to><linked-queue> or <linked-adminobject> in the obtained property file, code the resource display name that forms the reference destination.
When the property file is obtained after specifying the attributes, the coding is as follows:
<resource-ref> |
When you use the @Resource annotation to resolve the resource references and if multiple methods are specified at the same time, the settings are enabled in the following priority order:
The following table describes the optional name settings enabled when a different optional name is set concurrently in the J2EE resources.
Table 12-7 Reference destination settings enabled when multiple methods are set concurrently
Presence of name attribute settings #1 | Presence of mappedName attribute settings | Presence of settings such as <linked-to> in the property file #2 | Enabled optional name settings #3 |
---|---|---|---|
Yes | Yes | Yes | Settings such as <linked-to> in the property file #2 |
No | mappedName attribute settings of the @Resource annotation | ||
No | Yes | Settings such as <linked-to> in the property file #2 | |
No | The name attribute settings of the @Resource annotation | ||
No | If the @Resource annotation is specified in a field/ method, the default value is used and the result is the same as when 'Yes' is specified.#4 Also, if the @Resource annotation is specified in a class, the name attribute of the @Resource annotation cannot be omitted. If the name attribute of the @Resource specified in the class does not exist, the reading of the annotation fails and an error occurs during import.#5 |
#1: The optional name of the resource can also be specified in <resource-ref> or <res-ref-name> of the default DD corresponding to the name attribute of the @Resource annotation. If the optional name is not specified in the mappedName attribute of the @Resource annotation and in <linked-to> of the property file, the resource that has the optional name specified in <resource-ref> or <res-ref-name> of the default DD becomes the reference destination.
#2: Includes cosminexus.xml and the property files other than cosminexus.xml.
#3: If the optional name is not set, the application fails to start.
#4: If the optional name is not specified in the mappedName attribute of the @Resource annotation and <linked-to> in the property file (cosminexus.xml and the property files other than cosminexus.xml) and if the resource optional name matches with the default value of the name attribute or the name attribute value of the @Resource annotation specified in the class, the resource having that optional name becomes the reference destination.
#5: If the optional name is not specified in the mappedName attribute and <linked-to> in the property file (cosminexus.xml and the property files other than cosminexus.xml) and if the resource optional name matches with the name attribute value of @Resource specified in the class, the resource having that optional name becomes the reference destination.