一、起步

  引入依赖:

<!-- 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的BeanUtilshttps://www.cnblogs.com/dongfangshenhua/p/7099970.html

    如果引入了hutool依赖,推荐hutool的BeanUtils!

【commons】Bean工具类——commons-beanutils之BeanUtils的更多相关文章

  1. bean工具类

    package com.zq.utils; import java.lang.reflect.Method;import java.util.Arrays;import java.util.Colle ...

  2. Spring获取bean工具类,可用于在线程里面获取bean

    Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...

  3. Apache Commons 常用工具类整理

    其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧 怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包 public class ApacheCommonsT ...

  4. 获取spring上下文的bean 工具类

    有些场景我们不属于controller,service,dao,但是我们需要从spring中得到spring容器里面的bean.这时候我们需要一个类继承 ApplicationContextAware ...

  5. org.apache.commons.httpclient工具类

    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpcl ...

  6. org.apache.commons.httpclient工具类(封装的HttpUtil)

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...

  7. Resultset转Bean工具类

    package org.pandas.webIdp.webOP.help; import java.lang.reflect.Field; import java.lang.reflect.Metho ...

  8. linkin大话数据结构--apache commons工具类

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动. 一.Commons BeanUtils 说明:针对Bean的一个工具集.由于Bean往往是有一堆ge ...

  9. java 常用Bean 转换工具类

    package com.hnf.framework.utils; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.data ...

随机推荐

  1. C#秒转换小时

    #region 秒转换小时 SecondToHour /// <summary> /// 秒转换小时 /// </summary> /// <param name=&qu ...

  2. mac 打开整个系统的隐藏文件

    打开:defaults write com.apple.finder AppleShowAllFiles -bool true 关闭:defaults write com.apple.finder A ...

  3. Office 365 Pass-through身份验证及Seamless Single Sign-On

    Hello 小伙伴们, 这篇文章将视点聚焦在传递身份验证(Pass-through Authentication)上,将分享如何安装,配置和测试Azure Active Directory(Azure ...

  4. 封装简单的mvc框架

    MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式. MVC把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller). PH ...

  5. css实现梯形

    使用伪元素before和after分别在矩形元素前后加三角形或者直接设置border 使用3d旋转矩形,使之看起来像矩形 <html> <head> <meta char ...

  6. VB ASP 使用 now() 时默认格式调整方法

    修改注册表 [HKEY_USERS\.DEFAULT\Control Panel\International] "sShortDate"="yyyy-M-d" ...

  7. December 04th 2016 Week 50th Sunday

    Learn wisdom by the follies of others. 前车之鉴,后人之师. Maybe my personal state is that others can learn w ...

  8. [BZOJ 4031][LOJ 2122][HEOI 2015] 小Z的房间

    [BZOJ 4031][LOJ 2122][HEOI 2015] 小Z的房间 题意 给定一个 \(n\times m\) 的矩阵, 一些格子是障碍, 相邻的格子(四联通)之间可以连边, 求把非障碍的格 ...

  9. 18年10月31日 NOIP模拟赛

    T1.exercise 题解 数据很小直接模拟 代码 #include<iostream> #include<cstdio> #include<cmath> #in ...

  10. Python的multiprocessing,Queue,Process

    在多线程multiprocessing模块中,有两个类,Queue(队列)和Process(进程): 在Queue.py中也有一个Queue类,这两个Queue的区别? from multiproce ...