spring java:comp/env/ 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4099095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does java:comp/env/ do?
提问by Danny
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...
在连接一些 JNDI 工厂 bean 时,我只是花了太多时间试图找出一些错误。问题原来是,而不是这个......
<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?
我推断java:comp/env/可能引用了一些环境变量并使其最终被查看我的上下文文件。唯一的区别是java:comp/env/。从专家的嘴里说,那有什么作用?
Without the java:comp/env/prefix in the value, I would get an error that said "Name jdbc is not bound in this Context".
如果java:comp/env/值中没有前缀,我会收到一条错误消息,指出“Name jdbc is not bound in this Context”。
采纳答案by cherouvim
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.
在命名空间的根上下文是一个名为“comp”的绑定,它绑定到为组件相关绑定保留的子树。名称“comp”是组件的缩写。根上下文中没有其他绑定。但是,根上下文是为策略的未来扩展保留的,专门用于命名与组件本身无关而是与其他类型的实体(如用户或部门)相关的资源。例如,未来的策略可能允许您使用诸如“java:user/alice”和“java:org/engineering”之类的名称来命名用户和组织/部门。
在“comp”上下文中,有两个绑定:“env”和“UserTransaction”。名称“env”绑定到为组件的环境相关绑定保留的子树,如其部署描述符所定义。“env”是环境的缩写。J2EE 推荐(但不要求)以下结构用于“env”名称空间。
So the binding you did from spring or, for example, from a tomcat context descriptor go by default under java:comp/env/
因此,您从 spring 或例如从 tomcat 上下文描述符中所做的绑定默认在 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:
或者你可以做一个中间步骤,这样你就不必为你检索的每个资源指定“java:comp/env”:
Context ctx = new InitialContext();
Context envCtx = (Context)ctx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("foo");
回答by Filip Spiridonov
There is also a property resourceRefof JndiObjectFactoryBeanthat is, when set to true, used to automatically prepend the string java:comp/env/if it is not already present.
还有一个属性resourceRef的JndiObjectFactoryBean是,当设置为true,用于自动在前面加上字符串java:comp/env/,如果它是不存在的。
<bean id="someId" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/loc"/>
<property name="resourceRef" value="true"/>
</bean>
回答by Thiago Le?o Moreira
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:/liferayinstead of java:comp/env/liferay
经过多次尝试并深入研究 Tomcat 的源代码后,我发现简单的属性useNaming="false"成功了!!现在 Tomcat 解析名称java:/liferay而不是java:comp/env/liferay

