首页 > 其他 > 详细

获取 bean 对象属性方法

时间:2019-12-24 09:36:12      阅读:135      评论:0      收藏:0      [点我收藏+]
@Test
public void testBeanInfo() throws IntrospectionException, InvocationTargetException, IllegalAccessException {
BaseVo param = new BaseVo();
param.setCode(111);
param.setMessage("success");

Map<String, Object> map = Maps.newConcurrentMap();

BeanInfo beanInfo = Introspector.getBeanInfo(param.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
//获取属性对应的 value
Object value = getter.invoke(param);
//key=bean 属性,value=bean 属性对应的 value
map.put(key, value);
}
}

System.out.println(map);
}

获取 bean 对象属性方法

原文:https://www.cnblogs.com/ming-blogs/p/12089711.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!