比较的是四种复制的方式,分别为Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cglib的BeanCopier。...IMethodCallBack() { @Override public String getMethodName() { return "PropertyUtils.copyProperties...FromBean frombean) throws Exception { ToBean toBean = new ToBean(); PropertyUtils.copyProperties...从整体的表现来看,Cglib的BeanCopier的性能是最好的无论是数量较大的1万次的测试,还是数量较少10次,几乎都是趋近与零损耗,Spring是在次数增多的情况下,性能较好,在数据较少的时候,性能比PropertyUtils...PropertyUtils的性能相对稳定,表现是呈现线性增长的趋势。而Apache的BeanUtil的性能最差,无论是单次Copy还是大数量的多次Copy性能都不是很好。
你可以使用PropertyUtils类的getProperty方法来获取属性的值。...以下是如何获取Person对象的name属性的值: import org.apache.commons.beanutils.PropertyUtils; public class Main {...你可以使用PropertyUtils类的setProperty方法来设置属性的值。...以下是如何设置Person对象的age属性的值: import org.apache.commons.beanutils.PropertyUtils; public class Main {...复制指定属性 如果你只想复制JavaBean的部分属性,可以使用PropertyUtils类的copyProperty方法。
; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.beanutils.PropertyUtils...(bean, "name")); assertEquals(jsonObject.get("bool"), PropertyUtils.getProperty(bean, "bool"));...assertEquals(jsonObject.get("int"), PropertyUtils.getProperty(bean, "int")); assertEquals(jsonObject.get...("double"), PropertyUtils.getProperty(bean, "double")); assertEquals(jsonObject.get("func"),...PropertyUtils.getProperty(bean, "func")); List arrayList = (List) JSONArray.toCollection(jsonObject
PropertyUtils.getProperty import org.apache.commons.beanutils.PropertyUtils; import java.lang.reflect.InvocationTargetException...(new People(),"name")); } } 这样就能调用到创建的对象的对应 name 属性的 getter 方法,除此之外,PropertyUtils.getProperty 还支持递归获取属性...,比如a对象中有属性b,b对象中有属性c,我们可以通过 PropertyUtils.getProperty(a, "b.c"); 的方式进行递归获取 利用链剖析 首先我们知道这个方法可以调用对象的 getter...方法调用这个getter方法,但怎么从反序列化的readObject()方法调用到PropertyUtils.getProperty这个静态方法呢,继续找哪里会调用这个方法,可以发现熟悉的compare...(o1, this.property); // <-- 此处调用 Object value2 = PropertyUtils.getProperty(o2, this.property
JavaBean & PropertyUtils CB包中的PropertyUtils实际上是为了优化Java的Bean结构的而其中的getProperty函数通过有两个对象, 第一个为Object...所以PropertyUtils.getProperty就是执行bean中的getName函数并将得到的结果返回....(templates, "outputProperties"); 实际执行: Object end = templates.OutputProperties(); PropertyUtils.getProperty...CB中调用getProperty(中) 在上面我们找到了调用链的最后环节:通过调用TemplatesImpl#newTranformer动态加载类, 而进入这个最后环节的入口就是PropertyUtils...来到CommonsBeanutils中寻找一下调用了PropertyUtils#getProperty的函数方法然后在CB包中的BeanComparator#compare调用了getProperty,
PropertyUtils.copyProperties("要转换的类", "转换后的类"); 用法其实很简单,第一个参数是要转换的类,第二个参数是转换后的类。...BeanUtils.copyProperties VS PropertyUtils.copyProperties 两者最大的区别是: BeanUtils.copyProperties会进行类型转换,而PropertyUtils.copyProperties...既然进行了类型转换,那BeanUtils.copyProperties的速度比不上PropertyUtils.copyProperties。...因此,PropertyUtils.copyProperties应用的范围稍为窄一点,它只对名字和类型都一样的属性进行copy,如果名字一样但类型不一样,它会报错。...对于需求1,可以这样: import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.PropertyUtils
return name; } public void setName(String name) { this.name = name; } } 然后我们执行 PropertyUtils.getProperty...中存在如下方法TemplatesImpl#getOutputProperties() 我们可以发现它调用了newTransformer并且是一个getter方法,所以我们可以想办法用BeanUtils的PropertyUtils...org.apache.commons.beanutils.BeanComparator,它继承自java.util.Comparator接口,所以实现了compare方法: 如果传入两个对象时 this.property不为空,就会分别对两个对象调用PropertyUtils.getProperty...java.util.PriorityQueue,在java.util.PriorityQueue反序列化时自动调用BeanComparator#compare,这个时候想办法将传入该方法的对象改成恶意TemplatesImpl,使PropertyUtils.getProperty...PriorityQueue#siftDownUsingComparator() BeanComparator#compare() PropertyUtils
(userInfo, "age", 8); System.out.println(PropertyUtils.getProperty(userInfo, "age")); System.out.println...(PropertyUtils.getProperty(userInfo, "age").getClass().getName()); PropertyUtils.setProperty(userInfo...(PropertyUtils.java:896) at com.peidasoft.instrospector.BeanInfoTest.main(BeanInfoTest.java:32) Caused...e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } ---- PropertyUtils...由于age属性的数据类型是int,所以方法PropertyUtils.setProperty(userInfo,”age”, “8”)会爆出数据类型不匹配,无法将值赋给属性。
1.8.0_131] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_131] at com.atomikos.beans.PropertyUtils.setDirectProperty...(PropertyUtils.java:205) ~[atomikos-util-3.9.3.jar:na] at com.atomikos.beans.PropertyUtils.setProperty...(PropertyUtils.java:110) ~[atomikos-util-3.9.3.jar:na] at com.atomikos.beans.PropertyUtils.setProperties...(PropertyUtils.java:186) ~[atomikos-util-3.9.3.jar:na] at com.atomikos.jdbc.AtomikosDataSourceBean.doInit
System.out.println("get age type:" + BeanUtils.getProperty(userInfo, "age").getClass().getName()); PropertyUtils.setProperty...(userInfo, "age", 8); System.out.println(PropertyUtils.getProperty(userInfo, "age"));...System.out.println(PropertyUtils.getProperty(userInfo, "age").getClass().getName()); // 特殊...age属性为Integer类型 PropertyUtils.setProperty(userInfo, "age", "8"); } catch (IllegalAccessException...(PropertyUtils.java:896) at com.niocoder.test.introspector.BeanUtilTest.main(BeanUtilTest.java:28) Caused
属性拷贝工具类使用总结 字段和属性 使用说明 **org.springframework.beans.BeanUtils#copyProperties** org.apache.commons.beanutils.PropertyUtils...copyProperties_ org.apache.commons.beanutils.BeanUtils#_copyProperties Spring#BeanUtils apache.commons#PropertyUtils...属性拷贝工具类使用总结 对项目中经常使用的属性拷贝工具类进行总结: org.apache.commons.beanutils.BeanUtils org.apache.commons.beanutils.PropertyUtils...,Spring BeanUtils,Cglib BeanCopier) 摘要总结:Spring是在次数增多的情况下,性能较好,在数据较少的时候,性能比PropertyUtils的性能差一些。...PropertyUtils的性能相对稳定,表现是呈现线性增长的趋势。而Apache的BeanUtil的性能最差,无论是单次Copy还是大数量的多次Copy性能都不是很好。
实例代码1: import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.PropertyUtils...B.PropertyUtils以属性本身的类型进行操作。...实例1: Person p = new Person("ZHG",24); Object myName = PropertyUtils.getProperty(p,"name"); System.out.println...(myName); PropertyUtils.setProperty(p,"age",23); Object myAge = PropertyUtils.getProperty(p,"age"...); System.out.println(myAge); PropertyDescriptor d1 = PropertyUtils.getPropertyDescriptor(p,"name
password='123456', email='abc@163.com'} Book2{username='null', password='123456', email='abc@163.com'} PropertyUtils...使用PropertyUtils.copyProperties()拷贝一个bean中的属性到另一个bean中,第一个参数是目标bean,第二个参数是源bean。...PropertyUtils.copyProperties(novel, srcBook); 输出: System.out.println(novel.getName());
commons-beanutils中提供了一个静态方法PropertyUtils.getProperty,让使用者可以直接调用任意JavaBean的getter方法,比如: PropertyUtils.getProperty...除此之外,PropertyUtils.getProperty还支持递归获取属性,比如a对象中有属性b,b对象中有属性c,我们可以通过PropertyUtils.getProperty(a, "b.c")...( o1, property ); final Object value2 = PropertyUtils.getProperty( o2, property ); return...上一节我们说了,PropertyUtils.getProperty这个方法会自动去调用一个JavaBean的getter方法,这个点是任意代码执行的关键。...所以,PropertyUtils.getProperty( o1, property )这段代码,当o1是一个TemplatesImpl对象,而property的值为outputProperties时,
this.comparator.compare(o1, o2); } else { try { Object value1 = PropertyUtils.getProperty...(o1, this.property); Object value2 = PropertyUtils.getProperty(o2, this.property);...new RuntimeException("NoSuchMethodException: " + var7.toString()); } } } 通过PropertyUtils.getProperty...测试能否调用TemplatesImpl.getOutputProperties // PropertyUtils.getProperty(templates,"outputProperties...(o1, this.property); Object value2 = PropertyUtils.getProperty(o2, this.property);
BeanUtils 着重于”Bean”,返回值通常是 String,而 PropertyUtils 着重于属性,它的返回值通常是 Object。...; PropertyUtils.getSimpleProperty(Object bean, String name); 1 Indexed:如果你是用到 Collection 或 List 实作出来的...objects , 只需要使用一个 index 数值就可以取得数据的型态; PropertyUtils.getIndexedProperty(Object bean, String name); PropertyUtils.getIndexedProperty...name); PropertyUtils.getMappedProperty(Object bean, String name, String key); PropertyUtils.setMappedProperty...PropertyUtils.getNestedProperty(Object bean, String name); PropertyUtils.setNestedProperty(Object bean
void test() { BeanUtilsBean beanUtils = BeanUtilsBean.getInstance(); PropertyUtilsBean propertyUtils...= beanUtils.getPropertyUtils(); PropertyDescriptor[] origDescriptors = propertyUtils.getPropertyDescriptors
org.apache.commons.beanutils.BeanUtils; org.apache.commons.beanutils.PropertyUtils; org.springframework.cglib.beans.BeanCopier...} } "四大金刚" 性能统计 方法 1000 10000 100000 1000000 apache BeanUtils 906毫秒 807毫秒 1892毫秒 11049毫秒 apache PropertyUtils...性能走势 --> spring cglib BeanCopier 优于 spring copyProperties 优于 apache PropertyUtils 优于 apache BeanUtils...Apache PropertyUtils 源码分析 从源码可以清晰的看到,类型检查变成了非空校验,去掉了每一次copy的日志记录,性能肯定更好了。...Apache Beanutils 的性能问题出现在类型校验和每一次copy的日志记录; Apache PropertyUtils 进行了如下优化: 类型检查变成了非空校验 去掉了每一次copy的日志记录
实现浅克隆的常用API有以下3种, 1、工具类BeanUtils和PropertyUtils,BeanUtils是Spring提供的,PropertyUtils是Apache的commons包中提供的。
转载自 https://blog.csdn.net/jdjdndhj/article/details/62056137 第一步: BeanUtils.copyProperties()与PropertyUtils.copyProperties...4、BeanUtils与PropertyUtils对比(这里对比copyProperties方法) PropertyUtils的copyProperties()方法几乎与BeanUtils.copyProperties
领取专属 10元无门槛券
手把手带您无忧上云