一、如何读取?
- smy.properties配置文件内容
- 使用如下代码,传入文件名和key参数,即可得到value的值
/**
*
* @param pro 配置文件名
* @param key
* @return
* @throws IOException
*/
private static String readProperty2(String pro,String key) throws IOException {
Properties properties = PropertiesLoaderUtils.loadAllProperties(pro);
String value = properties.getProperty(key);
return value;
}
}
二、运用场景
结合类对象Class.forName和接口interface,可以将需要调用的实现类写入配置文件,执行的方法也可以写入配置文件
public class Test {
public static void main(String[] args) throws Exception {
String className = readProperty2("smy.properties","classname");
String methodName = readProperty2("smy.properties","methodname");
Class cls = Class.forName(className);
// Constructor con = cls.getDeclaredConstructor();
Method mt1 = cls.getDeclaredMethod(methodName);
Bank c = (Bank) cls.newInstance();
mt1.invoke(c);
}