复写springframework下的BeanUtils.copyProperties方法

编码时,实体类参数名转换常导致重复工作。为解决此问题,参考BeanUtils.copyProperties方法,结合注解重写一套方法,还新建注解、编写测试示例类和对象复制方法。本机测试耗时不到1ms,给出git地址,欢迎交流指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景: 编码过程中经常遇到,实体类中参数名转换其他参数名,以做后续业务逻辑处理。 同一个功能,可能不同的人接手后,不知道之前转换的方法在哪里,又重新写了一个方法,或者不同的实体,需要转换成同一个实体,这样就造成重复工作浪费。

为解决这个问题,对参数名转换进行封装,提高工作效率, 参考BeanUtils.copyProperties方法,结合注解的使用,重写了一套方法。

 

  • 新建注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RefAnnotation {

    String[] dec() default {};
}
  •  编写两个测试示例类
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ReflectVo implements Serializable {
    private static final long serialVersionUID = 8827950221777954148L;

    @RefAnnotation(dec = {"aaa","bbb","ccc"})
    private String name;

    @RefAnnotation(dec = {"ddd","eee"})
    private String scott;
}

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class SourceVo implements Serializable {
    private static final long serialVersionUID = -6564361729001137186L;

    private String aaa;
    private String ddd;
}
  •  对象复制方法
public class BeanCopyVo {

    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 = BeanUtils.getPropertyDescriptors(actualEditable);

        Field[] fields = actualEditable.getDeclaredFields();

        Map<String, Field> map = new HashMap<>();
        for (Field f : fields) {
            map.put(f.getName(), f);
        }

        for (PropertyDescriptor targetPd : targetPds) {
            Method writeMethod = targetPd.getWriteMethod();

            if (null == writeMethod) {
                continue;
            }

            //获取注解
            String fieldName = targetPd.getName();
            Field field = map.get(fieldName);
            if (null == field) {
                continue;
            }

            if (!field.isAnnotationPresent(RefAnnotation.class)) {
                continue;
            }
            RefAnnotation annotation = field.getAnnotation(RefAnnotation.class);
            String[] dec = annotation.dec();

            if (writeMethod != null) {
                PropertyDescriptor sourcePd = null;
                for (String methodName : dec) {
                    sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), methodName);

                    if (null == sourcePd) {
                        continue;
                    }

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

            }
        }
    }
}
  • 注意 性能影响请结合自己系统测试,本机测试结果耗时不到1ms。

git地址:https://github.com/fengyantao/beancopy

欢迎大家多多交流和指导

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DazedMen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值