在Java SE中使用JNDI

http://book.csdn.net/bookfiles/371/10037114080.shtml
http://www.java3z.com/cwbwebhome/article/article2/21040.html?id=1780
http://www.javaeedev.com/blog/article.jspx?articleId=8a41b5bd128e6daa01128e9214620013
http://docs.sun.com/source/819-1553/jndi.html
http://www.594k.com/java/html/y2007m6/13826/
JNDI全攻略
http://hankun.blogbus.com/logs/1774694.html
http://blog.donews.com/foxgem/archive/2005/05/30/404803.aspx
需要实现Referenceable接口,并实现RetReference方法。
package jndi;

import java.io.Serializable;
import java.rmi.Remote;

import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;

public class Person implements Referenceable{
	
	private String name;
	
	private int age;	
	
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public Reference getReference() throws NamingException {
		// TODO Auto-generated method stub
		Reference ref =  new Reference(getClass().getName(), SimpleFactory.class.getName(),null);
		ref.add(new StringRefAddr("name",name));
		ref.add(new StringRefAddr("age",new Integer(age).toString()));
		return ref;
	}
}

_____________________________________________________________________
package jndi;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

public class SimpleFactory implements ObjectFactory {

	@Override
	public Object getObjectInstance(Object obj, Name name, Context ctx,
			Hashtable<?, ?> env) throws Exception {
		// TODO Auto-generated method stub
		  if(obj instanceof Reference){
			  Reference ref=(Reference)obj;
			   String name1=(String)ref.get("name").getContent();
			   int age=Integer.parseInt(ref.get("age").getContent().toString());			  
			   return new Person(name1,age);
		  }		
		return null;
	}

}



______________________________________________________________________

package jndi;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class DataBind {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	       Hashtable hs = new Hashtable();

	        //设置连接LDAP的实现工厂
	        hs.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");

	        hs.put(javax.naming.Context.PROVIDER_URL,"file:///c:/");

	        try {
	        	InitialContext ictx = new InitialContext(hs);
	        	Person o = (Person)ictx.lookup("ttt/a");
	        	System.out.print(o.getAge());
	        } catch (NamingException ex) {
	           System.err.println("bind object fail: " + ex.toString());
	        }  
		}

}

你可能感兴趣的:(java,html,.net,Blog,sun)