一、起步

  引入依赖:

<!-- 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相关推荐

  1. 常用工具类 (一) : Apache Commons 常用工具类整理 (全)

    文章目录 一.commons-lang3 引入依赖 StringUtils 字符串工具类 StringEscapeUtils 转义字符串工具类 NumberUtils 数字工具类 ArrayUtils ...

  2. apache commons常用工具类

    1.有些情况下,Arrays满足不到你对数组的操作?不要紧,ArrayUtils帮你 ArrayUtils public class TestMain {  public static void ma ...

  3. org.apache.commons.commons-lang3工具类(一)

    https://blog.csdn.net/qq_31289187/article/details/85343068 本文只是简单的介绍了commons-lang3中的SystemUtils.Stri ...

  4. Apache Commons StringUtils工具类深入整理

    字符串是在程序开发中最常见的,Apache Commons开源项目在org.apache.commons.lang3包下提供了StringUtils工具类,该类相当于是对jdk自带的String类的增 ...

  5. 获取Spring容器管理的Bean工具类

    很多时候我们在一些不受spring管理的类中需要用到spring管理的Bean,那么这个时候可以使用如下工具类从spring容器中获取相关的Bean实例. @Component public clas ...

  6. springboot通过各种不同类型参数获取容器中的bean工具类

    一般依赖通过@Autowired,@Resource注入就可以了,也可以通过将属性设置为private final类型,再使用lombok的@AllArgsConstructor也可以注入.使用这个工 ...

  7. springboot中动态获取bean工具类

    在springmvc或者springboot的项目中,经常遇到需要获取其他的bean的类,从而使用该bean内部的一些方法,以供业务调用,我们知道,在spring项目中,某个被spring管理的类要调 ...

  8. spring的beanutils工具类_基于spring-beans实现工具类BeanUtils基于Class实例化注入对象及查找方法、复制属性等操作...

    一.前言 基于spring-beans(4.1.4)的工具类org.springframework.beans.BeanUtils对注入spring对象按照Class实例化instantiateCla ...

  9. 二、java项目常用工具类之beancopy,bean和map转换工具类

    项目环境: jdk1.8+spring4.3.12 一.问题描述及试用场景: 在项目规范中,要求类名以DO为尾的类作为数据库层实体bean,类名以MO为尾的类作为系统传输层实体bean,类名以VO为尾 ...

最新文章

  1. 绘画软件优动漫PAINT系统要求
  2. android 异步线程的使用
  3. Excel学习使用教程
  4. 树叶节点_茶,一片树叶的旅程
  5. OpenShift 之 Quarkus(3)用独立的Prometheus监控Quarkus应用
  6. # 2017-2018-1 20155224 《信息安全系系统设计基础》第四周MyOD
  7. Web前端开发视频教程
  8. Python数据库sqlite3详解
  9. 【cmd】cmd怎么到达指定的文件夹
  10. cluster_acc计算
  11. 【echarts】在柱状图上方显示数值
  12. docker quick start
  13. Homebrew - 安装与使用
  14. matlab RGB到HSI的彩色转换及实现
  15. 如何将QQLive和QQ2010和虚拟光驱运行在ubuntu上
  16. 全球5G手机最新排名!
  17. python中调用API的几种方式
  18. Super4PCS文章阅读笔记
  19. Guessnumber
  20. java 2048游戏_JAVA2048游戏 本课程设计是基于java语言的2048小游戏设计 联合开发网 - pudn.com...

热门文章

  1. 新京报:“AI换脸”被滥用 科技莫成“黑产”帮凶
  2. 交换机是如何对数据包打标签去标签的_如何使用PC抓带vlan标签的数据包?王海军老师告诉你...
  3. openwrt dhcp不分配_【装维技巧】DHCP工作原理详解(上)
  4. syslog 向内存中缓存_漫谈缓存(Cache)、大规模芯片系统的存储层次结构优化以及开源仿真工具ZSim...
  5. matlab 创建批量文件夹_Matlab开发Web App服务器(一)
  6. .net mvc actionresult 返回字符串_字符串游戏之无效的身份证
  7. 006_Gson定制型适配器
  8. 012_JDBC模板
  9. bootanimation文件名_Android开机logo制作
  10. 自定义控件:侧滑面板