gpt4 book ai didi

java - 我根本不了解@Resource mappedName 元素

转载 作者:搜寻专家 更新时间:2023-11-01 01:21:53 24 4
gpt4 key购买 nike

我都读过,但我不明白,有人可以举个例子并用通俗易懂的英语解释一下吗?主要是它和“name”元素有什么区别/

来自 Oracle 教程

The mappedName element is a non-portable, implementation-specific name that the resource should be mapped to. Because the name element, when specified or defaulted, is local only to the application, many Java EE servers provide a way of referring to resources across the application server. This is done by setting the mappedName element. Use of the mappedName element is non-portable across Java EE server implementations.

来自 Oracle Javadocs映射名称

public abstract String mappedName

A product specific name that this resource should be mapped to. The name of this resource, as defined by the name element or defaulted, is a name that is local to the application component using the resource. (It's a name in the JNDI java:comp/env namespace.) Many application servers provide a way to map these local names to names of resources known to the application server. This mapped name is often a global JNDI name, but may be a name of any form. Application servers are not required to support any particular form or type of mapped name, nor the ability to use mapped names. The mapped name is product-dependent and often installation-dependent. No use of a mapped name is portable.

Default: ""

最佳答案

假设您在应用程序服务器中配置了一个连接池 (javax.sql.DataSource) 并放置在 JNDI 中,例如java:datasources/jta/MyDb.

假设您的应用程序的一个组件(可能是一个 EJB)需要访问一个数据库。 DataSource 可以注入(inject)为:

@Resource
private DataSource theDatabase;

您的组件可能是通用的,即可以部署在多个环境中。或者在单个环境中可能有许多数据源(java:datasources/jta/MyDb1, java:datasources/jta/MyDb2, ...)。如何将特定的 DataSource 映射到您的组件?

@Resource(mappedName="java:datasources/jta/MyDb")
private DataSource theDatabase;

(确切的细节可能会丢失或特定于应用程序服务器,但我希望你能理解一般的观点。)

关于java - 我根本不了解@Resource mappedName 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18759244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com