日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【commons】Bean工具类——commons-beanutils之BeanUtils

發布時間:2025/4/16 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【commons】Bean工具类——commons-beanutils之BeanUtils 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、起步

  引入依賴:

<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> <dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.8.3</version> </dependency>

  當然,一般可能項目中不會需要單獨引入這個依賴,可能其他依賴自動引入了此包,請先檢查,避免重復引入!

二、開始

  文檔:http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.3/apidocs/org/apache/commons/beanutils/BeanUtils.html

  參考隨筆:https://www.cnblogs.com/vmax-tam/p/4159985.html

  1.常用方法   

copyProperties(Object dest, Object orig) Copy property values from the origin bean to the destination bean for all cases where the property names are the same.copyProperty(Object bean, String name, Object value) Copy the specified property value to the specified destination bean, performing any type conversion that is required. populate(Object bean, Map<String,? extends Object> properties) Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.

  2.代碼演示

    不再(tou)贅述(lan),查看網友示例:https://www.cnblogs.com/vmax-tam/p/4159985.html

  3.注意點(引用自網友)

  (淺拷貝)  

  關于bean復制,如果屬性較少,建議直接寫個方法完成get/set即可。如果屬性較多,可以自己采用反射實現一個滿足自己需要的工具類,或者使用spring的那個beanutils類,不建議使用commons-beanutils包中的那個BeanUtils類,剛看了下,這個類對于內部靜態類的對象復制也會出現問題,檢驗太復雜了,常會出現一些詭異的問題。畢竟我們bean復制一般就是簡單的屬性copy而已。

而且,由于這些BeanUtils類都是采用反射機制實現的,對程序的效率也會有影響。因此,慎用BeanUtils.copyProperties!!!

  4.對比與拓展

    如果有Date等類型,請參考spring的BeanUtils:https://www.cnblogs.com/dongfangshenhua/p/7099970.html

    如果引入了hutool依賴,推薦hutool的BeanUtils!

轉載于:https://www.cnblogs.com/jiangbei/p/8417568.html

總結

以上是生活随笔為你收集整理的【commons】Bean工具类——commons-beanutils之BeanUtils的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。