java:comp/env/有什么作用? [英] What does java:comp/env/ do?

查看:28
本文介绍了java:comp/env/有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在连接一些 JNDI 工厂 bean 时花了太多时间试图找出一些错误.原来的问题是,而不是这个...

I just spent too much time of my day trying to figure out some errors when hooking up some JNDI factory bean. The problem turned out to be that instead of this...

<bean id="someId" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/loc"/>
</bean>

其实是我写的...

<bean id="someId" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="jdbc/loc"/>
</bean>

我推断 java:comp/env/ 可能引用了一些环境变量并使其成为最终查看我的上下文文件.唯一的区别是 java:comp/env/.从专家的口中,那有什么作用?

I infer that the java:comp/env/ perhaps references some environment variable and makes it so that, ultimately, my context file is looked at. The only difference is java:comp/env/. From an expert's mouth, what does that do?

如果值中没有 java:comp/env/ 前缀,我会收到一个错误消息,提示 Name jdbc is not bound in this Context".>

Without the java:comp/env/ prefix in the value, I would get an error that said "Name jdbc is not bound in this Context".

推荐答案

引用 https://web.archive.org/web/20140227201242/http://v1.dione.zcu.cz/java/docs/jndi-1.2/tutorial/beyond/misc/policy.html

在命名空间的根上下文是名称为comp"的绑定,绑定到保留的子树用于组件相关的绑定.这名称comp"是组件的缩写.没有其他绑定根上下文.然而,根上下文是为未来保留的扩大政策,特别是用于命名未绑定的资源到组件本身,但到其他实体类型,例如用户或部门.例如,未来策略可能允许您命名用户和组织/部门使用名称,例如java:user/alice"和java:org/engineering".

At the root context of the namespace is a binding with the name "comp", which is bound to a subtree reserved for component-related bindings. The name "comp" is short for component. There are no other bindings at the root context. However, the root context is reserved for the future expansion of the policy, specifically for naming resources that are tied not to the component itself but to other types of entities such as users or departments. For example, future policies might allow you to name users and organizations/departments by using names such as "java:user/alice" and "java:org/engineering".

在comp"上下文中,有两个绑定:env"和UserTransaction".名称env"绑定到子树这是为组件保留的与环境相关的绑定,如由其部署描述符定义.env"是环境的缩写.这J2EE 推荐(但不要求)env"的以下结构命名空间.

In the "comp" context, there are two bindings: "env" and "UserTransaction". The name "env" is bound to a subtree that is reserved for the component's environment-related bindings, as defined by its deployment descriptor. "env" is short for environment. The J2EE recommends (but does not require) the following structure for the "env" namespace.

因此,您从 spring 或例如从 tomcat 上下文描述符中进行的绑定默认在 java:comp/env/下进行

So the binding you did from spring or, for example, from a tomcat context descriptor go by default under java:comp/env/

例如,如果您的配置是:

For example, if your configuration is:

<bean id="someId" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="foo"/>
</bean>

然后你可以直接使用:

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/foo");

或者你可以做一个中间步骤,这样你就不必为你检索的每个资源指定java:comp/env":

or you could make an intermediate step so you don't have to specify "java:comp/env" for every resource you retrieve:

Context ctx = new InitialContext();
Context envCtx = (Context)ctx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("foo");

这篇关于java:comp/env/有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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