java 对象之间属性值复制
2012-07-28 13:12
721 查看
java对象之间属性值复制在许多开源框架中也有实现,在这里介绍下自己的实现、apache commons-beanutils、Spring三种方式实现。
1 自己实现。采用反射,通过源对象getter 方法获取属性值,并通过目标对象的setter方法设置到目标对象中去。没有考虑太多性能,如果需要批量或者经常性执行该操作,建议不采用此种实现。下面给出源码:
2 利用apache commons-beanutils的开源实现。
BeanUtils.copyProperties(dst, src)。方法能够将源对象和目标对象中相同名称的属性值复制过去。注意的是参数前面的是目标对象,后面是源对象。使用该方法需要注意:不能将入口方法与源对象、目标对象之一放在同一源文件之内,否者将没有任何效果。
PropertyUtils.copyProperties(dst, src)。功能与BeanUtils.copyProperties类似,只是在同属性名称的类型参数之间可以执行转换操作。
3 利用Spring实现属性之间的复制。spring内部自有实现方法,如果我们需要在外面采用spring的托管复制,需要修改spring的源码,将spring中的org.springframework.beans.CachedIntrospectionResults类的forClass、getPropertyDescriptor、getBeanInfo改为可见的后重新打包。然后将Spring中关于复制的代码提取出来,最后修改成代码如下:
1 自己实现。采用反射,通过源对象getter 方法获取属性值,并通过目标对象的setter方法设置到目标对象中去。没有考虑太多性能,如果需要批量或者经常性执行该操作,建议不采用此种实现。下面给出源码:
/** * 利用反射实现对象之间属性复制 * @param from * @param to */ public static void copyProperties(Object from, Object to) throws Exception { copyPropertiesExclude(from, to, null); } /** * 复制对象属性 * @param from * @param to * @param excludsArray 排除属性列表 * @throws Exception */ @SuppressWarnings("unchecked") public static void copyPropertiesExclude(Object from, Object to, String[] excludsArray) throws Exception { List<String> excludesList = null; if(excludsArray != null && excludsArray.length > 0) { excludesList = Arrays.asList(excludsArray); //构造列表对象 } Method[] fromMethods = from.getClass().getDeclaredMethods(); Method[] toMethods = to.getClass().getDeclaredMethods(); Method fromMethod = null, toMethod = null; String fromMethodName = null, toMethodName = null; for (int i = 0; i < fromMethods.length; i++) { fromMethod = fromMethods[i]; fromMethodName = fromMethod.getName(); if (!fromMethodName.contains("get")) continue; //排除列表检测 if(excludesList != null && excludesList.contains(fromMethodName.substring(3).toLowerCase())) { continue; } toMethodName = "set" + fromMethodName.substring(3); toMethod = findMethodByName(toMethods, toMethodName); if (toMethod == null) continue; Object value = fromMethod.invoke(from, new Object[0]); if(value == null) continue; //集合类判空处理 if(value instanceof Collection) { Collection newValue = (Collection)value; if(newValue.size() <= 0) continue; } toMethod.invoke(to, new Object[] {value}); } } /** * 对象属性值复制,仅复制指定名称的属性值 * @param from * @param to * @param includsArray * @throws Exception */ @SuppressWarnings("unchecked") public static void copyPropertiesInclude(Object from, Object to, String[] includsArray) throws Exception { List<String> includesList = null; if(includsArray != null && includsArray.length > 0) { includesList = Arrays.asList(includsArray); //构造列表对象 } else { return; } Method[] fromMethods = from.getClass().getDeclaredMethods(); Method[] toMethods = to.getClass().getDeclaredMethods(); Method fromMethod = null, toMethod = null; String fromMethodName = null, toMethodName = null; for (int i = 0; i < fromMethods.length; i++) { fromMethod = fromMethods[i]; fromMethodName = fromMethod.getName(); if (!fromMethodName.contains("get")) continue; //排除列表检测 String str = fromMethodName.substring(3); if(!includesList.contains(str.substring(0,1).toLowerCase() + str.substring(1))) { continue; } toMethodName = "set" + fromMethodName.substring(3); toMethod = findMethodByName(toMethods, toMethodName); if (toMethod == null) continue; Object value = fromMethod.invoke(from, new Object[0]); if(value == null) continue; //集合类判空处理 if(value instanceof Collection) { Collection newValue = (Collection)value; if(newValue.size() <= 0) continue; } toMethod.invoke(to, new Object[] {value}); } } /** * 从方法数组中获取指定名称的方法 * * @param methods * @param name * @return */ public static Method findMethodByName(Method[] methods, String name) { for (int j = 0; j < methods.length; j++) { if (methods[j].getName().equals(name)) return methods[j]; } return null; }
2 利用apache commons-beanutils的开源实现。
BeanUtils.copyProperties(dst, src)。方法能够将源对象和目标对象中相同名称的属性值复制过去。注意的是参数前面的是目标对象,后面是源对象。使用该方法需要注意:不能将入口方法与源对象、目标对象之一放在同一源文件之内,否者将没有任何效果。
PropertyUtils.copyProperties(dst, src)。功能与BeanUtils.copyProperties类似,只是在同属性名称的类型参数之间可以执行转换操作。
3 利用Spring实现属性之间的复制。spring内部自有实现方法,如果我们需要在外面采用spring的托管复制,需要修改spring的源码,将spring中的org.springframework.beans.CachedIntrospectionResults类的forClass、getPropertyDescriptor、getBeanInfo改为可见的后重新打包。然后将Spring中关于复制的代码提取出来,最后修改成代码如下:
/** * 利用spring实现bean之间属性复制 * @param source * @param target */ @SuppressWarnings("unchecked") public static void copyPropertiesBySpring(Object source, Object target) throws Exception { Class actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (int i = 0; i < targetPds.length; i++) { PropertyDescriptor targetPd = targetPds[i]; if (targetPd.getWriteMethod() != null) { PropertyDescriptor sourcePd = getPropertyDescriptor(source .getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass() .getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source, new Object[0]); if(value == null) continue; //集合类判空处理 if(value instanceof Collection) { // Collection newValue = (Collection)value; // if(newValue.size() <= 0) continue; } Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass() .getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, new Object[] { value }); } catch (Throwable ex) { } } } } } /** * 获取指定类指定名称的属性描述符 * @param clazz * @param propertyName * @return * @throws BeansException */ @SuppressWarnings("unchecked") public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) throws BeansException { CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz); return cr.getPropertyDescriptor(propertyName); } /** * 获取指定类得所有属性描述符 * @param clazz * @return * @throws BeansException */ @SuppressWarnings("unchecked") public static PropertyDescriptor[] getPropertyDescriptors(Class clazz) throws BeansException { CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz); return cr.getBeanInfo().getPropertyDescriptors(); }
相关文章推荐
- java 对象之间属性值复制
- java不同对象之间同名属性值复制
- java 对象之间的复制
- Java利用反射实现对象之间相同属性复制
- commons-beanutils的使用【在对象之间进行属性值复制】
- Java对象之间的深度复制拷贝
- java中两个对象间的属性值复制,比较,转为map方法实现
- java中equals和==之间的区别?clone方法的作用,及其为什么要使用clone方法?如何使用clone复制对象?以及深克隆浅克隆
- java对象复制和属性值复制工具类
- Java -- 利用反射实现对象之间相同属性复制BeanUtil
- java 中对象之间的复制
- java 反射实现不同对象相同属性值复制
- Java对象间属性值的复制-Spring的BeanUtil
- 对象之间进行属性值复制
- java反射获取对象的属性及属性值
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- 关于java对象复制(clone)
- Java中关于Json对象类型和字符串类型之间互相转化的问题 .
- Java 遍历对象属性,并将属性值不为空的取出.
- java对象复制