欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析Java中Apache BeanUtils和Spring BeanUtils的用法

 更新時間:2020年11月12日 16:07:46   作者:java夢想口服液  
這篇文章主要介紹了Java中Apache BeanUtils和Spring BeanUtils的用法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

# 前言

在我們實際項目開發(fā)過程中,我們經(jīng)常需要將不同的兩個對象實例進行屬性復制,從而基于源對象的屬性信息進行后續(xù)操作,而不改變源對象的屬性信息,比如DTO數(shù)據(jù)傳輸對象和數(shù)據(jù)對象DO,我們需要將DO對象進行屬性復制到DTO,但是對象格式又不一樣,所以我們需要編寫映射代碼將對象中的屬性值從一種類型轉(zhuǎn)換成另一種類型。

# 對象拷貝

在具體介紹兩種 BeanUtils 之前,先來補充一些基礎(chǔ)知識。它們兩種工具本質(zhì)上就是對象拷貝工具,而對象拷貝又分為深拷貝和淺拷貝,下面進行詳細解釋。

# 什么是淺拷貝和深拷貝

在Java中,除了 基本數(shù)據(jù)類型之外,還存在 類的實例對象這個引用數(shù)據(jù)類型,而一般使用 “=”號做賦值操作的時候,對于基本數(shù)據(jù)類型,實際上是拷貝的它的值,但是對于對象而言,其實賦值的只是這個對象的引用,將原對象的引用傳遞過去,他們實際還是指向的同一個對象。

而淺拷貝和深拷貝就是在這個基礎(chǔ)上做的區(qū)分,如果在拷貝這個對象的時候,只對基本數(shù)據(jù)類型進行了拷貝,而對引用數(shù)據(jù)類型只是進行引用的傳遞,而沒有真實的創(chuàng)建一個新的對象,則認為是淺拷貝。反之,在對引用數(shù)據(jù)類型進行拷貝的時候,創(chuàng)建了一個新的對象,并且復制其內(nèi)的成員變量,則認為是深拷貝。

簡單來說:

淺拷貝:對基本數(shù)據(jù)類型進行值傳遞,對引用數(shù)據(jù)類型進行引用傳遞般的拷貝,此為淺拷貝

深拷貝:對基本數(shù)據(jù)類型進行值傳遞,對引用數(shù)據(jù)類型,創(chuàng)建一個新的對象,并復制其內(nèi)容,此為深拷貝。

兩難!到底用Apache BeanUtils還是Spring BeanUtils?

# BeanUtils

前面簡單講了一下對象拷貝的一些知識,下面就來具體看下兩種 BeanUtils 工具

# Apache 的 BeanUtils

首先來看一個非常簡單的BeanUtils的例子

publicclass PersonSource {
 private Integer id;
 private String username;
 private String password;
 private Integer age;
 // getters/setters omiited
}
publicclass PersonDest {
 private Integer id;
 private String username;
 private Integer age;
 // getters/setters omiited
}
publicclass TestApacheBeanUtils {
 public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
  //下面只是用于單獨測試
  PersonSource personSource = new PersonSource(1, "pjmike", "12345", 21);
  PersonDest personDest = new PersonDest();
  BeanUtils.copyProperties(personDest,personSource);
  System.out.println("persondest: "+personDest);
 }
}
persondest: PersonDest{id=1, username='pjmike', age=21}

從上面的例子可以看出,對象拷貝非常簡單,BeanUtils最常用的方法就是:

//將源對象中的值拷貝到目標對象//將源對象中的值拷貝到目標對象
public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
 BeanUtilsBean.getInstance().copyProperties(dest, orig);
}

但是由于 Apache下的BeanUtils對象拷貝性能太差,不建議使用,而且在阿里巴巴Java開發(fā)規(guī)約插件上也明確指出:

Ali-Check | 避免用Apache Beanutils進行屬性的copy。

commons-beantutils 對于對象拷貝加了很多的檢驗,包括類型的轉(zhuǎn)換,甚至還會檢驗對象所屬的類的可訪問性,可謂相當復雜,這也造就了它的差勁的性能,具體實現(xiàn)代碼如下:

public void copyProperties(final Object dest, final Object orig)
  throws IllegalAccessException, InvocationTargetException {

  // Validate existence of the specified beans
  if (dest == null) {
   thrownew IllegalArgumentException
     ("No destination bean specified");
  }
  if (orig == null) {
   thrownew IllegalArgumentException("No origin bean specified");
  }
  if (log.isDebugEnabled()) {
   log.debug("BeanUtils.copyProperties(" + dest + ", " +
      orig + ")");
  }

  // Copy the properties, converting as necessary
  if (orig instanceof DynaBean) {
   final DynaProperty[] origDescriptors =
    ((DynaBean) orig).getDynaClass().getDynaProperties();
   for (DynaProperty origDescriptor : origDescriptors) {
    final String name = origDescriptor.getName();
    // Need to check isReadable() for WrapDynaBean
    // (see Jira issue# BEANUTILS-61)
    if (getPropertyUtils().isReadable(orig, name) &&
     getPropertyUtils().isWriteable(dest, name)) {
     final Object value = ((DynaBean) orig).get(name);
     copyProperty(dest, name, value);
    }
   }
  } elseif (orig instanceof Map) {
   @SuppressWarnings("unchecked")
   final
   // Map properties are always of type <String, Object>
   Map<String, Object> propMap = (Map<String, Object>) orig;
   for (final Map.Entry<String, Object> entry : propMap.entrySet()) {
    final String name = entry.getKey();
    if (getPropertyUtils().isWriteable(dest, name)) {
     copyProperty(dest, name, entry.getValue());
    }
   }
  } else/* if (orig is a standard JavaBean) */ {
   final PropertyDescriptor[] origDescriptors =
    getPropertyUtils().getPropertyDescriptors(orig);
   for (PropertyDescriptor origDescriptor : origDescriptors) {
    final String name = origDescriptor.getName();
    if ("class".equals(name)) {
     continue; // No point in trying to set an object's class
    }
    if (getPropertyUtils().isReadable(orig, name) &&
     getPropertyUtils().isWriteable(dest, name)) {
     try {
      final Object value =
       getPropertyUtils().getSimpleProperty(orig, name);
      copyProperty(dest, name, value);
     } catch (final NoSuchMethodException e) {
      // Should not happen
     }
    }
   }
  }

 }

