• java复制对象,复制对象属性,只可复制两个对象想同的属性名。也可自定义只复制需要的属性。


    注意:使用时copy()方法只会复制相同的属性。常用的copy()方法。以下为封装的工具和使用方式。

    1.封装类

    import java.util.Map;

    import java.util.WeakHashMap;

    import org.springframework.cglib.beans.BeanCopier;
    import org.springframework.cglib.core.Converter;
    public class BeanCopyUtils {
    // 使用WeakHashMap缓存,在内存不足时会自动释放
    private final static Map<String, BeanCopier> BEAN_COPIER_MAP = new WeakHashMap<>();
    private final static Map<String, Converter> CONVERTER_MAP = new WeakHashMap<>();
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    private BeanCopyUtils() {
    }

    /**
    * 创建BeanCopier,并缓存
    *
    * @param src
    * @param target
    * @param useConverter
    * @return
    */
    private static BeanCopier getBeanCopier(Object src, Object target, boolean useConverter) {
    String key = generateKey(src, target, useConverter);
    BeanCopier bc = BEAN_COPIER_MAP.get(key);
    if (null == bc) {
    synchronized (lock1) {
    bc = BEAN_COPIER_MAP.get(key);
    if (null == bc) {
    bc = BeanCopier.create(src.getClass(), target.getClass(), useConverter);
    BEAN_COPIER_MAP.put(key, bc);
    System.out.println("Create BeanCopier with key:" + key);
    }
    }
    }
    return bc;
    }

    /**
    * 复制对象属性
    *
    * @param src
    * @param target
    */
    public static void copy(Object src, Object target) {
    BeanCopier bc = getBeanCopier(src, target, false);
    bc.copy(src, target, null);
    }

    /**
    * 使用自定义的属性转换器复制对象属性
    *
    * @param src
    * @param target
    * @param converter
    */
    public static void copy(Object src, Object target, Converter converter) {
    BeanCopier bc = getBeanCopier(src, target, true);
    bc.copy(src, target, converter);
    }

    /**
    * 对象属性复制,只复制fields中指定的属性,每个属性用逗号分隔
    *
    * @param src
    * @param target
    * @param fields
    */
    public static void copyWithFields(Object src, Object target, final String fields) {
    BeanCopier bc = getBeanCopier(src, target, true);
    bc.copy(src, target, newConverter(src, target, fields, true));
    }

    /**
    * 对象属性复制,排除指定属性
    *
    * @param src
    * @param target
    * @param fields
    */
    public static void copyWithoutFields(Object src, Object target, final String fields) {
    BeanCopier bc = getBeanCopier(src, target, true);
    bc.copy(src, target, newConverter(src, target, fields, false));
    }

    /**
    * new属性转换器,
    *
    * @param fields
    * 需要复制或排除的属性
    * @param fieldCopyFlag
    * 属性复制标识 true:表明fields为需要复制的属性;false:表明fields是需要排除复制的属性
    * @return
    */
    private static Converter newConverter(Object src, Object target, final String fields, final boolean fieldCopyFlag) {
    String key = buildConverterkey(src, target, fields, fieldCopyFlag);
    Converter converter = CONVERTER_MAP.get(key);
    if (null == converter) {
    synchronized (lock2) {
    converter = CONVERTER_MAP.get(key);
    if (null == converter) {
    converter = new Converter() {
    @Override
    public Object convert(Object fieldValue, Class fieldType, Object methodName) {
    String field = methodName.toString().substring(3); // 得到属性名,如Name
    field = field.substring(0, 1).toLowerCase() + field.substring(1); // 将首字母小写
    if ((fieldCopyFlag && fields.contains(field))
    || (!fieldCopyFlag && !fields.contains(field))) {
    return fieldValue;
    }
    return null;
    }
    };
    CONVERTER_MAP.put(key, converter);
    System.out.println("Created Converter with key:" + key);
    }
    }
    }
    return converter;
    }

    private static String generateKey(Object src, Object target, boolean useConverter) {
    return src.getClass().getName() + target.getClass().getName() + String.valueOf(useConverter);
    }

    private static String buildConverterkey(Object src, Object target, String fields, boolean copyFlag) {
    String baseKey = generateKey(src, target, true);
    String key = baseKey + fields + String.valueOf(copyFlag);
    return key;
    }

    }

    2.使用实例

    FileCabinet fb = list.get(i);
    FileCabinetReturn fbReturn = new FileCabinetReturn();
    BeanCopyUtils.copy(fb, fbReturn);

  • 相关阅读:
    【JZOJ3347】树的难题
    【JZOJ3379】查询
    【JZOJ3337】wyl8899的TLE
    【JZOJ3318】Brunhilda的生日
    【JZOJ3319】雪地踪迹
    2018暑假集训——待做且可尝试去做的题
    2018暑假集训专题小结 Part.2
    2018.07.13【省赛模拟】模拟B组 【GDOI2016模拟】作业分配
    2018暑假集训专题小结 Part.1
    2018.07.12【2018提高组】模拟B组 【NOIP2015模拟10.27】魔法阵
  • 原文地址:https://www.cnblogs.com/guangxiang/p/11061265.html
Copyright © 2020-2023  润新知