java:comp/env/是做什么的? [英] What does java:comp/env/ do?

查看:141
本文介绍了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>

我实际上是写这个的...

I had actually written this...

<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前缀的情况下,我会收到一条错误消息,提示名称jdbc在此上下文中未绑定".

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

推荐答案

引用

在名称空间的根上下文中 是名称为"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>

然后您可以使用以下命令直接访问它:

Then you can access it directly using:

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天全站免登陆