BeanUtils.copyProperties这个方法,是将一个对象的属性复制给另一个。
我看了看源码,看机制好像是但我不确认是下面这样的:
现在调用的是基本的BeanUtils.copyProperties(OBJ1,OBJ2);
1.先判定是否为空;(具体怎么叫空我也没懂)
2.没有,接着读取OBJ2的类型 ;
3.获取OBJ2的属性;
4.获取ignore列表;
5.从OBJ2中遍历非忽略的属性名称;
6.在OBJ1中获取该属性名称的值;(赋值前还判断OBJ1该属性是否为空)
7.遍历完毕。
至此全部结束。
现在问题来了,由于数据库中有十几个字段是NTEXT字段存储大量的文字。但是并不是每个页面都要显示那些字段的,大部分只需要显示几个常用字段。
如果每次直接用BeanUtils.copyProperties赋值。会把大量大段文字进行复制。
尤其是一次可能读取数十条数百条数据的还是效率很低。
boss让我不要用这个方法,让我一个一个setXXX(getXXxxx)这样。
可是我觉得这样效率工作效率也很低。而且代码会变得很沉长。于是我觉得可以通过去除target OBJ里面的不必要的属性进行优化读取。
如果BeanUtils.copyProperties这个方法确实通过从target里面先读取 所需要的属性再进行检测source是否有才进行赋值的话,确实可以加快效率;
但是如果BeanUtils.copyProperties这个方法是先读取source里面,再进行检测target是否有该属性,那么即便是去掉target的属性,效率也只是获得部分提高。只是少了个赋值阶段。还是有读取阶段。
所以想问,BeanUtils.copyProperties这个方法具体是怎么样的一个机制??
刚干java半个月。不是很熟悉。
望大神不吝赐教~~~
附录:BeanUtils.copyProperties的实现
public static void copyProperties(Object source, Object target, String[] ignoreProperties)
throws BeansException {
copyProperties(source, target, null, ignoreProperties);
}
private static void copyProperties(Object source, Object target, Class<?> editable, String[] ignoreProperties)
throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
for (PropertyDescriptor targetPd : targetPds) {
if (targetPd.getWriteMethod() != null &&
(ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null && sourcePd.getReadMethod() != null) {
try {
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException("Could not copy properties from source to target", ex);
}
}
}
}
}
我看了看源码,看机制好像是但我不确认是下面这样的:
现在调用的是基本的BeanUtils.copyProperties(OBJ1,OBJ2);
1.先判定是否为空;(具体怎么叫空我也没懂)
2.没有,接着读取OBJ2的类型 ;
3.获取OBJ2的属性;
4.获取ignore列表;
5.从OBJ2中遍历非忽略的属性名称;
6.在OBJ1中获取该属性名称的值;(赋值前还判断OBJ1该属性是否为空)
7.遍历完毕。
至此全部结束。
现在问题来了,由于数据库中有十几个字段是NTEXT字段存储大量的文字。但是并不是每个页面都要显示那些字段的,大部分只需要显示几个常用字段。
如果每次直接用BeanUtils.copyProperties赋值。会把大量大段文字进行复制。
尤其是一次可能读取数十条数百条数据的还是效率很低。
boss让我不要用这个方法,让我一个一个setXXX(getXXxxx)这样。
可是我觉得这样效率工作效率也很低。而且代码会变得很沉长。于是我觉得可以通过去除target OBJ里面的不必要的属性进行优化读取。
如果BeanUtils.copyProperties这个方法确实通过从target里面先读取 所需要的属性再进行检测source是否有才进行赋值的话,确实可以加快效率;
但是如果BeanUtils.copyProperties这个方法是先读取source里面,再进行检测target是否有该属性,那么即便是去掉target的属性,效率也只是获得部分提高。只是少了个赋值阶段。还是有读取阶段。
所以想问,BeanUtils.copyProperties这个方法具体是怎么样的一个机制??
刚干java半个月。不是很熟悉。
望大神不吝赐教~~~
附录:BeanUtils.copyProperties的实现
public static void copyProperties(Object source, Object target, String[] ignoreProperties)
throws BeansException {
copyProperties(source, target, null, ignoreProperties);
}
private static void copyProperties(Object source, Object target, Class<?> editable, String[] ignoreProperties)
throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
for (PropertyDescriptor targetPd : targetPds) {
if (targetPd.getWriteMethod() != null &&
(ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null && sourcePd.getReadMethod() != null) {
try {
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException("Could not copy properties from source to target", ex);
}
}
}
}
}