聊聊BeanUtils.copyProperties和clone()方法的區(qū)別
最近擼代碼的時候發(fā)現(xiàn)有人將一個對象的值賦給另一個對象的時候,并沒有使用常規(guī)的set/get方法去給對象賦值,而是采用BeanUtils.copyProperties(A,B)這個方法去賦值,但是有的還是有局限性,比如Date類型的值無法賦值,只能賦值為null,所以我大致百度了一下,作為記錄。
首先,BeanUtils有兩種:
org.springframework.beans和org.apache.commons.beanutils,前面是spring的,后面是apache公司的。
效率:
傳統(tǒng)的set/get>spring的>apache的
我們首先來使用一下效率最慢的org.apache.commons.beanutils
需要在pom文件中引入這個包
并且要配合第三方的日志工具來使用,一般都是使用的是common logging包
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
在引入完成之后,我們新建一個類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的BeanUtils
BeanUtils.copyProperties(copyBean,bean);
System.out.println(copyBean.getUserName());
System.out.println(copyBean.getPassword());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
執(zhí)行結(jié)果

接下來我們再來使用一下spring的org.springframework.beans
在pom文件里面引入所需要的包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
然后新建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());
}
輸出結(jié)果:

可見兩者實現(xiàn)的結(jié)果是相同的,但是兩者的效率是不一樣的
而我們在看clone()方法的時候,發(fā)現(xiàn)也是類似的,也可以將對象中的屬性copy到另一個對象的屬性中
新建一個實體類StudentEntity實現(xiàn)Cloneable接口
/**
* @Description:
* @Author: qxf
* @Date: 2019/2/21 2:22 PM
*/
public class StudentEntity implements Cloneable {
private String userName;
private String passWord;
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> 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;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
測試方法
public void testClone(){
//測試clone方法
StudentEntity entity = new StudentEntity();
entity.setUserName("qxf");
entity.setPassWord("test");
List<String> 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<String> entityList = new ArrayList<>();
entityList = entityClone.getList();
for (int i=0;i<entityList.size();i++){
System.out.println(entityList.get(i));
}
}catch (Exception e){
e.printStackTrace();
}
}
但是clone()的方法和copyProperties的區(qū)別還有待學(xué)習(xí)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring實現(xiàn)bean對象創(chuàng)建代碼詳解
這篇文章主要介紹了spring實現(xiàn)bean對象創(chuàng)建代碼詳解,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
jpa使用uuid策略后無法手動設(shè)置id的問題及解決
這篇文章主要介紹了jpa使用uuid策略后無法手動設(shè)置id的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
如何在Spring?Boot中使用MyBatis訪問數(shù)據(jù)庫
MyBatis可以通過簡單的XML或者注解來配置和映射原始類型,接口,和Java POJO為數(shù)據(jù)庫中記錄,使用MyBatis幫助我們解決各種問題,本文介紹如何在Spring?Boot中使用MyBatis訪問數(shù)據(jù)庫,感興趣的朋友一起看看吧2023-11-11
詳解使用Spring Boot開發(fā)Restful程序
本篇文章主要介紹了詳解使用Spring Boot開發(fā)Restful程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Java JSON轉(zhuǎn)成List結(jié)構(gòu)數(shù)據(jù)
這篇文章主要介紹了Java JSON轉(zhuǎn)成List結(jié)構(gòu)數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SpringBoot使用SOFA-Lookout監(jiān)控的方法
本文介紹SpringBoot使用螞蟻金服SOFA-Lookout配合Prometheus進(jìn)行監(jiān)控,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03

