详解Spring中BeanUtils工具类的使用

 更新时间:2022年06月29日 08:24:47   作者:IT利刃出鞘  
这篇文章主要通过一些示例为大家详细介绍了Spring中BeanUtils工具类的使用,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下

简介

说明

本文介绍Spring的BeanUtils工具类的用法。

我们经常需要将不同的两个对象实例进行属性复制,比如将DO对象进行属性复制到DTO,这种转换最原始的方式就是手动编写大量的 get/set代码,很繁琐。为了解决这一痛点,就诞生了一些方便的类库,常用的有 Apache的 BeanUtils,Spring的 BeanUtils, Dozer,Orika等拷贝工具。

由于Apache的BeanUtils的性能很差,强烈不建议使用。阿里巴巴Java开发规约插件上也明确指出:
“Ali-Check | 避免用Apache Beanutils进行属性的copy。”

Spring的BeanUtils方法

方法说明
BeanUtils.copyProperties(source, target);source对应的对象成员赋值给target对应的对象成员
BeanUtils.copyProperties(source, target, "id", "time");忽略拷贝某些字段。本处是忽略:id与time

Spring的BeanUtils方法注意事项 

泛型只在编译期起作用,不能依靠泛型来做运行期的限制; 

浅拷贝和深拷贝

浅拷贝:对基本数据类型进行值传递,对引用数据类型进行引用传递般的拷贝,此为浅拷贝

深拷贝:对基本数据类型进行值传递,对引用数据类型,创建一个新的对象,并复制其内容,此为深拷贝。

Spring的BeanUtils与Apache的BeanUtils区别

Spring的BeanUtilsApache的BeanUtils
性能
原因:对两个对象中相同名字的属性进行简单的get/set,仅检查属性的可访问性

原因:有很多检验:类型的转换、对象所属类的可访问性等
注意:本处类型转换是类似的类,多个String转为List<String>是不行的。
使用方面第一个参数是源,第二个参数是目标。
无需捕获异常
第一个参数是目标,第二个参数是源。
必须捕获异常
深/浅拷贝浅拷贝浅拷贝

Apache的BeanUtils方法。(依赖:maven里直接搜“commons-beanutils”)

方法说明
BeanUtils.copyProperties(Object target, Object source);source对应的对象成员赋值给target对应的对象成员
BeanUtils.copyProperties(Object target, String name, Object source); 只拷贝某些字段

Apache的BeanUtils支持的类型转换

  1. java.lang.BigDecimal
  2. java.lang.BigInteger
  3. boolean and java.lang.Boolean
  4. byte and java.lang.Byte
  5. char and java.lang.Character
  6. java.lang.Class
  7. double and java.lang.Double
  8. float and java.lang.Float
  9. int and java.lang.Integer
  10. long and java.lang.Long
  11. short and java.lang.Short
  12. java.lang.String
  13. java.sql.Date
  14. java.sql.Time
  15. java.sql.Timestamp

java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用java.sql.Date类型。否则在转换时会提示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);
    }
}

执行结果

QuestionStudentVO(id=3, content=This is content, value=100, studentId=null, name=null, points=201)

到此这篇关于详解Spring中BeanUtils工具类的使用的文章就介绍到这了,更多相关Spring BeanUtils工具类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Quarkus中ConfigSourceInterceptor的加密配置实现

    Quarkus中ConfigSourceInterceptor的加密配置实现

    这篇文章主要为大家介绍Quarkus中ConfigSourceInterceptor加密配置的实现方式,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-02-02
  • Spring中Bean的单例和多例使用说明

    Spring中Bean的单例和多例使用说明

    这篇文章主要介绍了Spring中Bean的单例和多例使用说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • java对同一个文件进行读写操作方法

    java对同一个文件进行读写操作方法

    在本篇文章里我们给大家详细讲述了java对同一个文件进行读写操作的方法和知识点,需要的朋友们可以参考学习下。
    2018-10-10
  • SpringBoot中实现定时任务的几种方式

    SpringBoot中实现定时任务的几种方式

    定时任务在我们项目开发中也是很重要的,对于某些场景必须要用定时任务 ,如定时发送邮件啊,定时统计数据等,这篇文章主要讲讲项目中实现定时任务的几种方式,需要的朋友可以参考下
    2023-05-05
  • 详解Java前缀树Trie的原理及代码实现

    详解Java前缀树Trie的原理及代码实现

    Trie又被称为前缀树、字典树。Trie利用字符串的公共前缀来高效地存储和检索字符串数据集中的关键词,最大限度地减少无谓的字符串比较,其核心思想是用空间换时间。本文主要介绍了Trie的原理及实现,感兴趣的可以了解一下
    2022-11-11
  • 教你怎么使用Optional处理null

    教你怎么使用Optional处理null

    今天教各位小伙伴怎么使用Optional处理null,文中有非常详细的代码示例,对正在学习java的小伙伴们有很大的帮助,需要的朋友可以参考下
    2021-05-05
  • SpringBoot集成消息队列的项目实践

    SpringBoot集成消息队列的项目实践

    本文主要介绍了SpringBoot集成消息队列的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • SpringBoot整合UEditor的示例代码

    SpringBoot整合UEditor的示例代码

    本篇文章主要介绍了SpringBoot整合UEditor的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • SpringBoot项目中连接SQL Server的三种方式

    SpringBoot项目中连接SQL Server的三种方式

    连接SQL Server是许多Spring Boot项目中常见的需求之一,本文主要介绍了SpringBoot项目中连接SQL Server的三种方式,具有一定的参考价值 ,感兴趣的可以了解一下
    2023-09-09
  • Java split()方法中的特殊符号举例详解

    Java split()方法中的特殊符号举例详解

    Java中的split方法可以将一个字符串按照指定的分隔符进行分割,返回一个字符串数组,这篇文章主要给大家介绍了关于Java split()方法中的特殊符号的相关资料,需要的朋友可以参考下
    2023-07-07

最新评论