Apache Commons的BeanUtils提供了getPropertey()方法但返回值为String,如果bean中有list等值域就无法取得,所以写了一个通过java reflect方法取得Object类型值域的方法
java 代码
-
-
-
-
-
-
-
-
- public static Object getProperty(Object bean, String fieldName) {
- Field[] fields = bean.getClass().getDeclaredFields();
- Field.setAccessible(fields, true);
- Object obj = null;
- for (int i = 0; i < fields.length; i++) {
- Field field = fields[i];
-
- if (fieldName.equals(field.getName())) {
- try {
- obj = field.get(bean);
- } catch (IllegalArgumentException e) {
- log.error(e.getMessage());
- } catch (IllegalAccessException e) {
- log.error(e.getMessage());
- }
- }
- }
- return obj;
- }