詳解Spring中BeanUtils工具類的使用
簡介
說明
本文介紹Spring的BeanUtils工具類的用法。
我們經(jīng)常需要將不同的兩個對象實(shí)例進(jìn)行屬性復(fù)制,比如將DO對象進(jìn)行屬性復(fù)制到DTO,這種轉(zhuǎn)換最原始的方式就是手動編寫大量的 get/set代碼,很繁瑣。為了解決這一痛點(diǎn),就誕生了一些方便的類庫,常用的有 Apache的 BeanUtils,Spring的 BeanUtils, Dozer,Orika等拷貝工具。
由于Apache的BeanUtils的性能很差,強(qiáng)烈不建議使用。阿里巴巴Java開發(fā)規(guī)約插件上也明確指出:
“Ali-Check | 避免用Apache Beanutils進(jìn)行屬性的copy。”
Spring的BeanUtils方法
| 方法 | 說明 |
|---|---|
| BeanUtils.copyProperties(source, target); | source對應(yīng)的對象成員賦值給target對應(yīng)的對象成員 |
| BeanUtils.copyProperties(source, target, "id", "time"); | 忽略拷貝某些字段。本處是忽略:id與time |
Spring的BeanUtils方法注意事項(xiàng)
泛型只在編譯期起作用,不能依靠泛型來做運(yùn)行期的限制;
淺拷貝和深拷貝
淺拷貝:對基本數(shù)據(jù)類型進(jìn)行值傳遞,對引用數(shù)據(jù)類型進(jìn)行引用傳遞般的拷貝,此為淺拷貝
深拷貝:對基本數(shù)據(jù)類型進(jìn)行值傳遞,對引用數(shù)據(jù)類型,創(chuàng)建一個新的對象,并復(fù)制其內(nèi)容,此為深拷貝。
Spring的BeanUtils與Apache的BeanUtils區(qū)別
| 項(xiàng) | Spring的BeanUtils | Apache的BeanUtils |
|---|---|---|
| 性能 | 好 原因:對兩個對象中相同名字的屬性進(jìn)行簡單的get/set,僅檢查屬性的可訪問性 | 差 原因:有很多檢驗(yàn):類型的轉(zhuǎn)換、對象所屬類的可訪問性等 注意:本處類型轉(zhuǎn)換是類似的類,多個String轉(zhuǎn)為List<String>是不行的。 |
| 使用方面 | 第一個參數(shù)是源,第二個參數(shù)是目標(biāo)。 無需捕獲異常 | 第一個參數(shù)是目標(biāo),第二個參數(shù)是源。 必須捕獲異常 |
| 深/淺拷貝 | 淺拷貝 | 淺拷貝 |
Apache的BeanUtils方法。(依賴:maven里直接搜“commons-beanutils”)
| 方法 | 說明 |
|---|---|
| BeanUtils.copyProperties(Object target, Object source); | source對應(yīng)的對象成員賦值給target對應(yīng)的對象成員 |
| 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異常。
實(shí)例
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)文章
SpringCloud中的熔斷監(jiān)控HystrixDashboard和Turbine示例詳解
HystrixDashboard是用于實(shí)時監(jiān)控Hystrix性能的工具,展示請求響應(yīng)時間和成功率等數(shù)據(jù),本文介紹了如何配置和使用HystrixDashboard和Turbine進(jìn)行熔斷監(jiān)控,包括依賴添加、啟動類配置和測試流程,感興趣的朋友一起看看吧2024-09-09
SpringBoot整合rabbitMq自定義消息轉(zhuǎn)換方式
這篇文章主要介紹了SpringBoot整合rabbitMq自定義消息轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
多模塊maven的deploy集成gitlab?ci自動發(fā)版配置
這篇文章主要為大家介紹了多模塊maven項(xiàng)目deploy集成gitlab?ci自動發(fā)版的配置流程步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
在MyBatis的XML映射文件中<trim>元素所有場景下的完整使用示例代碼
在MyBatis的XML映射文件中,<trim>元素用于動態(tài)添加SQL語句的一部分,處理前綴、后綴及多余的逗號或連接符,示例展示了如何在UPDATE、SELECT、INSERT和SQL片段中使用<trim>元素,以實(shí)現(xiàn)動態(tài)的SQL構(gòu)建,感興趣的朋友一起看看吧2025-01-01
SpringBoot整合MybatisPlus實(shí)現(xiàn)增刪改查功能
MybatisPlus是國產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動。本文將整合MybatisPlus實(shí)現(xiàn)增刪改查功能,感興趣的可以了解一下2022-05-05

