package com.itheima.exam.factory;
import java.io.FileInputStream;
import java.util.Properties;
import com.itheima.exam.dao.StudentDao;
public class DaoFactory {
private String studentDaoClassName;
//工厂
//单例
private DaoFactory(){
try {
//读取配置文件 获取实现类的类名
Properties props = new Properties();
props.load(new FileInputStream("src/dao.properties"));
this.studentDaoClassName = props.getProperty("studentDao");
} catch (Exception e) {
// TODO: handle exception
throw new ExceptionInInitializerError();
}
}
private static DaoFactory factory=new DaoFactory();
public static DaoFactory getInstance(){
return factory;
}
//生产dao
public StudentDao newstuStudentDao(){
try {
return (StudentDao) Class.forName(this.studentDaoClassName).newInstance();
} catch (Exception e) {
throw new FactoryException(e);
}
}
}
|