獲取 bean 對象屬性方法


@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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM