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>
    

      

  • 相关阅读:
    python 关于文件操作
    python2 编码与解码
    Git系列(二) 冲突解决
    异步回调机制分析
    CSS盒子模型理解
    Git多人协同开发模型
    CSS连载控制背景与CSS精灵
    函数调用在回调,委托与事件在程序设计中的应用
    TFS与Git结合进行代码管理
    Git系列之(二)Git协议与工作协同
  • 原文地址:https://www.cnblogs.com/q1359720840/p/14781811.html
Copyright © 2011-2022 走看看