classpath下加载文件资源的一种方式

public class DaoFactory {
	
	private static BaseDao baseDao = null;
	private static DaoFactory instance = new DaoFactory();
	
	private DaoFactory() {
		
		try {
			
			Properties prop = new Properties();
			// 只要在类路径下就可以找到
			InputStream inputStream = DaoFactory.class.getClassLoader().getResourceAsStream("dao.properties");
					//new FileInputStream(new File("src/dao.properties"));
			prop.load(inputStream);
			String userDaoClass = prop.getProperty("userDaoClass");
			Class clazz =  Class.forName(userDaoClass);
			baseDao = (BaseDao) clazz.newInstance();
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExceptionInInitializerError(e);
		}
		
	}
	
	public static DaoFactory getInstance() {
		return instance;
	}
	
	public BaseDao getBaseDao() {
		return baseDao;
	}
	
}

你可能感兴趣的:(classpath)