关于两个BeanUtils.copyProperties()的用法及区别

目录
  • 两个BeanUtils.copyProperties()用法及区别
  • 使用Beanutils.copyProperties()遇到的问题

两个BeanUtils.copyProperties()用法及区别

这两个类在不同的包下面,而这两个类的copyProperties()方法里面传递的参数赋值是相反的。

例如:

a,b为对象

BeanUtils.copyProperties(a, b);

public static void copyProperties(Object source, Object target) throws BeansException {//source 源文件,target 目标文件
        copyProperties(source, target, (Class)null, (String[])null);
    }

BeanUtils是org.apache.commons.beanutils.BeanUtils,b拷贝到a

 public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {//destination,目标文件,original原始的,源文件
        BeanUtilsBean.getInstance().copyProperties(dest, orig);
    }

这两个不要搞混了!

使用Beanutils.copyProperties()遇到的问题

BeanUtils.copyProperties VS PropertyUtils.copyProperties

两者最大的区别是:

BeanUtils.copyProperties会进行类型转换,而PropertyUtils.copyProperties不会。

既然进行了类型转换,那BeanUtils.copyProperties的速度比不上PropertyUtils.copyProperties。

因此,PropertyUtils.copyProperties应用的范围稍为窄一点,它只对名字和类型都一样的属性进行copy,如果名字一样但类型不一样,它会报错

使用BeanUtils有几个要注意的地方:

1.对于类型为Boolean/Short/Integer/Float/Double的属性,它会转换为0:

public class User {
 private Integer intVal;
 private Double doubleVal;
 private Short shortVal;
 private Long longVal;
 private Float floatVal;
 private Byte byteVal;
 private Boolean booleanVal;
} 

User src = new User();
User dest = new User();
BeanUtils.copyProperties(dest, src);
System.out.println(src);
System.out.println(dest); 

//输出
User [intVal=null, doubleVal=null, shortVal=null, longVal=null, floatVal=null, byteVal=null, booleanVal=null]
User [intVal=0, doubleVal=0.0, shortVal=0, longVal=0, floatVal=0.0, byteVal=0, booleanVal=false] 

在stackoverflow上有人解释说是因为这几个类型都有对应的基本类型,在进行类型转换时,有可能遇到类似Integer -> int的转换,此时显然不能对int类型的属性赋值为null,因此统一转换为0。

如何让它不要转为0呢?可以这样:

import org.apache.commons.beanutils.converters.IntegerConverter;  
IntegerConverter converter = new IntegerConverter(null); //默认为null,而不是0 
BeanUtilsBean beanUtilsBean = new BeanUtilsBean(); 
beanUtilsBean.getConvertUtils().register(converter, Integer.class); 

2.对于java.util.Date/BigDecimal/java.sql.Date/java.sql.Timestamp/java.sql.Time这几个类,如果值为null,则在copy时会抛异常,需要使用对应的Conveter:

public class User2 {
 private java.util.Date javaUtilDateVal;
 private java.sql.Date javaSqlDateVal;
 private java.sql.Timestamp javaSqlTimeStampVal;
 private BigDecimal bigDecimalVal;
 private java.sql.Time javaSqlTime;
} 

User2 src = new User2();
User2 dest = new User2();
BeanUtilsBean beanUtilsBean = new BeanUtilsBean(); 

//如果没有下面几行,则在转换null时会抛异常,例如:org.apache.commons.beanutils.ConversionException: No value specified for 'BigDecimal'
//在org.apache.commons.beanutils.converters这个包下面有很多的Converter,可以按需要使用
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.BigDecimalConverter(null), BigDecimal.class);
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.DateConverter(null), java.util.Date.class); 

beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.SqlTimestampConverter(null), java.sql.Timestamp.class);
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.sql.Date.class);
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.SqlTimeConverter(null), java.sql.Time.class); 

beanUtilsBean.copyProperties(dest, src);
System.out.println(src);
System.out.println(dest);

使用BeanUtils还会经常碰到这样变态的需求:

  • 假设是从A复制到B:
  • 需求1:如果B中某字段有值(不为null),则该字段不复制;也就是B中该字段没值时,才进行复制,适合于对B进行补充值的情况。
  • 需求2:如果A中某字段没值(为null),则该字段不复制,也就是不要把null复制到B当中。
  • 对于需求1,可以这样:
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.PropertyUtils;
public class CopyWhenNullBeanUtilsBean extends BeanUtilsBean{ 

