欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dao工厂类

程序员文章站 2022-05-23 19:41:36
...

import java.io.IOException;
import java.util.Properties;

public class DaoFactory {

private static Properties props = new Properties();

static {
	try {
		props.load(DaoFactory.class.getClassLoader().getResourceAsStream("dao.properties"));
	} catch (IOException e) {
		e.printStackTrace();
	}
}

/**
 * daoName
 * @param daoName
 * @return
 */
public static Object getInstance(String daoName) {
	String daoname = props.getProperty(daoName);
	// 通过反射用全路径名获取实例
	Object o = null;
	try {
		o = Class.forName(daoname).newInstance();
	} catch (InstantiationException e) {
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
	return o;
}

}

dao.properties文件配置

userDao = com.yanglei.daoImpl.UserDaoImpl

相关标签: Dao工厂类