java:comp/env/ 的作用是什么?

发布于 2024-09-30 12:07:41 字数 649 浏览 4 评论 0原文

我只是花了太多时间试图找出连接某些 JNDI 工厂 bean 时出现的一些错误。问题结果是,而不是这个...

<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/。从专家嘴里说出来,那有什么作用呢?

如果值中没有 java:comp/env/ 前缀,我会收到一条错误消息,指出“Name jdbc is not bond in this Context”。

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>

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2024-10-07 12:07:41

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

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

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

因此,您从 spring 或 Tomcat 上下文描述符进行的绑定默认位于 java:comp/env/ 下。 例如

,如果您的配置是:

<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”:

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

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

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".

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.

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");

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");
翻身的咸鱼 2024-10-07 12:07:41

JndiObjectFactoryBean 还有一个属性 resourceRef,当设置为 true 时,用于自动在前面添加字符串 java:comp/ env/(如果尚不存在)。

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

There is also a property resourceRef of JndiObjectFactoryBean that is, when set to true, used to automatically prepend the string java:comp/env/ if it is not already present.

<bean id="someId" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="jdbc/loc"/>
  <property name="resourceRef" value="true"/>
</bean>
晨敛清荷 2024-10-07 12:07:41

经过多次尝试并深入 Tomcat 的源代码,我发现简单的属性 useNaming="false" 成功了!!现在 Tomcat 解析名称 java:/liferay 而不是 java:comp/env/liferay

After several attempts and going deep in Tomcat's source code I found out that the simple property useNaming="false" did the trick!! Now Tomcat resolves names java:/liferay instead of java:comp/env/liferay

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文