BeanUtils工具类,基于Spring框架。

BeanUtils工具类用于高效地复制对象,使用到cglib的BeanCopier,这里直接使用org.springframework.cglib.beans的类。

import org.springframework.cglib.beans.BeanCopier;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 *
 * @author Johny
 * 
 */
public class BeanUtils {

    private static final Map BEAN_COPIER_MAP = new ConcurrentHashMap<>();

    private BeanUtils() {
    }

    /**
     * 对象复制
     * @param source 被复制对象,为空会抛出异常
     * @param target 复制的结果,为空会抛出异常
     */
    public static void copyProperties(Object source, Object target) {
        Objects.requireNonNull(source, "source must not be null");
        Objects.requireNonNull(target, "target must not be null");

        BeanCopier beanCopier = getBeanCopier(source.getClass(), target.getClass());
        beanCopier.copy(source, target, null);
    }

    /**
     * 对象复制
     * @param source 被复制对象,为空会抛出异常
     * @param clazz 复制类型,为空会抛出异常
     * @param 
     */
    public static  T copyObject(Object source, Class clazz) {
        T result;
        try {
            result = clazz.newInstance();
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException("fail to create instance of type" + clazz.getCanonicalName(), e);
        }

        copyProperties(source, result);
        return result;
    }
    /**
     * 复制列表
     *
     * @param list   被复制列表
     * @param classz 复制类型
     * @param 
     * @return
     */
    public static  List copyList(List list, Class classz) {
        List resultList = new LinkedList<>();
        if (CollectionUtils.isEmpty(list)) {
            return resultList;
        }
        for (Object obj1 : list) {
            resultList.add(copyObject(obj1, classz));
        }
        return resultList;
    }

    private static BeanCopier getBeanCopier(Class source, Class target) {
        String key = generateKey(source, target);
        return BEAN_COPIER_MAP.computeIfAbsent(key, x -> BeanCopier.create(source, target, false));
    }

    private static String generateKey(Class source, Class target) {
        return source.getCanonicalName().concat(target.getCanonicalName());
    }
}

BeanUtils工具类的实际应用例子:

import com.ligeit.operate.biz.domain.model.CatDomain;
import com.ligeit.operate.biz.domain.repository.IHuaBoRepository;
import com.ligeit.operate.infrastructure.persistence.mongo.Cat;
import com.ligeit.operate.infrastructure.utils.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 *
 * @author Johny
 * 
 */
@Repository
public class CatRepository implements ICatRepository {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public List getCatList(Long catId) {
        Query query = new Query();
        if(catId == null){
            query.addCriteria(Criteria.where("level").is(1));
        }else{
            query.addCriteria(Criteria.where("categoryId").is(catId));
        }
        List list = mongoTemplate.find(query, Cat.class, MongoConstant.PRODUCE_CAT);
        return BeanUtils.copyList(list,CatDomain.class);
    }
}

 

你可能感兴趣的:(BeanUtils工具类,基于Spring框架。)