# Spring 的 BeanUtils

使用spring的BeanUtils進行對象拷貝:

publicclass TestSpringBeanUtils {
 public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {

  //下面只是用于單獨測試
  PersonSource personSource = new PersonSource(1, "pjmike", "12345", 21);
  PersonDest personDest = new PersonDest();
  BeanUtils.copyProperties(personSource,personDest);
  System.out.println("persondest: "+personDest);
 }
}

Spring下的BeanUtils也是使用 copyProperties方法進行拷貝,只不過它的實現(xiàn)方式非常簡單,就是對兩個對象中相同名字的屬性進行簡單的get/set,僅檢查屬性的可訪問性。具體實現(xiàn)如下:

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);

 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);
   }
   }
  }
  }
 }
 }

可以看到,成員變量賦值是基于目標對象的成員列表,并且會跳過ignore的以及在源對象中不存在,所以這個方法是安全的,不會因為兩個對象之間的結(jié)構(gòu)差異導致錯誤,但是必須保證同名的兩個成員變量類型相同

# 小結(jié)

以上簡要的分析兩種BeanUtils,因為Apache下的BeanUtils性能較差,不建議使用,可以使用 Spring的BeanUtils ,或者使用其他拷貝框架,比如:Dozer、ModelMapper等等

到此這篇關(guān)于淺析Java中Apache BeanUtils和Spring BeanUtils的用法的文章就介紹到這了,更多相關(guān)Apache BeanUtils和Spring BeanUtils內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot+feign+Hystrix整合(親測有效)

    springboot+feign+Hystrix整合(親測有效)

    本文主要介紹了springboot+feign+Hystrix整合,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-11-11
  • JVM調(diào)優(yōu)參數(shù)的設(shè)置

    JVM調(diào)優(yōu)參數(shù)的設(shè)置

    Java虛擬機的調(diào)優(yōu)是一個復雜而關(guān)鍵的任務(wù),可以通過多種參數(shù)來實現(xiàn),本文就來介紹一下JVM調(diào)優(yōu)參數(shù)的設(shè)置,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • SpringBoot+MinIO實現(xiàn)對象存儲的示例詳解

    SpringBoot+MinIO實現(xiàn)對象存儲的示例詳解

    MinIO?是一個基于Apache?License?v2.0開源協(xié)議的對象存儲服務(wù),它是一個非常輕量的服務(wù),可以很簡單的和其他應(yīng)用的結(jié)合,所以下面我們就來看看SpringBoot如何整合MinIO實現(xiàn)對象存儲吧
    2023-10-10
  • 三步輕松實現(xiàn)Java的SM2前端加密后端解密

    三步輕松實現(xiàn)Java的SM2前端加密后端解密

    SM2算法和RSA算法都是公鑰密碼算法,SM2算法是一種更先進安全的算法,在我們國家商用密碼體系中被用來替換RSA算法,這篇文章主要給大家介紹了關(guān)于如何通過三步輕松實現(xiàn)Java的SM2前端加密后端解密的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Java面向?qū)ο笾b類的用途與實際使用

    Java面向?qū)ο笾b類的用途與實際使用

    所謂包裝類,就是能夠直接將簡單類型的變量表示為一個類,在執(zhí)行變量類型的相互轉(zhuǎn)換時,我們會大量使用這些包裝類,本文我們來深入探索一下Java包裝類的相關(guān)內(nèi)容,需要的朋友可以參考下
    2022-03-03
  • java中的空指針異常情況以及解決方案

    java中的空指針異常情況以及解決方案

    這篇文章主要介紹了java中的空指針異常情況以及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java中concat()方法的使用說明

    java中concat()方法的使用說明

    這篇文章主要介紹了java中concat()方法的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • java集合類arraylist循環(huán)中刪除特定元素的方法

    java集合類arraylist循環(huán)中刪除特定元素的方法

    下面小編就為大家?guī)硪黄狫ava集合類ArrayList循環(huán)中刪除特定元素的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • 探討Java中最常見的十道面試題(超經(jīng)典)

    探討Java中最常見的十道面試題(超經(jīng)典)

    本篇文章是對Java中最常見的十道面試題進行了詳細的分析介紹,需要的朋友參考下
    2013-07-07
  • 一定要讓你搞懂Java位運算符

    一定要讓你搞懂Java位運算符

    這篇文章主要為大家介紹了Java位運算符,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01

最新評論