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

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

问题描述

java:comp / env 是什么意思?

看起来像是什么:

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

做什么?

我明白了查找如下:

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

上下文中查找名称 MyDatasource 。 xml web.xml 获取数据库的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组件(Web应用程序或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天全站免登陆