---
title: "jndi简单入门"
author: "brucejiang"
date: "2010-09-21T09:59:03Z"
tags: []
categories: ["编程语言"]
slug: "jndi"
draft: false
lastmod: "2010-09-21T09:59:35Z"
id: 768784
url: "https://brucejiang.iteye.com/blog/768784"
---

## [JNDI简单入门](http://www.blogjava.net/liudecai/archive/2010/06/20/323900.html)

JNDI就是为JAVA中命名和目录服务定义的JAVA API，是命名服务的抽象机制。 在J2EE中，JNDI的目的是用来查找J2EE服务器的注册资源 。 只要该对象在命名服务器上注册过，且你知道命名服务器的地址和该对象在命名服务器上注册的JNDI名。 这样你就可以在无需知道对象位置的情况下获取和使用对象。SUN对JNDI只提供接口，使用JNDI只需要用到JNDI接口而不必关心具体实现 。  
使用main方法做JNDI的demo时出现NoInitialContextException是因为无法从System.properties中获得 必要的JNDI参数，在服务器环境下，服务器启动时就把这些参数放到System.properties中了，于是直接new InitialContext()就搞定了。但是在单机环境下，可没有JNDI服务在运行，那就需要手动启动一个JNDI服务。在JDK 5的rt.jar中一共找到了4种SUN自带的JNDI实现：LDAP，CORBA，RMI，DNS。  
这4种JNDI要正常运行还需要底层的相应服务。一般我们没有LDAP或CORBA服务器，也就无法启动这两种JNDI服务，DNS用于查域名的，以后再研究，唯一可以在main()中启动的就是基于RMI的JNDI服务。  
现在我们就可以在main()中启动基于RMI的JNDI服务并且绑一个对象到JNDI上。注意，我直接把JNDI的相关参数放入了System.properties中，这样，后面的代码如果要查JNDI，直接new InitialContext()就可以了

在RMI中绑JNDI的限制是，绑定的对象必须是Remote类型，所以就需要自己扩展一个。其实JNDI还有两个 Context.SECURITY\_PRINCIPAL和Context.SECURITY\_CREDENTIAL，如果访问JNDI需要用户名和口令， 这两个也要提供，不过一般用不上。下面是两个使用JNDI的简单例子的代码，可以直接运行。

package com.ellen.jndi;

import java.io.Serializable;  
import java.rmi.Remote;  
import java.rmi.RemoteException;  
import java.rmi.registry.LocateRegistry;  
import java.util.Date;  
import javax.naming.Context;  
import javax.naming.InitialContext;  
import javax.naming.NamingException;

//在RMI中绑JNDI的限制是，绑定的对象必须是Remote类型  
class Person implements Remote, Serializable {  
 private static final long serialVersionUID = -8592182872966400365L;

 private String name;  
 private String pass;

 public String getName() {  
  return name;  
 }

 public void setName(String name) {  
  this.name = name;  
 }

 public String getPass() {  
  return pass;  
 }

 public void setPass(String pass) {  
  this.pass = pass;  
 }

 public String toString() {  
  return "name=" + this.getName() + "&pass=" + this.getPass();  
 }

}

// 在RMI中绑JNDI的限制是，绑定的对象必须是Remote类型  
// 外部扩展,可以内部扩展也可以外部扩展  
class RemoteDate extends Date implements Remote {  
};

public class Demo {  
 public static void initDate() throws NamingException, RemoteException {  
  // 设置参数  
  LocateRegistry.createRegistry(1099);  
  System.setProperty(Context.INITIAL\_CONTEXT\_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");  
  System.setProperty(Context.PROVIDER\_URL, "rmi://localhost:1099");  
  InitialContext ctx = new InitialContext();  
  // 在RMI中绑JNDI的限制是，绑定的对象必须是Remote类型  
  // 内部扩展,可以内部扩展也可以外部扩展  
  class RemoteDate extends Date implements Remote {  
  }  
  ;  
  ctx.bind("java:comp/env/systemStartTime", new RemoteDate());  
  ctx.close();  
 }

 public static void initDate2() throws NamingException, RemoteException {  
  // 设置参数  
  LocateRegistry.createRegistry(1099);  
  System.setProperty(Context.INITIAL\_CONTEXT\_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");  
  System.setProperty(Context.PROVIDER\_URL, "rmi://localhost:1099");  
  InitialContext ctx = new InitialContext();  
  // 自己扩展,可以内部扩展也可以外部扩展  
  // class RemoteDate extends Date implements Remote {  
  // }  
  // ;  
  ctx.bind("java:comp/env/systemStartTime", new RemoteDate());  
  ctx.close();  
 }

 public static void findDate() throws NamingException, RemoteException {  
  // 直接使用  
  InitialContext ctx = new InitialContext();  
  Date startTime = (Date) ctx.lookup("java:comp/env/systemStartTime");  
  System.out.println("+++++++++++++++++++++++" + startTime.toString());  
  ctx.close();  
 }

 public static void jndiDate() throws NamingException, RemoteException {  
  // Demo.initDate();  
  Demo.initDate2();  
  Demo.findDate();  
 }

 public static void initPerson() throws NamingException, RemoteException {  
  // 设置参数  
  LocateRegistry.createRegistry(1099);  
  System.setProperty(Context.INITIAL\_CONTEXT\_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");  
  System.setProperty(Context.PROVIDER\_URL, "rmi://localhost:1099");  
  InitialContext ctx = new InitialContext();  
  // Person person = new Person();  
  // person.setName("ellen");  
  // person.setPass("000727");  
  ctx.bind("java:comp/env/person", new Person());  
  ctx.close();  
 }

 public static void initPerson2() throws NamingException, RemoteException {  
  // 设置参数  
  LocateRegistry.createRegistry(1099);  
  System.setProperty(Context.INITIAL\_CONTEXT\_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");  
  System.setProperty(Context.PROVIDER\_URL, "rmi://localhost:1099");  
  InitialContext ctx = new InitialContext();  
  Person person = new Person();  
  person.setName("ellen");  
  person.setPass("000727");  
  ctx.bind("java:comp/env/person", person);  
  ctx.close();  
 }

 public static void findPerson() throws NamingException, RemoteException {  
  // 直接使用  
  InitialContext ctx = new InitialContext();  
  Person person = (Person) ctx.lookup("java:comp/env/person");  
  System.out.println("------" + person.toString());  
  System.out.println("------" + person.getName());  
  System.out.println("------" + person.getPass());  
  ctx.close();  
 }

 public static void jndiPerson() throws NamingException, RemoteException {  
  // Demo.initPerson();  
  Demo.initPerson2();  
  Demo.findPerson();  
 }

 public static void main(String\[] args) throws NamingException, RemoteException {  
  // 为什么两个jndi的例子不能同时运行  
  // internal error: ObjID already in use  
  // Demo.jndiDate();  
  Demo.jndiPerson();  
 }

}