Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JEE6: What can be injected with @Resource?

I'm trying to find out, what can be injected via the @Resource annotation into a Stateless Session Bean. Where would I find a list? Does it depend on the container (in my case it's Glassfish 3.1.1)?

like image 571
Hank Avatar asked Aug 31 '11 08:08

Hank


1 Answers

JSR-250 (Common Annotations for the Java platform) addresses the runtime behavior of a container when processing classes with the @Resource annotation; the relevant section is 2.3, which discusses the @Resource annotation.

By and large, any resource that can be present in a deployment descriptor as a env-entry, service-ref, resource-ref, message-destination-ref or a resource-env-ref can be annotated with @Resource annotation for injection. The list of specific Java types whose instances can be injected into supported classes is also specified in the same section, and is 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

Note the last item in the table - according to the specification, any administered object associated with a resource, that is present in the JNDI directory can be injected.

A definite list of such classes whose instances must be injected by a container, can be obtained from Chapter 5 of the Java EE 6 Platform Specification. This is not tabulated in any form, but nevertheless, the chapter specifies how an application developer must request for injection of any significant resource. Most resources listed in the chapter are injected by specifying the @Resource annotation, or an another annotation that is used explicitly for that resource.

The EJB 3.1 specification may repeat the contents of afore mentioned chapter 5 of the platform specification, for resources specific to the EJB container. The relevant details are in chapter 16 titled "Enterprise Bean Environment", with the relevant details being present in sub-sections titled "Bean Provider's Responsibilities".

With respect to the Servlet 3.0 Specification, the relevant details may be found in Section 15.5.4 which discusses the @Resource annotation semantics for a servlet container.

like image 67
Vineet Reynolds Avatar answered Sep 21 '22 03:09

Vineet Reynolds