 @Override
 public void copyProperty(Object bean, String name, Object value)
   throws IllegalAccessException, InvocationTargetException {
  try {
   Object destValue = PropertyUtils.getSimpleProperty(bean, name);
   if (destValue == null) {
    super.copyProperty(bean, name, value);
   }
  } catch (NoSuchMethodException e) {
   throw new RuntimeException(e);
  }
 }
}

对于需求2,可以这样:

import org.apache.commons.beanutils.BeanUtilsBean;
public class CopyFromNotNullBeanUtilsBean extends BeanUtilsBean { 

 @Override
 public void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException {
  if (value == null) {
   return;
  }
  super.copyProperty(bean, name, value);
 }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • BeanUtils.copyProperties()参数的赋值顺序说明

    目录 BeanUtils.copyProperties()参数的赋值顺序 BeanUtils.copyProperties初体验,及其参数含义解释 用处 案例: 创建一个源类:source 创建一个目标target源类 创建测试类test ignoreProperties参数 案例 案例测试 BeanUtils.copyProperties()参数的赋值顺序 BeanUtils.copyProperties(x,y)有两个不同的jar包,引入不同的包,赋值的顺序不一样. 分别是: org.spr

  • 基于Beanutils.copyProperties()的用法及重写提高效率

    目录 Beanutils.copyProperties()用法及重写提高效率 一.简介 二.用法 三.重写 原理 BeanUtils.copyProperties 使用注意 示例演示 开始进行示例 最后强调 Beanutils.copyProperties()用法及重写提高效率 特别说明本文介绍的是Spring(import org.springframework.beans.BeanUtils)中的BeanUtils.copyProperties(A,B)方法.是将A中的值赋给B.apache

  • BeanUtils.copyProperties使用总结以及注意事项说明

    目录 1.前言 2.一般使用 3.拷贝属性时忽略空值 4.使用注意事项(1) 5.使用注意事项(2) 6.使用注意事项(3) 1.前言 开发过程中,讲一个对象的属性和值赋值到另一个对象上,大量使用了get.set方法,看着很臃肿,思考下肯定不只有我有这种想法,所以技术上肯定有方法能解决这个问题,所以查阅了一些资料发现了BeanUtils.copyProperties这个方法以下是这次所有的总结以及使用时的注意事项. 使用org.springframework.beans.BeanUtils.co

  • BeanUtils.copyProperties()拷贝id属性失败的原因及解决

    目录 BeanUtils.copyProperties()拷贝id属性失败 部分代码如下 解决方法 BeanUtils.copyProperties 出错 BeanUtils.copyProperties()拷贝id属性失败 po类中id有值,但是使用BeanUtils.copyProperties()拷贝出的vo类id属性为null,检查后发现是因为po继承的父类声明了一个泛型. 部分代码如下 public abstract class AbstractEntity<ID extends Se

  • 聊聊BeanUtils.copyProperties和clone()方法的区别

    目录 首先,BeanUtils有两种: 效率: 需要在pom文件中引入这个包 在pom文件里面引入所需要的包 新建一个实体类StudentEntity实现Cloneable接口 测试方法 最近撸代码的时候发现有人将一个对象的值赋给另一个对象的时候,并没有使用常规的set/get方法去给对象赋值,而是采用BeanUtils.copyProperties(A,B)这个方法去赋值,但是有的还是有局限性,比如Date类型的值无法赋值,只能赋值为null,所以我大致百度了一下,作为记录. 首先,BeanU

  • BeanUtils.copyProperties复制不生效的解决

    目录 前言 问题的排查 问题的扩展 前言 呵呵 前端时间使用 BeanUtils.copyProperties 的时候碰到了一个这样的问题 我有两个实体, 有同样的属性, 一个有给定的属性的 getter, 另外一个有 给定的属性的 setter, 但是 我使用 BeanUtils.copyProperties 的时候 把来源对象的这个属性 复制不到 目标对象上面 然后 当时也跟踪了一下代码, 然后 这里整理一下 改代码片段吧 然后在调试的过程中 也发现了一些其他的问题, 呵呵 算是额外的了解吧

