JEE6: What can I add with @Resource?

I am trying to figure out what can be inserted via the @Resource annotation into a session without a Bean state. Where can I find a list? Does it depend on the container (in my case it is Glassfish 3.1.1)?

+6
source share
1 answer

JSR-250 (General Annotations for the Java Platform) describes the behavior of the container runtime when processing classes using the @Resource annotation; the relevant section 2.3, which discusses the @Resource annotation.

In general, any resource that may be present in the deployment descriptor as env-entry , service-ref , resource-ref , message-destination-ref or resource-env-ref can be annotated with @Resource annotation for injection. A list of specific Java types whose instances can be introduced into supported classes is also listed in the same section and reproduced below:

 
  Java Type Equivalent Resource type 

 java.lang.String env-entry
 java.lang.Character env-entry
 java.lang.Integer env-entry
 java.lang.Boolean env-entry
 java.lang.Double env-entry
 java.lang.Byte env-entry
 java.lang.Short env-entry
 java.lang.Long env-entry
 java.lang.Float env-entry
 javax.xml.rpc.Service service-ref
 javax.xml.ws.Service service-ref
 javax.jws.WebService service-ref
 javax.sql.DataSource resource-ref
 javax.jms.ConnectionFactory resource-ref
 javax.jms.QueueConnectionFactory resource-ref
 javax.jms.TopicConnectionFactory resource-ref
 javax.mail.Session resource-ref
 java.net.URL resource-ref
 javax.resource.cci.ConnectionFactory resource-ref
 org.omg.CORBA_2_3.ORB resource-ref
 any other connection factory defined by a resource adapter resource-ref
 javax.jms.Queue message-destination-ref
 javax.jms.Topic message-destination-ref
 javax.resource.cci.InteractionSpec resource-env-ref
 javax.transaction.UserTransaction resource-env-ref
 Everything else resource-env-ref

Pay attention to the last element in the table - according to the specification, you can enter any managed object associated with a resource that is present in the JNDI directory.

A specific list of classes whose instances must be entered by the container can be obtained from Chapter 5 of the Java EE 6 Platform> specification . This is not tabulated in any form, but the chapter nonetheless determines how the application developer should request the injection of a significant resource. Most of the resources listed in the chapter are introduced by specifying the @Resource annotation or another annotation that is explicitly used for this resource.

the EJB 3.1 specification may repeat the contents of the above chapter 5 of the platform specification for EJB container-specific resources. The relevant details are given in chapter 16 under the name Bean Enterprise, and the relevant data are presented in the Bean Vendor Responsibilities subsections.

Regarding the Servlet 3.0 Specification, relevant information can be found in Section 15.5.4, which discusses the semantics of @Resource annotation for a servlet container.

+11
source

Source: https://habr.com/ru/post/896292/


All Articles