對象拷貝
對象拷貝分為深拷貝和淺拷貝差凹。根據(jù)使用場景進行不同選擇期奔。在Java中,數(shù)據(jù)類型分為值類型(基本數(shù)據(jù)類型)和引用類型危尿,值類型包括int呐萌、double、byte谊娇、boolean肺孤、char等簡單數(shù)據(jù)類型,引用類型包括類济欢、接口赠堵、數(shù)組等復雜類型。
深度拷貝和淺度拷貝的主要區(qū)別在于是否支持引用類型的屬性拷貝法褥,本文將探討目前使用較多的幾種對象拷貝的方案茫叭,以及其是否支持深拷貝和性能對比。
關于深拷貝和淺拷貝的理解可以參考:http://www.reibang.com/p/e8c6155d9694
首先來創(chuàng)建兩個測試bean
注:一定要保證有set/get
方法,成員變量必須要同名
@Data
public class User1 {
String name;
String password;
String phone;
}
@Data
public class User2 {
String name;
String password;
String phone;
}
1.Spring的BeanUtils(簡單易用)
org.springframework.beans.BeanUtils
BeanUtils.copyProperties(源對象半等,目標對象)
測試方法:
public static void main(String[] args){
User1 user1=new User1();
user1.setName("user1_name");
user1.setPassword("user1_password");
user1.setPhone("user1_phone");
User2 user2=new User2();
BeanUtils.copyProperties(user1,user2);
System.out.println(user2.toString());
}
執(zhí)行結果:User2(name=user1_name, password=user1_password, phone=user1_phone)
實現(xiàn)原理
BeanUtils部分源碼分析
public abstract class BeanUtils {
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, (Class)null, (String[])null);
}
private static void copyProperties(Object source, Object target, @Nullable Class<?> editable, @Nullable 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;
PropertyDescriptor[] var7 = targetPds;
int var8 = targetPds.length;
for(int var9 = 0; var9 < var8; ++var9) {
PropertyDescriptor targetPd = var7[var9];
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 var15) {
throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
}
}
}
}
}
}
``````
}
實現(xiàn)的方式很簡單揍愁,就是對兩個對象中相同名字的屬性進行簡單get/set
,僅檢查屬性的可訪問性酱鸭÷鹂澹可以看到, 成員變量賦值是基于目標對象的成員列表, 并且會跳過ignore
的以及在源對象中不存在的, 所以這個方法是安全的, 不會因為兩個對象之間的結構差異導致錯誤
注:必須保證同名的兩個成員變量類型相同,同名屬性一個是包裝類型凹髓,一個是非包裝類型也是可以的
2.Apache的BeanUtils(拓展性強烁登,相對復雜)
Apache Common BeanUtil是一個常用的在對象之間復制數(shù)據(jù)的工具類,web開發(fā)框架struts就是依賴于它進行ActionForm
的創(chuàng)建。
org.apache.commons.beanutils.BeanUtils
BeanUtils.copyProperties(目標對象饵沧,源對象)
需要引入依賴
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
測試方法:
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
User1 user1=new User1();
user1.setName("user1_name");
user1.setPassword("user1_password");
user1.setPhone("user1_phone");
User2 user2=new User2();
BeanUtils.copyProperties(user2,user1);
System.out.println(user2.toString());
}
執(zhí)行結果:User2(name=user1_name, password=user1_password, phone=user1_phone)
實現(xiàn)原理
BeanUtils部分源碼分析
public class BeanUtils {
public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
BeanUtilsBean.getInstance().copyProperties(dest, orig);
}
public void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
if (dest == null) {
throw new IllegalArgumentException("No destination bean specified");
} else if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
} else {
if (this.log.isDebugEnabled()) {
this.log.debug("BeanUtils.copyProperties(" + dest + ", " + orig + ")");
}
int i;
String name;
Object value;
if (orig instanceof DynaBean) {
DynaProperty[] origDescriptors = ((DynaBean)orig).getDynaClass().getDynaProperties();
for(i = 0; i < origDescriptors.length; ++i) {
name = origDescriptors[i].getName();
if (this.getPropertyUtils().isReadable(orig, name) && this.getPropertyUtils().isWriteable(dest, name)) {
value = ((DynaBean)orig).get(name);
this.copyProperty(dest, name, value);
}
}
} else if (orig instanceof Map) {
Iterator entries = ((Map)orig).entrySet().iterator();
while(entries.hasNext()) {
Entry entry = (Entry)entries.next();
name = (String)entry.getKey();
if (this.getPropertyUtils().isWriteable(dest, name)) {
this.copyProperty(dest, name, entry.getValue());
}
}
} else {
PropertyDescriptor[] origDescriptors = this.getPropertyUtils().getPropertyDescriptors(orig);
for(i = 0; i < origDescriptors.length; ++i) {
name = origDescriptors[i].getName();
if (!"class".equals(name) && this.getPropertyUtils().isReadable(orig, name) && this.getPropertyUtils().isWriteable(dest, name)) {
try {
value = this.getPropertyUtils().getSimpleProperty(orig, name);
this.copyProperty(dest, name, value);
} catch (NoSuchMethodException var7) {
}
}
}
}
}
}
``````
}
commons-beanutils
則施加了很多的檢驗锨络,包括類型的轉(zhuǎn)換,甚至于還會檢驗對象所屬的類的可訪問性狼牺。BeanUtils能夠順利的完成對象屬性值的復制羡儿,依賴于其對類型的識別。
除了支持基本類型以及基本類型的數(shù)組之外是钥,還支持java.sql.Date
,java.sql.Time
, java.sql.TimeStamp
, java.io.File
, javaio.URL
這些類的對象掠归,其余一概不支持。不過可以自定義Converter
悄泥。然后注冊進去虏冻。在org.apache.commons.beanutils.converters
包中有一系列converter
類,用于不同類型之間對象的轉(zhuǎn)化弹囚。主要通過注入新的類型轉(zhuǎn)換器厨相,因為默認情況下,BeanUtils對復雜對象的復制是引用
注:commons-beanutils中的裝換是不支持java.util.Date
的鸥鹉。
BeanUtils的官方API文檔:https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BeanUtils.html
converter的官方API文檔:https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/Converter.html
BeanUtils自定義的轉(zhuǎn)換器可以參考
https://blog.csdn.net/marksinoberg/article/details/51830076
https://blog.csdn.net/qq_14945847/article/details/77450222