java复制bean

一、瞎扯

      对于bean的复制(obj1-->obj2),常见的有两个工具类,一个是org.springframework.beans.BeanUtils,一个是org.apache.commons.beanutils.BeanUtils。。

      至于两者的一些区别,请看博客:

            https://www.cnblogs.com/dongfangshenhua/p/7099970.html

            https://blog.csdn.net/langqiao123/article/details/72961383/

      官方文档:

            https://docs.spring.io/spring-framework/docs/1.1.1/api/org/springframework/beans/BeanUtils.html

二、我的案例

      基于org.springframework.beans.BeanUtils对于对象复制的支持,我写了个工具类,简单的封装了一下。。

      1、项目结构:

java复制bean_第1张图片

      2、pom文件:



    4.0.0

    me.ele
    beanUtilsDemo
    1.0-SNAPSHOT
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

    
        
        
        
            org.springframework
            spring-beans
            4.1.7.RELEASE
        

    

      3、代码:

package me.ele;

import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.util.*;

/**
 * @author LZJ
 * @create 2018-10-11 19:47
 **/
public class BeanCopyUtils {

    /**
     * 将一个collection 根据某一class 进行copy
     *
     * @param froms
     * @param toClazz
     * @param 
     * @return
     */
    public static  Collection copy(Collection froms, Class toClazz) {
        if (froms == null)
            return null;
        Collection toList;
        try {
            toList = froms.getClass().newInstance();
            if (froms instanceof List) {
                toList = new ArrayList();
            } else if (froms instanceof Set) {
                toList = new HashSet();
            }
            for (Object obj : froms) {
                T to = copy(obj, toClazz);
                toList.add(to);
            }
        } catch (Exception e) {
            throw new RuntimeException("copy exception", e);
        }
        return toList;
    }

    /**
     * 将一个bean[] 根据某一class 进行copy
     *
     * @param froms   List
     * @param toClazz 某一class
     * @param 
     * @return List
     */
    public static  Collection copy(Object[] froms, Class toClazz) {
        if (froms == null) {
            return null;
        }
        List toList = new ArrayList<>();
        for (Object obj : froms) {
            T to = copy(obj, toClazz);
            toList.add(to);
        }
        return toList;
    }

    /**
     * 将一个bean 根据某一class 进行copy
     *
     * @param from    Object
     * @param toClazz 某一class的名称
     * @param 
     * @return class的实例
     */
    public static  T copy(Object from, Class toClazz) {
        if (from == null) {
            return null;
        }
        T to;
        try {
            to = toClazz.newInstance();
            copy(to, from);
        } catch (Exception e) {
            throw new RuntimeException("copy exception", e);
        }
        return to;
    }

    /**
     * 将一个bean 直接复制给另一个 bean
     *
     * @param from 初始bean
     * @param to   目标bean
     */
    public static void copy(Object from, Object to) {
        if (from == null) {
            return;
        }
        try {
            BeanUtils.copyProperties(from, to);
        } catch (Exception e) {
            throw new RuntimeException("copy exception", e);
        }
    }

    /**
     * 将一个map对象 ,对象中key为属性名,value为属性值,根据某一class进行copy
     *
     * @param toClazz
     * @param from
     * @param 
     * @return
     */
    public static  T copy(Class toClazz, Map from) {
        if (from == null || from.isEmpty()) {
            return null;
        }
        T to;
        try {
            to = toClazz.newInstance();
            copy(to, from);
        } catch (Exception e) {
            throw new RuntimeException("copy exception", e);
        }
        return to;
    }

    /**
     * 将一个map对象 ,对象中key为属性名,value为属性值 copy值某一object
     *
     * @param to
     * @param from
     */
    public static void copy(Object to, Map from) {
        if (from == null || from.isEmpty()) {
            return;
        }
        PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(to.getClass());
        from.forEach((String propertyName, Object value) -> {
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                if (propertyDescriptor.getName().equals(propertyName)
                        || propertyDescriptor.getName().toLowerCase().equals(propertyName.toLowerCase())) {
                    try {
                        propertyDescriptor.getWriteMethod().invoke(to, value);
                    } catch (Exception e) {
                        throw new RuntimeException("copy exception", e);
                    }
                }
            }
        });
    }
}
 
  

 

 

 

你可能感兴趣的:(java基础)