BeanUtils.copyProperties()怎么使用

发布时间:2022-06-15 11:49:36 作者:iii
来源:亿速云 阅读:188

这篇文章主要介绍了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还会经常碰到这样变态的需求:

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

关于“BeanUtils.copyProperties()怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“BeanUtils.copyProperties()怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. hive的典型应用场景
  2. checkpoint system management

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

beanutils.copyproperties()

上一篇:Python Web App如何开发Dockerfiles

下一篇:如何对mongodb存储类JSON数据文档统计分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》