詳解Spring中BeanUtils工具類的使用
簡(jiǎn)介
說(shuō)明
本文介紹Spring的BeanUtils工具類的用法。
我們經(jīng)常需要將不同的兩個(gè)對(duì)象實(shí)例進(jìn)行屬性復(fù)制,比如將DO對(duì)象進(jìn)行屬性復(fù)制到DTO,這種轉(zhuǎn)換最原始的方式就是手動(dòng)編寫大量的 get/set代碼,很繁瑣。為了解決這一痛點(diǎn),就誕生了一些方便的類庫(kù),常用的有 Apache的 BeanUtils,Spring的 BeanUtils, Dozer,Orika等拷貝工具。
由于Apache的BeanUtils的性能很差,強(qiáng)烈不建議使用。阿里巴巴Java開發(fā)規(guī)約插件上也明確指出:
“Ali-Check | 避免用Apache Beanutils進(jìn)行屬性的copy。”
Spring的BeanUtils方法
| 方法 | 說(shuō)明 |
|---|---|
| BeanUtils.copyProperties(source, target); | source對(duì)應(yīng)的對(duì)象成員賦值給target對(duì)應(yīng)的對(duì)象成員 |
| BeanUtils.copyProperties(source, target, "id", "time"); | 忽略拷貝某些字段。本處是忽略:id與time |
Spring的BeanUtils方法注意事項(xiàng)
泛型只在編譯期起作用,不能依靠泛型來(lái)做運(yùn)行期的限制;
淺拷貝和深拷貝
淺拷貝:對(duì)基本數(shù)據(jù)類型進(jìn)行值傳遞,對(duì)引用數(shù)據(jù)類型進(jìn)行引用傳遞般的拷貝,此為淺拷貝
深拷貝:對(duì)基本數(shù)據(jù)類型進(jìn)行值傳遞,對(duì)引用數(shù)據(jù)類型,創(chuàng)建一個(gè)新的對(duì)象,并復(fù)制其內(nèi)容,此為深拷貝。
Spring的BeanUtils與Apache的BeanUtils區(qū)別
| 項(xiàng) | Spring的BeanUtils | Apache的BeanUtils |
|---|---|---|
| 性能 | 好 原因:對(duì)兩個(gè)對(duì)象中相同名字的屬性進(jìn)行簡(jiǎn)單的get/set,僅檢查屬性的可訪問(wèn)性 | 差 原因:有很多檢驗(yàn):類型的轉(zhuǎn)換、對(duì)象所屬類的可訪問(wèn)性等 注意:本處類型轉(zhuǎn)換是類似的類,多個(gè)String轉(zhuǎn)為L(zhǎng)ist<String>是不行的。 |
| 使用方面 | 第一個(gè)參數(shù)是源,第二個(gè)參數(shù)是目標(biāo)。 無(wú)需捕獲異常 | 第一個(gè)參數(shù)是目標(biāo),第二個(gè)參數(shù)是源。 必須捕獲異常 |
| 深/淺拷貝 | 淺拷貝 | 淺拷貝 |
Apache的BeanUtils方法。(依賴:maven里直接搜“commons-beanutils”)
| 方法 | 說(shuō)明 |
|---|---|
| BeanUtils.copyProperties(Object target, Object source); | source對(duì)應(yīng)的對(duì)象成員賦值給target對(duì)應(yīng)的對(duì)象成員 |
| 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是被支持的。因此如果對(duì)象包含時(shí)間類型的屬性,且希望被轉(zhuǎn)換的時(shí)候,一定要使用java.sql.Date類型。否則在轉(zhuǎn)換時(shí)會(huì)提示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;
}
測(cè)試類
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java并發(fā)系列之JUC中的Lock鎖與synchronized同步代碼塊問(wèn)題
這篇文章主要介紹了Java并發(fā)系列之JUC中的Lock鎖與synchronized同步代碼塊,簡(jiǎn)單介紹了lock鎖及鎖的底層知識(shí),結(jié)合案例給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
零基礎(chǔ)入門學(xué)習(xí)——Spring Boot注解(一)
這篇文章主要介紹了Spring Boot注解學(xué)習(xí)(一)要點(diǎn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-05-05
Java的JDBC中Statement與CallableStatement對(duì)象實(shí)例
這篇文章主要介紹了Java的JDBC中Statement與CallableStatement對(duì)象實(shí)例,JDBC是Java編程中用于操作數(shù)據(jù)庫(kù)的API,需要的朋友可以參考下2015-12-12
maven引入kabeja依賴的實(shí)現(xiàn)步驟
本文主要介紹了maven引入kabeja依賴的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn)
本文主要介紹了SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java中十六進(jìn)制和十進(jìn)制之間互相轉(zhuǎn)換代碼示例
這篇文章主要給大家介紹了關(guān)于Java中十六進(jìn)制和十進(jìn)制之間互相轉(zhuǎn)換的相關(guān)資料,我們項(xiàng)目過(guò)程中總是要用到十進(jìn)制與十六進(jìn)制相互轉(zhuǎn)換的方法,需要的朋友可以參考下2023-07-07

