日期:2014-05-16  浏览次数:20696 次

经典JDBC DAOFactory类实现
package ajax.user.language.factory.dao_factory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;


/**
 * 这是我个人最近在学JDBC技术时:总结以前别人写过代码开发一个自己个人一个小工具(只针对初学者)
 * 该程序主要是读取配置文件来产生dao层实现对象(采用了反射技术);
 * 程序优点:(1)可以产生DAO层多种不同实现对象(返回Object对象需要强制转换)
 * 		   (2)在多线程模式下也只有一个工厂对象。
 *         (3)使用生产dao层实现对象,即使底层实现有JDBC成hibernate或者其他实现,不需要修改代码只需修改配置文件
 * @author kevin      Email:wanwei.234@163.com
 * 2010-01-09
 */
public class DaoFactory {
	/**工厂对象引用*/
	private static DaoFactory daoFactory=null;
	/**dao实现对象引用*/
	private static Object daoImpl=null;
	/**阻止new对象*/
	private DaoFactory(){}
	/**创建工厂对象*/
	public static DaoFactory getDaoFactoryInstance(){
		if(daoFactory==null){
			synchronized(DaoFactory .class){
			if(daoFactory==null){//如果没有这个判断,当两个线程同时访问这个方法会出现创建两个对象
				daoFactory=new DaoFactory();
		    }
		  }
		}
		return daoFactory;
	}
	/**
	 * 这是通过properties文件来产生dao实现类对象
	 * properti文件写法:userDao=ajax.user.language.entity.User(这是类的全名);
	 * 该方法返回的是一个Object对象,所以在使用时强制转换为你所需要的实现对象。
	 * @param key userDao
	 * @param filePath properties文件路径
	 * @return Object对象
	 */
	public Object produceDaoImpObject(String key,String filePath){
		try {
			Properties prop=new Properties();
			InputStream input=new FileInputStream(new File(filePath));
			prop.load(input);
			String daoClass=prop.getProperty(key);
			daoImpl=Class.forName(daoClass).newInstance();
		} catch (Exception e) {
			throw new ExceptionInInitializerError();
		}
		return daoImpl;
	}
	/**
	 *通过配置文件名称读取src目录的的配置文件
	 * @param key
	 * @return Object对象
	 */
	public Object produceDaoImpObjectOverFileName(String key,String FileName){
		try {
			Properties prop=new Properties();
			InputStream input=DaoFactory.class.getClassLoader().getResourceAsStream(FileName);
			prop.load(input);
			String daoClass=prop.getProperty(key);
			System.out.println(daoClass);
			daoImpl=Class.forName(daoClass).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExceptionInInitializerError();
		}
		return daoImpl;
	}
}
?测试类:
package ajax.user.language.dao.impl;

import ajax.user.language.entity.Student;
import ajax.user.language.factory.dao_factory.DaoFactory;

public class Test {
	
	public static void main(String[] args) {
		DaoFactory daoFactory=DaoFactory.getDaoFactoryInstance();
		//StuDaoImpl stuImpl=(StuDaoImpl)daoFactory.produceDaoImpObjectOverFileName("StudentDaoImpl","daoconfig.properties");
		//StuDaoImpl stuImpl=(StuDaoImpl)daoFactory.produceDaoImpObject("StudentDaoImpl","D:\\newapps\\workspace\\ajax\\src\\daoconfig.properties");
	    StuDaoImpl stuImpl=(StuDaoImpl)daoFactory.produceDaoImpObject("StudentDaoImpl","src/daoconfig.properties");
		Student s1=new Student();
		s1.setId("stu1");
		s1.setName("zhangsan");
		s1.setSex("F");
		try {
			stuImpl.addStudent(s1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}

?配置文件:daoconfig.properties

StudentDaoImpl=ajax.user.language.dao.impl.StuDaoImpl

?