以下是实现代码:
import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.List;import org.springframework.beans.BeanUtils;public class BeanCopyUtil { /** * 复制bean属性 * * @param source 源bean * @param target 目标bean * @param replaceChars 需要替换的字符列表 */ public static void copyProperties(Object source, Object target, List<String> replaceChars) { BeanUtils.copyProperties(source, target); if (replaceChars != null && !replaceChars.isEmpty()) { PropertyDescriptor[] sourcePds = BeanUtils.getPropertyDescriptors(source.getClass()); PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass()); for (PropertyDescriptor sourcePd : sourcePds) { if (sourcePd.getReadMethod() != null) { for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null && sourcePd.getName().equals(targetPd.getName())) { try { Object sourceValue = sourcePd.getReadMethod().invoke(source); if (sourceValue != null && sourceValue instanceof String) { String targetValue = (String) sourceValue; for (String replaceChar : replaceChars) { targetValue = targetValue.replace(replaceChar, ""); } targetPd.getWriteMethod().invoke(target, targetValue); } else { targetPd.getWriteMethod().invoke(target, sourceValue); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } break; } } } } } } /** * 复制bean属性 * * @param source 源bean * @param targetClass 目标bean的class * @param replaceChars 需要替换的字符列表 * @return 目标bean */ public static <T> T copyProperties(Object source, Class<T> targetClass, List<String> replaceChars) { T target = null; try { target = targetClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } copyProperties(source, target, replaceChars); return target; } /** * 复制bean属性 * * @param sourceList 源bean列表 * @param targetClass 目标bean的class * @param replaceChars 需要替换的字符列表 * @return 目标bean列表 */ public static <T> List<T> copyProperties(List<?> sourceList, Class<T> targetClass, List<String> replaceChars) { List<T> targetList = new ArrayList<>(); for (Object source : sourceList) { T target = copyProperties(source, targetClass, replaceChars); targetList.add(target); } return targetList; }}
使用示例:
public class Test { public static void main(String[] args) { // 复制单个bean属性 User source = new User(); source.setId(1L); source.setName("Tom"); source.setAge(20); User target = new User(); BeanCopyUtil.copyProperties(source, target, Arrays.asList("o")); System.out.println(target.getId()); // 1 System.out.println(target.getName()); // Tm System.out.println(target.getAge()); // 20 // 复制bean列表属性 List<User> sourceList = new ArrayList<>(); sourceList.add(source); List<UserVO> targetList = BeanCopyUtil.copyProperties(sourceList, UserVO.class, Arrays.asList("o")); System.out.println(targetList.get(0).getId()); // 1 System.out.println(targetList.get(0).getName()); // Tm System.out.println(targetList.get(0).getAge()); // 20 }}class User { private Long id; private String name; private Integer age; // 省略getter和setter方法}class UserVO { private Long id; private String name; private Integer age; // 省略getter和setter方法}