Java通过反射机制动态设置对象属性值的方法
更新时间:2016年07月24日 13:26:53 投稿:jingxian
下面小编就为大家带来一篇Java通过反射机制动态设置对象属性值的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
/** * MethodName: getReflection<br> * Description:解析respXML 在通过反射设置对象属性值 * User: liqijing * Date:2015-7-19下午12:42:55 * @param clzzName * @param respXML * @return * @throws ClassNotFoundException * @throws DocumentException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException * @throws SecurityException * @throws NoSuchFieldException */ public static <T> Object getReflection(String clzzName , String respXML) throws ClassNotFoundException, DocumentException, IllegalArgumentException, IllegalAccessException, InstantiationException, SecurityException, NoSuchFieldException{ Object o = Class.forName(clzzName).newInstance(); Class clz = Class.forName(clzzName).newInstance().getClass(); Document doc = null ; doc = DocumentHelper.parseText(respXML); Element el = doc.getRootElement(); for (Field f : clz.getDeclaredFields()){ Iterator it=el.elementIterator(); while(it.hasNext()){ Element elt = (Element) it.next(); if(f.getName().equals(elt.getName())){ f = clz.getDeclaredField(elt.getName()); f.setAccessible(true); f.set(o, elt.getText()); } } } return o; }
Dear All:
在开发过程中有类似的需求通过反射动态设置属性值,希望有帮助。也很高兴与大家分享,谢谢。
以上这篇Java通过反射机制动态设置对象属性值的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
解决CentOS7中运行jar包报错:xxx(Permission denied)
在实际工作我们经常会在linux上运行Spring boot编写的微服务程序,下面这篇文章主要给大家介绍了关于如何解决CentOS7中运行jar包报错:xxx(Permission denied)的相关资料,需要的朋友可以参考下2024-02-02浅析SpringBoot2底层注解@Conditional@ImportResource
这篇文章主要为大家介绍了SpringBoot2底层注解@Conditional@ImportResource的分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-05-05Spring定时任务关于@EnableScheduling的用法解析
这篇文章主要介绍了Spring定时任务关于@EnableScheduling的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-06-06
最新评论