最近擼代碼的時候發現有人將一個對象的值賦給另一個對象的時候,并沒有使用常規的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包
1
2
3
4
5
6
7
8
9
10
|
< 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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類相同
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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(); } } |
執行結果
接下來我們再來使用一下spring的org.springframework.beans
在pom文件里面引入所需要的包
1
2
3
4
5
|
< dependency > < groupId >org.springframework</ groupId > < artifactId >spring-beans</ artifactId > < version >5.0.7.RELEASE</ version > </ dependency > |
然后新建CopyBean和TestBean兩個類,這兩個和上述的相同,因此在此不再贅述
Main方法
1
2
3
4
5
6
7
8
9
10
|
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()); } |
輸出結果:
可見兩者實現的結果是相同的,但是兩者的效率是不一樣的
而我們在看clone()方法的時候,發現也是類似的,也可以將對象中的屬性copy到另一個對象的屬性中
新建一個實體類StudentEntity實現Cloneable接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/** * @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(); } } |
測試方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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的區別還有待學習
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qxf1374268/article/details/88125837