两个实体类之间进行互换
BeanUtils它提供了对java反射和自省API的包装。它里面还有很多工具类,这里我们介绍一下copyProperties。
我们如果有两个具有很多相同属性的JavaBean,一个很常见的情况就是Struts里的PO对象(持久对象)和对应的ActionForm,传统的方式对属性逐个赋值:
1 2 3 | casesUserIntegralEntity.setPluginId(casesUserIntegralEntityList.get(i).getPluginId()); casesUserIntegralEntity.setTypeKey(casesUserIntegralEntityList.get(i).getTypeKey()); casesUserIntegralEntity.setPrimaryId(casesUserIntegralEntityList.get(i).getPrimaryId()); casesUserIntegralEntity.setName(casesUserIntegralEntityList.get(i).getName()); casesUserIntegralEntity.setIntegralUser(casesUserIntegralEntityList.get(i).getIntegralUser()); casesUserIntegralEntity.setIntegralStanderd(casesUserIntegralEntityList.get(i).getIntegralStanderd()); |
如果按照上面那种赋值方式,是非常麻烦的,而且代码量可以估计会很多
这时候我们如果用copyProperties,直接一行代码,然后就搞定了。
BeanUtils.copyProperties("转换前的类", "转换后的类");
例如:
1 | BeanUtils.copyProperties(casesUserIntegralEntity,casesUserIntegral); |
但是我们需要注意:
BeanUtils.copyProperties(a, b);
b中的存在的属性,a中一定要有,但是a中可以有多余的属性;
a中与b中相同的属性都会被替换,不管是否有值;
a、 b中的属性要名字相同,才能被赋值,不然的话需要手动赋值;
Spring的BeanUtils的CopyProperties方法需要对应的属性有getter和setter方法;
如果存在属性完全相同的内部类,但是不是同一个内部类,即分别属于各自的内部类,则spring会认为属性不同,不会copy;
spring和apache的copy属性的方法源和目的参数的位置正好相反,所以导包和调用的时候都要注意一下。
源码
最后附上源码,方便大家可以更好地理解他的属性;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | /** * Copy the property values of the given source bean into the given target bean. * <p>Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * @param source the source bean * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @param ignoreProperties array of property names to ignore * @throws BeansException if the copying failed * @see BeanWrapper */ private static void copyProperties(Object source, Object target, @Nullable Class<?> editable, @Nullable String... ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null" ); Assert.notNull(target, "Target must not be null" ); Class<?> actualEditable = target.getClass(); if (editable != null ) { if (!editable.isInstance(target)) { throw new IllegalArgumentException( "Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]" ); } actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null ); for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null ) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[ 0 ], readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible( true ); } Object value = readMethod.invoke(source); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible( true ); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new FatalBeanException( "Could not copy property '" + targetPd.getName() + "' from source to target" , ex); } } } } } } |
另外,apache和springboot下面都有这个工具类,顺序是颠倒的,引包需要注意。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本