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>
    

      

  • 相关阅读:
    iType.js仿输入文字效果
    css上下左右居中
    js的几种继承方式
    jquery ajax跨越
    js构造函数+原型
    less基础引用
    vw单位相关
    移动端适配(rem单位定义方法)
    第二周 day2 python学习笔记
    第一周 day1 Python学习笔记
  • 原文地址:https://www.cnblogs.com/q1359720840/p/14781811.html
Copyright © 2011-2022 走看看