什么是 java:comp/env? [英] what is java:comp/env?

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

问题描述

java:comp/env 是什么意思?

看起来像什么:

Context envContext = (Context)initContext.lookup("java:comp/env");

做什么?

我理解这样的查找:

(DataSource)envContext.lookup("jdbc/MyDatasource")

context.xmlweb.xml 中查找名称MyDatasource 以获取数据库的URL.是这样吗?!!但是前者查找有什么作用?

looks up for the name MyDatasource in the context.xml or web.xml to get the URL of the database. Is it so ? !! But what does the former look up do ?

推荐答案

java:comp/env 是 JNDI 树中的节点,您可以在其中找到当前 Java EE 组件(一个 webapp,或 EJB).

java:comp/env is the node in the JNDI tree where you can find properties for the current Java EE component (a webapp, or an EJB).

Context envContext = (Context)initContext.lookup("java:comp/env");

允许定义一个直接指向这个节点的变量.它允许做

allows defining a variable pointing directly to this node. It allows doing

SomeBean s = (SomeBean) envContext.lookup("ejb/someBean");
DataSource ds = (DataSource) envContext.lookup("jdbc/dataSource");

而不是

SomeBean s = (SomeBean) initContext.lookup("java:comp/env/ejb/someBean");
DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/dataSource");

相对路径而不是绝对路径.这就是它的用途.

Relative paths instead of absolute paths. That's what it's used for.

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

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