JEE6:@Resource可以注入什么?

发布于 2024-12-02 12:15:54 字数 92 浏览 1 评论 0原文

我试图找出什么可以通过 @Resource 注释注入到无状态会话 Bean 中。我在哪里可以找到清单?它是否取决于容器(在我的例子中是 Glassfish 3.1.1)?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2024-12-09 12:15:54

JSR-250(Java 平台的通用注释) 解决了使用@Resource注解处理类时的容器;相关部分是 2.3,其中讨论了 @Resource 注释。

总的来说,部署描述符中可以作为 env-entryservice-refresource-ref、< code>message-destination-ref 或 resource-env-ref 可以使用 @Resource 注解进行注解以进行注入。其实例可以注入受支持的类的特定 Java 类型的列表也在同一部分中指定,并复制如下:


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

注意表中的最后一项 - 根据规范,与资源关联的任何管理对象,即存在于 JNDI 目录中的可以被注入。

此类类的明确列表,其实例必须由容器注入,可以从 Java EE 6 平台规范。这没有以任何形式列出,但尽管如此,本章指定了应用程序开发人员必须如何请求注入任何重要资源。本章中列出的大多数资源都是通过指定 @Resource 注释或显式用于该资源的另一个注释来注入的。

EJB 3.1规范可能会重复上述平台规范第5章的内容,针对特定于 EJB 容器的资源。相关详细信息在标题为“企业 Bean 环境”的第 16 章中,相关详细信息出现在标题为“Bean 提供者的责任”的小节中。

关于Servlet 3.0规范,相关细节可以在第15.5节中找到.4 讨论了 servlet 容器的@Resource 注释语义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文