怎么做网盘网站,外包公司官网,信息门户网站制作,seo快排公司哪家好BeanUtils.copyProperties用法 简介使用场景传统方式赋值#xff1a;反射方式赋值面向对象BeanUtils但是有几点我们需要注意#xff1a; BeanUtils.copyProperties VS PropertyUtils.copyProperties两者最大的区别是#xff1a; 使用BeanUtils有几个要注意的地方#xff1a… BeanUtils.copyProperties用法 简介使用场景传统方式赋值反射方式赋值面向对象BeanUtils但是有几点我们需要注意 BeanUtils.copyProperties VS PropertyUtils.copyProperties两者最大的区别是 使用BeanUtils有几个要注意的地方 关键字BeanUtils.copyProperties、Java、属性拷贝、属性赋值、对象拷贝、对象属性赋值、复制对象 简介
BeanUtils提供对Java反射和自省API的包装。其主要目的是利用反射机制对JavaBean的属性进行处理。
使用场景
当我们如果有两个具有很多相同属性的JavaBeanUser1Bean,User2Bean我想吧User1Bean的属性全部赋值给User2Bean。
传统方式赋值
user2Bean.setName(user1Bean.getName()); …3个属性set,get 3次我要是10000个属性呢你会写10000次吗 所以改进赋值方式
反射方式赋值
通过反射获取到user2Bean对象的属性set方法user1Bean对象的get方法循环user2Bean对象的属性执行user2Bean.setProperty(user1Bean.getProtpry()); 详细代码说实话我不想写你想写吗对你不想。
//此处省略10000行面向对象BeanUtils
依照能用别人的就不自己写原则面向对象我们开始使用BeanUtils。
//准备两个bean
UserBean user1 new UserBean();
user1.setName(张三);
user1.setAge(38);
user1.setSex(看着来吧);
UserBean user2 new UserBean();
//使用
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.copyProperties(user2, user1);
//好了代码写完了...
//没错写完了你后边想怎么蹂躏 user2 都行了 [斜眼笑]你可能在疑问我为什么不用BeanUtils.copyProperties(user2, user1); 是我在提醒你考虑bean中存在BigDecimal、Date属性的情况直接copyProperties会出错。 详情请看【使用BeanUtils有几个要注意的地方】 List item
但是有几点我们需要注意
targetObj被赋值的目标对象 srcObj 具备数据的对象 BeanUtils.copyProperties(targetObj, srcObj);
srcObj中的存在的属性targetObj中一定要有但是targetObj中可以有多余的属性targetObj中与srcObj中相同的属性都会被替换不管是否有值targetObj、 srcObj中的属性要名字相同才能被赋值不然的话需要手动赋值Spring的BeanUtils的CopyProperties方法需要对应的属性有getter和setter方法如果存在属性完全相同的内部类但是不是同一个内部类即分别属于各自的内部类则spring会认为属性不同不会copyspring和apache的copy属性的方法源和目的参数的位置正好相反所以导包和调用的时候都要注意一下。
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 [intValnull, doubleValnull, shortValnull, longValnull, floatValnull, byteValnull, booleanValnull]
User [intVal0, doubleVal0.0, shortVal0, longVal0, floatVal0.0, byteVal0, booleanValfalse]在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{ Overridepublic 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 { Overridepublic void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException { if (value null) { return; } super.copyProperty(bean, name, value); }
}参考: https://blog.csdn.net/dfshsdr/article/details/90513676 https://www.jb51.net/article/186524.htm
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); }
} 本文参考了 全站工程师https://javaforall.cn