简体   繁体   English

ejb中的资源注入

[英]resource injection in ejb

I don't understand the difference between lookup parameter and name parameter of the @Resource annotation. 我不明白@Resource批注的lookup参数和name参数之间的区别。 I read all the documentation about the annotation and its field etc. 我阅读了有关注释及其字段等的所有文档。

I know how name can be used to inject or bind a resource, but the problem is that did not find any example that explain the difference between lookup and name . 我知道如何使用name来注入或绑定资源,但是问题是没有找到任何示例来解释lookupname之间的区别。

Can anyone give me a such example or tell me the difference between them ? 谁能给我一个这样的例子,或者告诉我他们之间的区别?

name is a "virtual" reference name in the java:comp/env namespace. namejava:comp/env命名空间中的“虚拟”引用名称。 At some level, that reference needs to point to a "physical" resource that has been defined. 在某种程度上,该引用需要指向已定义的“物理”资源。 The standard way to do that is with the lookup attribute, but products will also have a way to "bind" the the resource reference, so in many products, the product-specific binding is functionally equivalent to the name. 执行此操作的标准方法是使用lookup属性,但是产品也将具有“绑定”资源引用的方法,因此在许多产品中,特定于产品的绑定在功能上等同于名称。 (The product-specific binding is still important because you don't want to rely on developers to hard-code data source names from the operational environment, etc.) (特定于产品的绑定仍然很重要,因为您不想依靠开发人员从操作环境中硬编码数据源名称,等等。)

For example, you might have @Reference(name="jdbc/myDS", lookup="jdbc/oracleDS") , which means java:comp/env/myDS will be defined as an indirect lookup to the jdbc/oracleDS data source that was bound to the default JNDI namespace. 例如,您可能具有@Reference(name="jdbc/myDS", lookup="jdbc/oracleDS") ,这意味着java:comp/env/myDS将被定义为对以下数据库的jdbc/oracleDS数据源的间接查找:被绑定到默认的JNDI名称空间。

For another example, you might have @DataSourceDefinition(name="derbyDS") and @Resource(name="jdbc/myDS", lookup="java:comp/env/derbyDS") . 再举一个例子,您可能具有@DataSourceDefinition(name="derbyDS")@Resource(name="jdbc/myDS", lookup="java:comp/env/derbyDS") The former defines an actual data source (again relative to java:comp ), and the latter defines an indirect lookup to it. 前者定义了实际的数据源(再次相对于java:comp ),而后者定义了对它的间​​接查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM