Java JNDI 名称 java:/
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31439421/
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
Java JNDI Name java:/
提问by Brian
I'm following the tutorial: https://docs.oracle.com/javase/tutorial/jndi/index.html
我正在关注教程:https: //docs.oracle.com/javase/tutorial/jndi/index.html
My adventure started while setting a JNDI name for a datasource with the WildFly application server. The name started with "java:/". I was curious on what it was and how it worked.
我的冒险开始于使用 WildFly 应用程序服务器为数据源设置 JNDI 名称。名称以“java:/”开头。我很好奇它是什么以及它是如何工作的。
I have Apache Directory LDAP server setup locally and I'm able to connect to it with:
我在本地设置了 Apache Directory LDAP 服务器,并且可以通过以下方式连接到它:
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=JNDITutorial");
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
try {
Context ctx = new InitialContext(env);
Object obj = ctx.lookup("cn=Rosanna Lee,ou=People");
} catch (NamingException e) {
e.printStackTrace();
}
My confusion is the JNDI name "java:/".
我的困惑是 JNDI 名称“java:/”。
Can someone please explain what "java:/" is and how I can use JNDI to interact with it?
有人可以解释一下“java:/”是什么以及我如何使用 JNDI 与之交互?
My assumption is its a directory located somewhere on my computer.
我的假设是它位于我计算机上的某个目录。
Thank you.
谢谢你。
回答by sprockets
The explanation is in the name: JNDI is the "Java Naming and Directory Interface". It is part of the Java EE specification and provides an API for java clients to discover and look up data and objects by name. These objects are accessible via certain contexts, e.g.
解释在名称中:JNDI 是“Java 命名和目录接口”。它是 Java EE 规范的一部分,并为 Java 客户端提供了一个 API,用于按名称发现和查找数据和对象。这些对象可以通过某些上下文访问,例如
The names of system-provided objects, such as JTA UserTransaction objects, are stored in the environment naming context java:comp/env. The Java EE platform allows a component to name user-defined objects, such as enterprise beans, environment entries, JDBC DataSource objects, and message connections. An object should be named within a subcontext of the naming environment according to the type of the object. For example, enterprise beans are named within the subcontext java:comp/env/ejb, and JDBC DataSource references are named within the subcontext java:comp/env/jdbc.
系统提供的对象(例如 JTA UserTransaction 对象)的名称存储在环境命名上下文 java:comp/env 中。Java EE 平台允许组件命名用户定义的对象,例如企业 bean、环境条目、JDBC 数据源对象和消息连接。对象应该根据对象的类型在命名环境的子上下文中命名。例如,企业 bean 在子上下文 java:comp/env/ejb 中命名,JDBC 数据源引用在子上下文 java:comp/env/jdbc 中命名。
ref: http://docs.oracle.com/cd/E19798-01/821-1841/girdr/index.html
参考:http: //docs.oracle.com/cd/E19798-01/821-1841/girdr/index.html
As Pawel noted in his comment, the Wildfly docs are very helpful here:
正如 Pawel 在他的评论中所指出的,Wildfly 文档在这里非常有帮助:
The Java EE platform specification defines the following JNDI contexts:
Java EE 平台规范定义了以下 JNDI 上下文:
- java:comp - The namespace is scoped to the current component (i.e. EJB)
- java:module - Scoped to the current module
- java:app - Scoped to the current application
- java:global - Scoped to the application server
- java:comp - 命名空间限定为当前组件(即 EJB)
- java:module - 作用域为当前模块
- java:app - 作用域为当前应用程序
- java:global - 作用域为应用服务器
In addition to the standard namespaces, WildFly also provides the following two global namespaces:
除了标准命名空间之外,WildFly 还提供了以下两个全局命名空间:
- java:jboss
- java:/
- 爪哇:jboss
- 爪哇:/
So "java:/" is just a global namespace (and context) in Wildfly and should be confused with a folder. It is simply a "named address" in a directory to access objects and services like JDBC, EJB, LDAP, etc.
所以“java:/”只是 Wildfly 中的全局命名空间(和上下文),应该与文件夹混淆。它只是一个目录中的“命名地址”,用于访问 JDBC、EJB、LDAP 等对象和服务。
For further information, the Java EE spec is useful:
有关更多信息,Java EE 规范很有用: