java jndi 开发,Java 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.

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

Hashtable env = new Hashtable();

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:/".

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.

Thank you.

解决方案

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.

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.

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

The Java EE platform specification defines the following JNDI contexts:

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

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

java:jboss

java:/

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.

For further information, the Java EE spec is useful:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值