使用 Java 的反射机制
/**
* 属性映射:使用 Java 的反射机制来获取一个对象(通常是一个 Java 类的实例)中指定属性的值
* @param obj
* @param propertyName
* @return
* @throws Exception
*/
private static Object getPropertyByReflection(Object obj, String propertyName) throws Exception {
// 获取对象的类
Class<?> objClass = obj.getClass();
// 根据属性名获取对应的 Field 对象
Field field = objClass.getDeclaredField(propertyName);
// 构造属性的 getter 方法名,根据 JavaBean 命名规范
String getterMethodName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
// 获取 getter 方法
Method getterMethod = objClass.getMethod(getterMethodName);
// 调用 getter 方法获取属性值
Object propertyValue = getterMethod.invoke(obj);
return propertyValue;
}
使用apache的属性工具类PropertyUtils
Object cellValue = PropertyUtils.getProperty(esbProcess, code);