詳解Spring中BeanUtils工具類的使用
簡介
說明
本文介紹Spring的BeanUtils工具類的用法。
我們經(jīng)常需要將不同的兩個對象實例進行屬性復制,比如將DO對象進行屬性復制到DTO,這種轉(zhuǎn)換最原始的方式就是手動編寫大量的 get/set代碼,很繁瑣。為了解決這一痛點,就誕生了一些方便的類庫,常用的有 Apache的 BeanUtils,Spring的 BeanUtils, Dozer,Orika等拷貝工具。
由于Apache的BeanUtils的性能很差,強烈不建議使用。阿里巴巴Java開發(fā)規(guī)約插件上也明確指出:
“Ali-Check | 避免用Apache Beanutils進行屬性的copy。”
Spring的BeanUtils方法
| 方法 | 說明 |
|---|---|
| BeanUtils.copyProperties(source, target); | source對應的對象成員賦值給target對應的對象成員 |
| BeanUtils.copyProperties(source, target, "id", "time"); | 忽略拷貝某些字段。本處是忽略:id與time |
Spring的BeanUtils方法注意事項
泛型只在編譯期起作用,不能依靠泛型來做運行期的限制;
淺拷貝和深拷貝
淺拷貝:對基本數(shù)據(jù)類型進行值傳遞,對引用數(shù)據(jù)類型進行引用傳遞般的拷貝,此為淺拷貝
深拷貝:對基本數(shù)據(jù)類型進行值傳遞,對引用數(shù)據(jù)類型,創(chuàng)建一個新的對象,并復制其內(nèi)容,此為深拷貝。
Spring的BeanUtils與Apache的BeanUtils區(qū)別
| 項 | Spring的BeanUtils | Apache的BeanUtils |
|---|---|---|
| 性能 | 好 原因:對兩個對象中相同名字的屬性進行簡單的get/set,僅檢查屬性的可訪問性 | 差 原因:有很多檢驗:類型的轉(zhuǎn)換、對象所屬類的可訪問性等 注意:本處類型轉(zhuǎn)換是類似的類,多個String轉(zhuǎn)為List<String>是不行的。 |
| 使用方面 | 第一個參數(shù)是源,第二個參數(shù)是目標。 無需捕獲異常 | 第一個參數(shù)是目標,第二個參數(shù)是源。 必須捕獲異常 |
| 深/淺拷貝 | 淺拷貝 | 淺拷貝 |
Apache的BeanUtils方法。(依賴:maven里直接搜“commons-beanutils”)
| 方法 | 說明 |
|---|---|
| BeanUtils.copyProperties(Object target, Object source); | source對應的對象成員賦值給target對應的對象成員 |
| BeanUtils.copyProperties(Object target, String name, Object source); | 只拷貝某些字段 |
Apache的BeanUtils支持的類型轉(zhuǎn)換
- java.lang.BigDecimal
- java.lang.BigInteger
- boolean and java.lang.Boolean
- byte and java.lang.Byte
- char and java.lang.Character
- java.lang.Class
- double and java.lang.Double
- float and java.lang.Float
- int and java.lang.Integer
- long and java.lang.Long
- short and java.lang.Short
- java.lang.String
- java.sql.Date
- java.sql.Time
- java.sql.Timestamp
java.util.Date是不被支持的,而它的子類java.sql.Date是被支持的。因此如果對象包含時間類型的屬性,且希望被轉(zhuǎn)換的時候,一定要使用java.sql.Date類型。否則在轉(zhuǎn)換時會提示argument mistype異常。
實例
entity
package com.example.entity;
import lombok.Data;
@Data
public class Question {
private Integer id;
private Integer studentId;
private String content;
private Integer value;
}
package com.example.entity;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private Integer points;
}
vo
package com.example.vo;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class QuestionStudentVO implements Serializable {
private Integer id;
private String content;
private Integer value;
private Integer studentId;
private List<String> name;
private Integer points;
}
測試類
package com.example;
import com.example.entity.Question;
import com.example.entity.Student;
import com.example.vo.QuestionStudentVO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void beanUtilTest(){
Question question = new Question();
question.setId(2);
// question.setStudentId(3);
question.setContent("This is content");
question.setValue(100);
Student student = new Student();
student.setId(3);
student.setName("Tony Stark");
student.setPoints(201);
QuestionStudentVO questionStudentVO = new QuestionStudentVO();
BeanUtils.copyProperties(question, questionStudentVO);
BeanUtils.copyProperties(student, questionStudentVO);
System.out.println(questionStudentVO);
}
}
執(zhí)行結(jié)果
QuestionStudentVO(id=3, content=This is content, value=100, studentId=null, name=null, points=201)
到此這篇關(guān)于詳解Spring中BeanUtils工具類的使用的文章就介紹到這了,更多相關(guān)Spring BeanUtils工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java并發(fā)系列之JUC中的Lock鎖與synchronized同步代碼塊問題
這篇文章主要介紹了Java并發(fā)系列之JUC中的Lock鎖與synchronized同步代碼塊,簡單介紹了lock鎖及鎖的底層知識,結(jié)合案例給大家介紹的非常詳細,需要的朋友可以參考下2022-04-04
零基礎(chǔ)入門學習——Spring Boot注解(一)
這篇文章主要介紹了Spring Boot注解學習(一)要點,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-05-05
Java的JDBC中Statement與CallableStatement對象實例
這篇文章主要介紹了Java的JDBC中Statement與CallableStatement對象實例,JDBC是Java編程中用于操作數(shù)據(jù)庫的API,需要的朋友可以參考下2015-12-12
SpringBoot?AOP?Redis實現(xiàn)延時雙刪功能實戰(zhàn)
本文主要介紹了SpringBoot?AOP?Redis實現(xiàn)延時雙刪功能實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Java中十六進制和十進制之間互相轉(zhuǎn)換代碼示例
這篇文章主要給大家介紹了關(guān)于Java中十六進制和十進制之間互相轉(zhuǎn)換的相關(guān)資料,我們項目過程中總是要用到十進制與十六進制相互轉(zhuǎn)換的方法,需要的朋友可以參考下2023-07-07

