标签:tcl static vat public 工具 java shm copy bean
注意:背赋值的bean不要加上 @Accessors(chain = true) 注解
import org.springframework.cglib.beans.BeanCopier;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class BeanCopierUtils {
// 创建过的BeanCopier实例放到缓存中,下次可以直接获取,提升性能
static final Map<String, BeanCopier> BEAN_COPIERS = new ConcurrentHashMap<String, BeanCopier>();
public static Object copy(Object srcObj, Object destObj) {
String key = genKey(srcObj.getClass(), destObj.getClass());
BeanCopier copier = null;
if (!BEAN_COPIERS.containsKey(key)) {
copier = BeanCopier.create(srcObj.getClass(), destObj.getClass(), false);
BEAN_COPIERS.put(key, copier);
} else {
copier = BEAN_COPIERS.get(key);
}
copier.copy(srcObj, destObj, null);
return destObj;
}
private static String genKey(Class<?> srcClazz, Class<?> destClazz) {
return srcClazz.getName() + destClazz.getName();
}
}
标签:tcl static vat public 工具 java shm copy bean
原文地址:https://www.cnblogs.com/bt2882/p/12808219.html