今天写的一个工厂工具类
package com.student.tools; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ObjectFactory { private static Properties pro=new Properties(); public static Object getBean(String key){ InputStream is= ObjectFactory.class.getResourceAsStream("/bean.properties"); String className=null; try { pro.clear(); pro.load(is); className=pro.getProperty(key); Class<?> clazz= Class.forName(className); return clazz.newInstance(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("无法读取配置文件"); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException("无法找到类"+className); } catch (InstantiationException e) { e.printStackTrace(); throw new RuntimeException("无法实例化"+className); } catch (IllegalAccessException e) { e.printStackTrace(); throw new RuntimeException("无法访问"+className+"的构造函数"); } } }
编辑器加载中...