JNDI的学习
JNDI全称 Java Naming and Directory Interface
JNDI是Java平台的一个标准扩展,提供了一组接口、类和关于命名空间的概念。如同其它很多Java技术一样,JDNI是provider-based的技术,暴露了一个API和一个服务供应接口(SPI)。这意味着任何基于名字的技术都能通过JNDI而提供服务,只要JNDI支持这项技术。JNDI目前所支持的技术包括LDAP、CORBA Common Object Service(COS)名字服务、RMI、NDS、DNS、Windows注册表等等。很多J2EE技术,包括EJB都依靠JNDI来组织和定位实体。
JDNI通过绑定的概念将对象和名称联系起来。在一个文件系统中,文件名被绑定给文件。在DNS中,一个IP地址绑定一个URL。在目录服务中,一个对象名被绑定给一个对象实体。
JNDI中的一组绑定作为上下文来引用。每个上下文暴露的一组操作是一致的。例如,每个上下文提供了一个查找操作,返回指定名字的相应对象。每个上下文都提供了绑定和撤除绑定名字到某个对象的操作。JNDI使用通用的方式来暴露命名空间,即使用分层上下文以及使用相同命名语法的子上下文。
jndi的用途:
1。你可以用jndi来得到object类的属性
如:Attribute attr =directory.getAttributes(personName).get("email");
String email = (String)attr.get();
2。你可以用jndi来搜索对象
如:foxes = directory.search("o=Wiz,c=US", "sn=Fox", controls);
查找谁的名字叫Fox在wiz部门的员工?
3。你可以用jndi通过naming/directory服务查询像printers和databases的对象
如:查询 Printer
Printer printer = (Printer)namespace.lookup(printerName);
printer.print(document);
4。你可以用jndi列表出命名空间的特殊级别的内容
如:
NamingEnumeration list = namespace.list("o=Widget, c=US");
while (list.hasMore()) {
NameClassPair entry = (NameClassPair)list.next();
display(entry.getName(), entry.getClassName());
}
以上根据jndi文档翻译的
地址:
http://java.sun.com/products/jndi/overview.html
tomcat数据库连接池配置中的
java:comp/env代表你的JVM的环境,comp=computer env=environment
JNDI全攻略之(一)
- javax.naming
- javax.naming.directory
- javax.naming.event
- javax.naming.ldap
- javax.naming.spi
<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"></shapetype>
-
LDAP(Lightweight Directory Access Protocol)服务提供者
-
CORBA COS(Common Object Request Broker Architecture Common Object Services)命名服务提供者
-
RMI(Java Remote Method Invocation)注册服务提供者
-
DNS(Domain Name System)服务提供者.
-
FSSP(File System Service Provider)文件系统服务提供者
-
其它服务提供者
* Created on 2005-3-4
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.sily.jndi;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
* @author shizy
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class TestJbossJNDI {
/**
*
*/
public TestJbossJNDI() {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) { try {
Properties env = new Properties();
env.load(new FileInputStream("jbossJndi.properties"));
env.list(System.out);
InitialContext ctx = new javax.naming.InitialContext(env);
System.out.println("Got context");
//create a subContext
ctx.createSubcontext("/sylilzy");
ctx.createSubcontext("sylilzy/sily");
ctx.rebind("sylilzy/sily/a", "I am sily a!");
ctx.rebind("sylilzy/sily/b", "I am sily b!");
//lookup context
Context ctx1=(Context)ctx.lookup("sylilzy");
Context ctx2=(Context)ctx1.lookup("/sylilzy/sily");
ctx2.bind("/sylilzy/g", "this is g");
//lookup binded object
Object o;
o=ctx1.lookup("sily/a");
System.out.println("get object from jndi:"+"get object from jndi:"+o);
//rename the object
ctx2.rename("/sylilzy/g", "g1");
o=ctx2.lookup("g1");
System.out.println("get object from jndi:"+"get object from jndi:"+o);
} catch (Exception e) {
e.printStackTrace();
}
}
}
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Got context
get object from jndi:I am sily a!
get object from jndi:this is g
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
/sylilzy/sily
-----------------------------
/sylilzy/sily/b:I am sily b!
/sylilzy/sily/a:I am sily a!
/sylilzy/sily/g1:this is g
-----------------------------
-----------------------------
<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"></shapetype>
本文介绍了JNDI(Java Naming and Directory Interface)的基本概念和技术特点,包括其架构与实现原理,并通过示例展示了如何使用JNDI进行对象绑定、查找及目录服务的操作。
3328

被折叠的 条评论
为什么被折叠?



