gpt4 book ai didi

spring - java :comp/env/do?是什么意思

转载 作者:IT老高 更新时间:2023-10-28 13:01:09 31 4
gpt4 key购买 nike

在连接一些 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 bound in this Context”。 p>

最佳答案

引用 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.

因此,您从 spring 或例如从 tomcat 上下文描述符所做的绑定(bind)默认位于 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");

关于spring - java :comp/env/do?是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4099095/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com