  • 关于两个BeanUtils.copyProperties()的用法及区别

    目录 两个BeanUtils.copyProperties()用法及区别 使用Beanutils.copyProperties()遇到的问题 两个BeanUtils.copyProperties()用法及区别 这两个类在不同的包下面,而这两个类的copyProperties()方法里面传递的参数赋值是相反的. 例如: a,b为对象 BeanUtils.copyProperties(a, b); public static void copyProperties(Object source, Ob

  • java Beanutils.copyProperties( )用法详解

    这是一篇开发自辩甩锅稿~~~~ 昨天测试小姐姐将我的一个bug单重开了,emmmm....内心OS:就调整下对象某个属性类型这么简单的操作,我怎么可能会出错呢,一定不是我的锅!!but再怎么抗拒,bug还是要改的,毕竟晚上就要发版本了~~ 老老实实将我前天改的部分跟了一遍,恩,完美,没有任何的缺失~~but本应success的测试数据,接口返还的结果确实是false来着,那还是老老实实debug吧. 一步步跟下来,恩,多么顺畅,就说一定不是我的锅~~诶?不对不对,这里的ID值,为啥是null?传

  • 解决BeanUtils.copyProperties不支持复制集合的问题

    工作中,经常使用Spring的工具类BeanUtils.copyProperties对bean属性进行复制,这里的复制属于浅复制.且不能复制集合和数组.本文会对该工具进行一些测试. 文末会提出复制集合属性的解决方案 准备工作:准备测试需要的类 @Data public class Class { private People[] member; private People teacher; private List<People> student; } @Data @NoArgsConstr

  • 解决BeanUtils.copyProperties之大坑

    目录 BeanUtils.copyProperties大坑 BeanUtils.copyProperties() 用法及区别 因为两个类引入了两个不同的BeanUtils类 例如 BeanUtils.copyProperties大坑 两个不同的包(springframework , apache)中有一个相同名字的类,相同的方法,方法的作用相同,参数个数相同.就是参数位置不同,是相反的.? import org.springframework.beans.BeanUtils; import or

  • 如何使用BeanUtils.copyProperties进行对象之间的属性赋值

    1.使用org.springframework.beans.BeanUtils.copyProperties方法进行对象之间属性的赋值,避免通过get.set方法一个一个属性的赋值 /** * 对象属性拷贝 <br> * 将源对象的属性拷贝到目标对象 * * @param source 源对象 * @param target 目标对象 */ public static void copyProperties(Object source, Object target) { try { BeanU

  • BeanUtils.copyProperties在拷贝属性时忽略空值的操作

    BeanUtils.copyProperties忽略空值 使用spring开发的人,对这行代码肯定不陌生,常用于DTO.VO.PO之间的复制. /** * 全属性copy对象 * **/ BeanUtils.copyProperties(Object source, Object target) 但这行代码会将所有的属性都进行copy,有的时候我们想要个别属性不进行复制(比如:null值属性),这时就需要用到另一个方法: /** * 忽略某些属性copy对象 * **/ BeanUtils.co

  • 解决Beanutils.copyproperties实体类对象不一致的问题

    今天给大家分析一个解决Beanutils.copyproperties实体类对象名不一致的解决方法,一般我们在两个对象拷贝的问题上,我个人用的比较多的就是Beanutils.copyproperties,字段名如果不一致的话就去实体类中使用重载,把当前实体类的对象赋值给另外一个对象,也有用到set(),当然这些也都能解决Beanutils.copyproperties实体类属性不一致的问题,不过今天要给大家分享的是,不用set()和实体类的重构,使用类的反射机制去完成! 话不多说直接开始: 我是

  • 解决BeanUtils.copyProperties无法成功封装的问题

    BeanUtils.copyProperties无法封装 使用 BeanUtils.copyProperties(user, memeber); 两个类中字段一样,但个别字段无法封装. 期初以为或许是字段的属性不同,仔细检查过还是一样,最后发现,是get.set方法名不同的原因. 如下 user里面有个字段为abc,他的get方法名为getABC(); member里面同样的字段abc,他的get方法名为getAbc(); 最后导致abc的字段无法成功封装 BeanUtils.copyPrope

随机推荐