博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java复制属性
阅读量:6277 次
发布时间:2019-06-22

本文共 3082 字,大约阅读时间需要 10 分钟。

hot3.png

三种复制属性的工具

1.cglib

import net.sf.cglib.beans.BeanCopier;BeanCopier copier = BeanCopier.create(User.class, User.class, false);copier.copy(source, user, null);

2.org.apache.commons

import org.apache.commons.beanutils.BeanUtils;BeanUtils.copyProperties(user, source);

3.spring beans

org.springframework.beans.BeanUtils.copyProperties(source, user);

效率方面:

spring beans>=cglib>org.apache.commons.beanutils.BeanUtils

org.springframework.beans.BeanUtils与net.sf.cglib.beans.BeanCopier

在同一个数量级,区别不大,比org.apache.commons.beanutils.BeanUtils效率快一个数量级.

 

org.springframework.beans.BeanUtils.copyProperties最终调用的是以下这个方法.

/**	 * Copy the property values of the given source bean into the given target bean.	 * 

Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * @param source the source bean * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @param ignoreProperties array of property names to ignore * @throws BeansException if the copying failed * @see BeanWrapper */ 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
ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new FatalBeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }

之前以为bean中有bean或List的会递归调用,实际上并没有.用反射调用read method和write method.

转载于:https://my.oschina.net/u/657390/blog/747960

你可能感兴趣的文章
IP地址的划分实例解答
查看>>
如何查看Linux命令源码
查看>>
运维基础命令
查看>>
入门到进阶React
查看>>
SVN 命令笔记
查看>>
检验手机号码
查看>>
重叠(Overlapped)IO模型
查看>>
Git使用教程
查看>>
使用shell脚本自动监控后台进程,并能自动重启
查看>>
Flex&Bison手册
查看>>
solrCloud+tomcat+zookeeper集群配置
查看>>
/etc/fstab,/etc/mtab,和 /proc/mounts
查看>>
Apache kafka 简介
查看>>
socket通信Demo
查看>>
技术人员的焦虑
查看>>
js 判断整数
查看>>
建设网站应该考虑哪些因素
查看>>
mongodb $exists
查看>>
js实现页面跳转的几种方式
查看>>
sbt笔记一 hello-sbt
查看>>