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
…