package com.ljl.human.utils;
import java.util.Properties;
import java.io.IOException;
public class ObjectUtils {
private static Properties properties=null;
static{
properties=new Properties();
try{
properties.load(ObjectUtils.class.getClassLoader().getResourceAsStream("object.properties"));
}catch (Exception e) {
throw new RuntimeException("配置文件加载失败....");
}
}
public static Object getObject(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
//第一步:获取我们的类路径(键对应我们的值-我们类的路径)
String classpath=properties.getProperty(name);
//第二步:通过反射创建我们的对象
Class classObj=Class.forName(classpath);
return classObj.newInstance();
}
}
用反射来生成对象(对象的工具类)
最新推荐文章于 2022-07-26 17:14:23 发布