//配置文件内容
className=xxx
methodName=xxx
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
//通过反射运行配置文件
public class ReflectDemo7 {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
//Properties读取文件获取键值对
Properties properties = new Properties();
// 两种读取方式都行
// FileReader fileReader = new FileReader("Example1\\class.txt");
ClassLoader classLoader = ReflectDemo7.class.getClassLoader();
InputStream resource = classLoader.getResourceAsStream("pro.properties");
properties.load(resource);
// fileReader.close();
//获取值
String className = properties.getProperty("className");
String methodName = properties.getProperty("methodName");
//执行方法
Class<?> aClass = Class.forName(className);
Object instance = aClass.newInstance();
Method method = aClass.getMethod(methodName);
method.invoke(instance);
}
}