聊聊BeanUtils.copyProperties和clone()方法的区别

目录

  • 首先,BeanUtils有两种:
  • 效率:
  • 需要在pom文件中引入这个包
  • 在pom文件里面引入所需要的包
  • 新建一个实体类StudentEntity实现Cloneable接口
  • 测试方法
最近撸代码的时候发现有人将一个对象的值赋给另一个对象的时候,并没有使用常规的set/get方法去给对象赋值,而是采用BeanUtils.copyProperties(A,B)这个方法去赋值,但是有的还是有局限性,比如Date类型的值无法赋值,只能赋值为null,所以我大致百度了一下,作为记录。

首先,BeanUtils有两种:
org.springframework.beans和org.apache.commons.beanutils,前面是spring的,后面是apache公司的。

效率:
传统的set/get>spring的>apache的
我们首先来使用一下效率最慢的org.apache.commons.beanutils

需要在pom文件中引入这个包
并且要配合第三方的日志工具来使用,一般都是使用的是common logging包
commons-loggingcommons-logging1.1.3commons-beanutilscommons-beanutils1.9.2

在引入完成之后,我们新建一个类TestBean,里面有两个属性,userName和password
private String userName; private String password; public String getUserName() {return userName; }public void setUserName(String userName) {this.userName = userName; }public String getPassword() {return password; }public void setPassword(String password) {this.password = password; }

然后我们再新建一个类CopyBean,这个类中的属性和上面的TestBean类相同
private String userName; private String password; public String getUserName() {return userName; }public void setUserName(String userName) {this.userName = userName; }public String getPassword() {return password; }public void setPassword(String password) {this.password = password; }

然后我们写我们的main方法
public static void main(String[] args) {TestBean bean = new TestBean(); bean.setUserName("qxf"); bean.setPassword("123"); CopyBean copyBean = new CopyBean(); try {//这个地方如果你安装了阿里的代码检测控件的话,会报错,提示你使用spring的BeanUtils,而不要使用apache的BeanUtilsBeanUtils.copyProperties(copyBean,bean); System.out.println(copyBean.getUserName()); System.out.println(copyBean.getPassword()); } catch (IllegalAccessException e) {e.printStackTrace(); } catch (InvocationTargetException e) {e.printStackTrace(); }}

执行结果
聊聊BeanUtils.copyProperties和clone()方法的区别
文章图片

接下来我们再来使用一下spring的org.springframework.beans

在pom文件里面引入所需要的包
org.springframeworkspring-beans5.0.7.RELEASE

然后新建CopyBean和TestBean两个类,这两个和上述的相同,因此在此不再赘述
Main方法
public static void main(String[] args) {TestBean bean = new TestBean(); bean.setUserName("qxf"); bean.setPassword("123"); CopyBean copyBean = new CopyBean(); //这个和apache的恰恰相反,具体的原因需要查看源码才可以理解BeanUtils.copyProperties(bean,copyBean); System.out.println(copyBean.getUserName()); System.out.println(copyBean.getPassword()); }

输出结果:
聊聊BeanUtils.copyProperties和clone()方法的区别
文章图片

可见两者实现的结果是相同的,但是两者的效率是不一样的
而我们在看clone()方法的时候,发现也是类似的,也可以将对象中的属性copy到另一个对象的属性中

新建一个实体类StudentEntity实现Cloneable接口
/*** @Description:* @Author: qxf* @Date: 2019/2/21 2:22 PM*/ public class StudentEntity implements Cloneable {private String userName; private String passWord; private List list; public List getList() {return list; }public void setList(List list) {this.list = list; }public String getUserName() {return userName; }public void setUserName(String userName) {this.userName = userName; }public String getPassWord() {return passWord; }public void setPassWord(String passWord) {this.passWord = passWord; }@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone(); }}


测试方法
public void testClone(){//测试clone方法StudentEntity entity = new StudentEntity(); entity.setUserName("qxf"); entity.setPassWord("test"); List list = new ArrayList<>(); list.add("Test1"); list.add("Test2"); list.add("Test3"); list.add("Test4"); entity.setList(list); try {StudentEntity entityClone = (StudentEntity) entity.clone(); System.out.println(entityClone.getUserName()); System.out.println(entityClone.getPassWord()); List entityList = new ArrayList<>(); entityList = entityClone.getList(); for (int i=0; i
但是clone()的方法和copyProperties的区别还有待学习
【聊聊BeanUtils.copyProperties和clone()方法的区别】以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读