- 浏览: 778718 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
萨琳娜啊:
Java读源码之Netty深入剖析网盘地址:https://p ...
Netty源码学习-FileRegion -
飞天奔月:
写得有趣 ^_^
那一年你定义了一个接口 -
GoldRoger:
第二个方法很好
java-判断一个自然数是否是某个数的平方。当然不能使用开方运算 -
bylijinnan:
<script>alert("close ...
自己动手实现Java Validation -
paul920531:
39行有个bug:"int j=new Random ...
java-蓄水池抽样-要求从N个元素中随机的抽取k个元素,其中N无法确定
BeanUtils.copyProperties VS PropertyUtils.copyProperties
两者最大的区别是:
BeanUtils.copyProperties会进行类型转换,而PropertyUtils.copyProperties不会。
既然进行了类型转换,那BeanUtils.copyProperties的速度比不上PropertyUtils.copyProperties。
因此,PropertyUtils.copyProperties应用的范围稍为窄一点,它只对名字和类型都一样的属性进行copy,如果名字一样但类型不一样,它会报错。
关于两者区别,http://caoyaojun1988-163-com.iteye.com/blog/1871316这篇文章总结了很多。
下面主要谈谈BeanUtils。
使用BeanUtils有几个要注意的地方:
1.对于类型为Boolean/Short/Integer/Float/Double的属性,它会转换为0:
在stackoverflow上有人解释说是因为这几个类型都有对应的基本类型,在进行类型转换时,有可能遇到类似Integer -> int的转换,此时显然不能对int类型的属性赋值为null,因此统一转换为0。
如何让它不要转为0呢?可以这样:
2.对于java.util.Date/BigDecimal/java.sql.Date/java.sql.Timestamp/java.sql.Time这几个类,如果值为null,则在copy时会抛异常,需要使用对应的Conveter:
使用BeanUtils还会经常碰到这样变态的需求:
假设是从A复制到B:
需求1:如果B中某字段有值(不为null),则该字段不复制;也就是B中该字段没值时,才进行复制,适合于对B进行补充值的情况。
需求2:如果A中某字段没值(为null),则该字段不复制,也就是不要把null复制到B当中。
对于需求1,可以这样:
对于需求2,可以这样:
两者最大的区别是:
BeanUtils.copyProperties会进行类型转换,而PropertyUtils.copyProperties不会。
既然进行了类型转换,那BeanUtils.copyProperties的速度比不上PropertyUtils.copyProperties。
因此,PropertyUtils.copyProperties应用的范围稍为窄一点,它只对名字和类型都一样的属性进行copy,如果名字一样但类型不一样,它会报错。
关于两者区别,http://caoyaojun1988-163-com.iteye.com/blog/1871316这篇文章总结了很多。
下面主要谈谈BeanUtils。
使用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); } }
评论
1 楼
byyuyi
2015-07-22
南总,求解释~~
import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.math.BigDecimal; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; import org.springframework.beans.FatalBeanException; import org.springframework.util.Assert; import com.smy.framework.core.util.MathUtil; public abstract class EnhanceBeanUtil extends BeanUtils { public static void copyProperties(Object source, Object target) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) if (targetPd.getWriteMethod() != null) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if ((sourcePd == null) || (sourcePd.getReadMethod() == null)) continue; try { Method readMethod = sourcePd.getReadMethod(); if (!(Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()))) { readMethod.setAccessible(true); } Object srcValue = readMethod.invoke(source, new Object[0]); if (srcValue != null) { Object value = srcValue; if ((sourcePd.getPropertyType().isAssignableFrom(Double.class)) && (targetPd.getPropertyType().isAssignableFrom(BigDecimal.class))) { value = new BigDecimal(((Double) srcValue).doubleValue()); } if ((sourcePd.getPropertyType().isAssignableFrom(BigDecimal.class)) && (targetPd.getPropertyType().isAssignableFrom(Double.class))) { value = Double.valueOf(((BigDecimal) srcValue).doubleValue()); } if ((sourcePd.getPropertyType().isAssignableFrom(Long.class)) && (targetPd.getPropertyType().isAssignableFrom(BigDecimal.class))) { value = new BigDecimal(((Long) srcValue).longValue()); } if ((sourcePd.getPropertyType().isAssignableFrom(BigDecimal.class)) && (targetPd.getPropertyType().isAssignableFrom(Long.class))) { value = Long.valueOf(((BigDecimal) srcValue).longValue()); } if ((sourcePd.getPropertyType().isAssignableFrom(String.class)) && (targetPd.getPropertyType().isAssignableFrom(BigDecimal.class))) { String srcValueStr = (String) srcValue; if (srcValueStr.matches("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){2})$")) { value = new BigDecimal((String) srcValue); } } if ((sourcePd.getPropertyType().isAssignableFrom(BigDecimal.class)) && (targetPd.getPropertyType().isAssignableFrom(String.class))) { value = MathUtil.roundTwoScale((BigDecimal) srcValue).toString(); } if (value != null) { Method writeMethod = targetPd.getWriteMethod(); if (!(Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()))) { writeMethod.setAccessible(true); } writeMethod.invoke(target, new Object[] { value }); } } } catch (Throwable ex) { throw new FatalBeanException( "Could not copy properties from source to target", ex); } } } }
发表评论
-
自己动手实现Java Validation
2015-09-18 20:37 10084参数检查用得最多的是J ... -
Haproxy+Keepalived高可用双机单活
2015-01-06 17:37 6590我们的应用MyApp不支持集群,但要求双机单活(两台机器:ma ... -
返回null还是empty
2014-05-16 15:35 100第一个问题,函数是应当返回null还是长度为0的数组(或集合) ... -
返回null还是empty
2014-05-16 15:34 2276第一个问题,函数是应当返回null还是长度为0的数组(或集合) ... -
返回null还是empty
2014-05-16 15:34 152第一个问题,函数是应当返回null还是长度为0的数组(或集合) ... -
返回null还是empty
2014-05-16 15:34 114第一个问题,函数是应当返回null还是长度为0的数组(或集合) ... -
返回null还是empty
2014-05-16 15:33 100第一个问题,函数是应当返回null还是长度为0的数组(或集合) ... -
Spring源码学习-JdbcTemplate queryForObject
2014-05-09 19:45 2989JdbcTemplate中有两个可能会混淆的queryForO ... -
Spring源码学习-JdbcTemplate batchUpdate批量操作
2014-05-07 16:21 18748Spring JdbcTemplate的batch操作最后还是 ... -
Spring源码学习-PropertyPlaceholderHelper
2014-04-25 18:47 2595今天在看Spring 3.0.0.RELEASE的源码,发现P ... -
J2EE设计模式-Intercepting Filter
2013-11-27 16:56 1502Intercepting Filter类似于职责链模式 有两种 ... -
CAS实现单点登录(SSO)
2013-07-29 18:08 1443参考以下两篇文章,对原作者表示感谢: http://blog. ... -
《重构,改善现有代码的设计》第八章 Duplicate Observed Data
2012-12-04 20:34 1469import java.awt.Color; impor ... -
org.apache.tools.zip实现文件的压缩和解压,支持中文
2012-08-08 19:32 5059刚开始用java.util.Zip,发 ...
相关推荐
一个BeanUtils.copyProperties的小型快速替代。 起因 由于BeanUtils(Spring或Apache Commons)的copyProperties实现是利用反射实现的,它在大量调用时具有比较严重的性能问题。 BeanMapper通过javassist类库实现在...
主要介绍了java Beanutils.copyProperties( )用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了使用BeanUtils.copyProperties进行对象之间的属性赋值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
org.apache.commons.beanutils.jar
commons-beanutils-1.8.0.jar beanutils.jar beanutils.jar工具包
关于JSF的commons-beanutils.jar.zip
json相关jar包(不使用springmvc开发时)。其中包含(commons-beanutils.jar、commons-collections-3.1.jar、commons-lang-2.6.jar、commons-logging.jar、ezmorph-1.0.6.jar、json-lib-2.2.3-jdk15.jar)
commons-beanutils.jar commons-beanutils.jar
Apache提供的这个beanutils包极大方便了javabean的 操作。包含了最新的commons-beanutils-1.9.3.jar和api文档,以及其依赖的commons-logging-1.2.jar包
beanUtils 方便访问javaBean 附带支持框架 logging jar包,Apache提供的这个beanutils包极大方便了javabean的 操作。包含了最新的commons-beanutils-1.9.3.jar,以及其依赖的commons-logging-1.2.jar包
这是commons常用到的包,包括:commons-pool.jar,commons-beanutils.jar等共八个commons.jar包
commons-beanutils.jar 开发时所需要的jar包
JSF开发所必需包:花了很长时间才收集好,很费时,现已收集好,何不分享给大家,让大家节省...commons-beanutils.jar commons-collections.jar commons-digester.jar jsf-api.jar jsf-impl.jar jstl.jar standard.jar
此为BeanUtils的实例。其中apache的包有一个小的BUG已在其中说明。
commons-beanutils.jar.zip(173 k)commons-beanutils.jar.zip(173 k)commons-beanutils.jar.zip(173 k) jdbc 专用