1:属性类型不一致导致拷贝失败
public class BeanUtilsTest { public static void main(String[] args) { SourcePoJo sourcePoJo = new SourcePoJo("jingdong", (long) 35711); TargetPoJo targetPoJo = new TargetPoJo(); BeanUtils.copyProperties(sourcePoJo,targetPoJo); System.out.println(targetPoJo); } } @Data @AllArgsConstructor class SourcePoJo{ private String username; private Long id; } @Data class TargetPoJo{ private String username; private String id; }
public class BeanUtilsTest { public static void main(String[] args) { SourcePoJo sourcePoJo = new SourcePoJo(); sourcePoJo.setUsername("joy"); TargetPoJo targetPoJo = new TargetPoJo(); BeanUtils.copyProperties(sourcePoJo,targetPoJo); System.out.println(targetPoJo); } } @Data class SourcePoJo{ private String username; private Long id; } @Data class TargetPoJo{ private String username; private long id; }
2:null值覆盖导致数据异常
public class BeanUtilsTest { public static void main(String[] args) { SourcePoJo sourcePoJo = new SourcePoJo(); sourcePoJo.setId("35711"); TargetPoJo targetPoJo = new TargetPoJo(); targetPoJo.setUsername("Joy"); BeanUtils.copyProperties(sourcePoJo,targetPoJo); System.out.println(targetPoJo); } } @Data class SourcePoJo{ private String username; private String id; } @Data class TargetPoJo{ private String username; private String id; }
3:导包错误导致拷贝数据异常
//org.springframework.beans.BeanUtils(源对象在左边,目标对象在右边) public static void copyProperties(Object source, Object target) throws BeansException //org.apache.commons.beanutils.BeanUtils(源对象在右边,目标对象在左边) public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException
4:查找不到字段引用,修改内容难以溯源
5:内部类数据无法成功拷贝
public class BeanUtilsTest { public static void main(String[] args) { SourcePoJo sourcePoJo = new SourcePoJo(); sourcePoJo.setUsername("joy"); SourcePoJo.InnerClass innerClass = new SourcePoJo.InnerClass("sourceInner"); sourcePoJo.innerClass=innerClass; System.out.println(sourcePoJo.toString()); TargetPoJo targetPoJo = new TargetPoJo(); BeanUtils.copyProperties(sourcePoJo,targetPoJo); System.out.println(targetPoJo.toString()); } } //下面是类的信息,这里就直接放到一块展示了 @Data @ToString public class SourcePoJo{ private String username; private Long id; public InnerClass innerClass; @Data @ToString @AllArgsConstructor public static class InnerClass{ public String innerName; } } @Data @ToString public class TargetPoJo{ private String username; private Long id; public InnerClass innerClass; @Data @ToString public static class InnerClass{ public String innerName; } }
6:BeanUtils.copyProperties是浅拷贝
public class BeanUtilsTest { public static void main(String[] args) { Person sourcePerson = new Person("sunyangwei",new Card("123456")); Person targetPerson = new Person(); BeanUtils.copyProperties(sourcePerson, targetPerson); sourcePerson.getCard().setNum("35711"); System.out.println(targetPerson); } } @Data @AllArgsConstructor class Card { private String num; } @NoArgsConstructor @AllArgsConstructor @Data class Person { private String name; private Card card; }
7:底层实现为反射拷贝效率低
public class BeanUtilsTest { public static void main(String[] args) { long copyStartTime = System.currentTimeMillis(); User sourceUser = new User("sunyangwei"); User targetUser = new User(); for(int i = 0; i < 10000; i++) { BeanUtils.copyProperties(sourceUser, targetUser); } System.out.println("copy方式:"+(System.currentTimeMillis()-copyStartTime)); long setStartTime = System.currentTimeMillis(); for(int i = 0; i < 10000; i++) { targetUser.setUserName(sourceUser.getUserName()); } System.out.println("set方式:"+(System.currentTimeMillis()-setStartTime)); } } @Data @AllArgsConstructor @NoArgsConstructor class User{ private String userName; }
本文仅供学习!所有权归属原作者。侵删!文章来源: 京东云开发者 -孙扬威 :http://mp.weixin.qq.com/s/bzLviYljo0RpDZnjbs5Ftw
文章评论