zoukankan      html  css  js  c++  java
  • 基于原型模式和享元模式的数据拷贝

    基于原型模式完成分层pojo之间拷贝。

    Apache的BeanUtils,反射、

    Apache的PropertyUtils,反射,

    Spring的BeanUtils,反射

    cglib的BeanCopier,动态代理性能好

    public class BeanCopierUtils{
    public static HashMap<String,BeanCopier> cacheMap=new HashMap<String,BeanCopier>();

    public void copyproperties(Object source,Object target){
    String cache= source.getclass().tostring()+target.getClass().tostring();
    if(!cacheMap.contentKey(cache)){
    synchronized(BeancopierUtils.class){
    if(!cacheMap.containskey(cache)){
    beancopier= Beancopier.create(source.getclass,target.getclass,false);
    cacheMap.put(cache,beancopier);
    }else{
    beanCopier= cahceMap.get(cache);
    }
    }

    }else{
    beanCopier=cacheMap.get(cache);
    }

    beancopier.copy(source,target,null);
    }
    }

      

    public class StudentVo {
            private String id;
            private String name;
    public <T> T clone(Class<T> tClass){
        T target= null;
        try {
            target = tClass.newInstance();
            BeanCopierUtils.copyproperties(this,target);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  target;
    }
        }
            <!-- cglib的BeanCopier需要的依赖 -->
            <dependency>
                <groupId>asm</groupId>
                <artifactId>asm</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
                <groupId>asm</groupId>
                <artifactId>asm-commons</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
                <groupId>asm</groupId>
                <artifactId>asm-util</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib-nodep</artifactId>
                <version>2.2.2</version>
            </dependency>
    

      

  • 相关阅读:
    day4-生成器
    第三天-函数
    编码问题
    可变不可变类型总结
    由var与let引起的百度地图所有的覆盖点的信息都是最后一个的
    《企业应用架构模式》 三
    IndexDB
    ESA与SOA
    SOA
    Dubbo(一)
  • 原文地址:https://www.cnblogs.com/q1359720840/p/14781811.html
Copyright © 2011-2022 走看看