我根本不了解@Resource mappingName元素 [英] I don't understand @Resource mappedName element at all

查看:326
本文介绍了我根本不了解@Resource mappingName元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我都看过,但我听不懂,有人可以举一个例子,用简单的英语解释一下吗?主要是它和名称"元素/

I've read both and I don't understand them, can someone give an example and explain it in plain english please ? mainly what's the difference between it and the "name" element/

从Oracle教程

mappedName元素是不可移植的,特定于实现的名称 资源应映射到的位置.因为名称元素,当 指定或默认,仅对于应用程序是本地的,许多Java EE 服务器提供了一种在整个应用程序中引用资源的方式 服务器.这是通过设置mappingName元素来完成的.使用 mappingName元素不可跨Java EE服务器移植 实施.

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 映射的名称

From Oracle Javadocs mappedName

公共抽象字符串mappingName

public abstract String mappedName

此资源应映射到的产品特定名称.这 该资源的名称(由name元素定义或默认)为 使用资源的应用程序组件本地的名称. (这是JNDI java:comp/env命名空间中的名称.)许多应用程序 服务器提供了一种将这些本地名称映射到资源名称的方法 应用服务器知道的.此映射的名称通常是全局名称 JNDI名称,但可以是任何形式的名称.应用服务器不是 需要支持任何特定形式或类型的映射名称,也不 使用映射名称的能力.映射的名称取决于产品 并且通常取决于安装.不使用映射名称是可移植的.

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.

默认值:"

推荐答案

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

Say you have a connection pool (javax.sql.DataSource) configured in the application server and placed in JNDI under e.g. java:datasources/jta/MyDb.

说应用程序的组件(可能是EJB)需要访问数据库. DataSource可以注入为:

Say a component (an EJB probably) of your application needs to access a database. The DataSource could be injected as:

@Resource
private DataSource theDatabase;

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

Your component may be generic, i.e. can be deployed in multiple environments. Or in a single environment there may be many datasources (java:datasources/jta/MyDb1, java:datasources/jta/MyDb2, ...). How do you map the specific DataSource to your component?

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

(确切的细节可能丢失,或者是特定于应用程序服务器的,但是希望您能理解这一点.)

(Exact details could be missing or be application-server specific, but I hope you get the general point.)

这篇关于我根本不了解@Resource mappingName元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