Java JNDI名称java:/ [英] Java JNDI Name java:/

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

问题描述

我正在学习本教程: https://docs.oracle.com/javase/tutorial/jndi/index.html

在使用WildFly应用程序服务器为数据源设置JNDI名称时,我的冒险开始了.名称以"java:/"开头.我对它是什么以及它如何工作感到好奇.

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.

我在本地设置了Apache Directory LDAP服务器,并且可以通过以下方式连接到它:

I have Apache Directory LDAP server setup locally and I'm able to connect to it with:

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();
    }

我的困惑是JNDI名称为"java:/".

My confusion is the JNDI name "java:/".

有人可以解释一下"java:/"是什么,以及如何使用JNDI与之交互?

Can someone please explain what "java:/" is and how I can use JNDI to interact with it?

我的假设是它位于我计算机上某个位置的目录.

My assumption is its a directory located somewhere on my computer.

谢谢.

推荐答案

解释的名称如下:JNDI是"Java命名和目录接口".它是Java EE规范的一部分,并为Java客户端提供了一个API,用于按名称发现和查找数据和对象.这些对象可通过某些上下文访问,例如

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.

系统提供的对象(例如JTA UserTransaction对象)的名称存储在命名上下文java:comp/env的环境中. Java EE平台允许组件命名用户定义的对象,例如企业Bean,环境条目,JDBC DataSource对象和消息连接.应该根据对象的类型在命名环境的子上下文中命名对象.例如,企业bean在子上下文java:comp/env/ejb中命名,而JDBC DataSource引用在子上下文java:comp/env/jdbc中命名.

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.

ref: http://docs.oracle. com/cd/E19798-01/821-1841/girdr/index.html

正如Pawel在评论中指出的那样,Wildfly文档在这里非常有帮助:

As Pawel noted in his comment, the Wildfly docs are very helpful here:

Java EE平台规范定义了以下JNDI上下文:

The Java EE platform specification defines the following JNDI contexts:

  • java:comp-名称空间的作用域是当前组件(即EJB)
  • java:module-适用于当前模块
  • java:app-适用于当前应用程序
  • java:global-适用于应用程序服务器

除了标准名称空间外,WildFly还提供以下两个全局名称空间:

In addition to the standard namespaces, WildFly also provides the following two global namespaces:

  • java:jboss
  • java:/

因此,"java:/"只是Wildfly中的全局命名空间(和上下文),应与文件夹混淆.它只是目录中的命名地址",用于访问JDBC,EJB,LDAP等对象和服务.

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 EE规范非常有用:

For further information, the Java EE spec is useful:

  • (Java 6) http://docs.oracle.com/cd/E19798-01/821-1841/girdr/index.html
  • (Java 7, PDF) http://download.oracle.com/otn-pub/jcp/java_ee-7-fr-spec/JavaEE_Platform_Spec.pdf

这篇关于Java JNDI名称java:/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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