应用 PropertyDescriptor 能够很不便的取得一个类的属性信息
PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(o.getClass(), propName);
能够很不便的获取 get、set 办法,不便进行反射相干解决
propertyDescriptor.getReadMethod()
propertyDescriptor.getWriteMethod()
参考示例,依据对象和属性名,获取属性值
private String getObjPropStrVal(Object o, String propName) {PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(o.getClass(), propName);
if (propertyDescriptor != null) {
Object val = null;
try {val = propertyDescriptor.getReadMethod().invoke(o);
} catch (Exception e) {e.printStackTrace();
}
if (val == null) {return null;} else if (propertyDescriptor.getPropertyType().getName().contains("Date")) {String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) val);
if (date.endsWith("00:00:00")) {date = date.substring(0, 10);
}
return date;
} else {return val.toString();
}
}
return null;
}