比Spring的BeanUtils.copyProperties还快的对象属性拷贝工具

关于对象属性拷贝,虽说有现成的工具可用,之前都是自己写的反射进行拷贝,效率比较后,发现效率不高。

自从看了spring的源码,决定优化一下自定义的拷贝工具,主要是吸收spring的优秀思想,即使用内存作为缓存,同样的一个class拷贝时不用每次都进行反射,这样大大提升了效率。

话不多说,直接上源码:

class MethodObj{
    private String name;
    private Method readMethod;
    private Method writeMethod;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Method getReadMethod() {
        return readMethod;
    }

    public void setReadMethod(Method readMethod) {
        this.readMethod = readMethod;
    }

    public Method getWriteMethod() {
        return writeMethod;
    }

    public void setWriteMethod(Method writeMethod) {
        this.writeMethod = writeMethod;
    }
}
class CacheMethod{
    Map<String, MethodObj> methodObjMap = new HashMap<>();
    static Map<Class, CacheMethod> classMapCacheMethod = new HashMap<>();
    Collection<MethodObj> methodObjs;

    public static Collection<MethodObj> getMethodObjList(Class clazz){
        CacheMethod cm = classMapCacheMethod.get(clazz);
        if(null==cm){
            cm = build(clazz);
        }
        return cm.getMethodObjs();
    }

    public static MethodObj getMethodObj(Class clazz, String propertyName){
        CacheMethod cm = classMapCacheMethod.get(clazz);
        if(null==cm){
            cm = build(clazz);
        }
        return cm.getMethodObjMap().get(propertyName);
    }

    private static CacheMethod build(Class clazz){
        CacheMethod cm = new CacheMethod();
        for(Method m : clazz.getDeclaredMethods()){
            String name = m.getName().replaceAll("set","").replaceAll("get","");
            MethodObj methodObj = cm.methodObjMap.get(name);
            if(null==methodObj){
                methodObj = new MethodObj();
                methodObj.setName(name);
            }
            if(m.getName().startsWith("set")){
                methodObj.setWriteMethod(m);
            }else if(m.getName().startsWith("get")){
                methodObj.setReadMethod(m);
            }
            cm.methodObjMap.put(name, methodObj);
        }
        cm.methodObjs = cm.methodObjMap.values();
        classMapCacheMethod.put(clazz, cm);
        return cm;
    }

    public Collection<MethodObj> getMethodObjs() {
        return methodObjs;
    }

    public void setMethodObjs(Collection<MethodObj> methodObjs) {
        this.methodObjs = methodObjs;
    }

    public Map<String, MethodObj> getMethodObjMap() {
        return methodObjMap;
    }

    public void setMethodObjMap(Map<String, MethodObj> methodObjMap) {
        this.methodObjMap = methodObjMap;
    }

    public static Map<Class, CacheMethod> getClassMapCacheMethod() {
        return classMapCacheMethod;
    }

    public static void setClassMapCacheMethod(Map<Class, CacheMethod> classMapCacheMethod) {
        CacheMethod.classMapCacheMethod = classMapCacheMethod;
    }
}
public class FastBeanUtils{

    public static void copy(Object dest, Object orig) throws InvocationTargetException, IllegalAccessException {
        Collection<MethodObj> methodObjList = CacheMethod.getMethodObjList(orig.getClass());
        for(MethodObj m: methodObjList){
            MethodObj methodObj = CacheMethod.getMethodObj(dest.getClass(), m.getName());
            if(null==methodObj){
                continue;
            }
            Object value = m.getReadMethod().invoke(orig);
            methodObj.getWriteMethod().invoke(dest, value);
        }
    }
}

通过循环1亿次对象拷贝,性能是spring的三倍。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring中使用BeanUtils.copyProperties进行属性拷贝时,如果属性的类型不匹配,会尝试进行类型转换。Spring会根据属性的类型和可用的类型转换器进行自动转换。 如果属性的类型不是直接兼容的,你可以注册自定义的类型转换器来实现属性的转换。可以通过实现Converter接口或继承自PropertyEditorSupport类来创建自定义的类型转换器。然后,将自定义的类型转换器注册到Spring的类型转换服务中。 下面是一个示例,演示如何注册自定义的类型转换器: ```java public class MyCustomConverter implements Converter<String, MyCustomType> { @Override public MyCustomType convert(String source) { // 实现自定义的类型转换逻辑 // 将字符串转换为MyCustomType对象 } } // 在配置类或者XML配置文件中注册自定义的类型转换器 @Configuration public class AppConfig { @Bean public ConversionService conversionService() { DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new MyCustomConverter()); return conversionService; } } ``` 在上述代码中,我们创建了一个名为`MyCustomConverter`的自定义转换器,将String类型的值转换为`MyCustomType`对象。然后,在配置类(或XML配置文件)中创建了一个`ConversionService` bean,并将自定义转换器添加到其中。 当使用`BeanUtils.copyProperties`进行属性拷贝时,Spring将会使用注册的类型转换器进行类型转换。如果遇到不兼容的类型,会尝试使用转换器进行转换。 需要注意的是,如果属性之间的转换逻辑比较复杂,可能需要考虑使用其他方式来实现属性拷贝,例如使用映射工具或手动编写转换逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值