原文网址:Spring--BeanUtils工具类--使用/实例_IT利刃出鞘的博客-CSDN博客

简介

说明

        本文介绍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方法注意事项

  1. BeanUtils是浅拷贝
  2. 泛型只在编译期起作用,不能依靠泛型来做运行期的限制;

浅拷贝和深拷贝

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

Spring的BeanUtils与Apache的BeanUtils区别

Spring的BeanUtils Apache的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 {@Testpublic 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工具类--使用/实例相关推荐

  1. BeanUtils工具类,简化数据封装

     login.html中form表单的action路径的写法         * 虚拟目录+Servlet的资源路径 BeanUtils工具类,简化数据封装         * 用于封装JavaBea ...

  2. BeanUtils工具类

    BeanUtils工具类,简化数据封装 用于封装JavaBean的 JavaBean:标准的Java类 要求: 1. 类必须被public修饰 2. 必须提供空参的构造器 3. 成员变量必须使用pri ...

  3. Spring Redis工具类

    /*** spring redis 工具类**/ @Component @SuppressWarnings(value = { "unchecked", "rawtype ...

  4. Spring 计时工具类 StopWatch

    Spring 计时工具类 StopWatch 位于Spring的核心包 设计目标 验证程序性能, 记录过程耗时 隐藏System.currentTimeMillis()的使用提升代码可读性 特性 支持 ...

  5. Java实现代码计时功能(Spring计时工具类--StopWatch学习总结)

    使用场景 计算某段程序的执行时间.计算每段线程所耗时间.计算方法调用的执行时间,等等. 传统方式 使用java代码实现计时功能 long startTime = System.currentTimeM ...

  6. spring的beanutils工具类_基于spring-beans实现工具类BeanUtils基于Class实例化注入对象及查找方法、复制属性等操作...

    一.前言 基于spring-beans(4.1.4)的工具类org.springframework.beans.BeanUtils对注入spring对象按照Class实例化instantiateCla ...

  7. beanutils工具类_16 个超级实用的 Java 工具类!

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  8. beanutils工具类_Apache Commons 工具类介绍及简单使用

    来源:http://h5ip.cn/9xu3 Apache Commons 工具类大家都有用过,但是可能缺乏系统学习,只用到了一小部分功能,无法发挥极限的价值,肥朝用大白话说就是,一颗好白菜都让猪给拱 ...

  9. Spring的工具类,方便在非spring管理环境中获取bean

    场景 在SpringBoot的后台项目中,如果想要引入并且调用某个bean,可以直接通过注解的方式. 比如在单元测试中引入某业务的Controller @RunWith(SpringJUnit4Cla ...

最新文章

  1. 机器学习入门(12)— 激活函数层 ReLU、Sigmoid 层的实现
  2. 【python】一次移动平均算法
  3. fsum函数测试以及分析
  4. golang error信息 转 字符串 x := fmt.Sprintf(“%s“, err)
  5. 关于“习惯”的精彩分析
  6. flink on yarn模式下释放flink占用yarn的资源
  7. css --- 兄弟选择器
  8. java异常—— finally 子句+带资源的 try语句
  9. 分享一个文件上传工具类
  10. else应输入一个语句是什么意思_Python学习基础篇 -4: Python中的转弯---分支语句
  11. postman-SSL证书问题-支持HTTPS请求
  12. opencv 环境相关
  13. GitHub GraphQL API已正式可用
  14. qt怎么连接oracle,Qt连接Oracle数据库详细介绍(QOCI)
  15. java将明文变为密文,使用java编程实现明文和密文之间的互转
  16. 关于ZETag云标签你了解多少?
  17. matlab 表示希腊字母yita,常用希腊字母读法
  18. Windows XP自动登录
  19. TL431驱动三线PT100热电阻电路设计(转)
  20. 阿里云发邮件遇到的问题

热门文章

  1. Struts 2.0相关知识 摘自Struts2权威指南(李刚 著)
  2. Python由来以及用途
  3. ora-04030 进程内存不足解决方案
  4. 聊聊WPF中字体的设置
  5. 基于JAVA+SpringMVC+Mybatis+Vue+MYSQL的高校企业员工公寓后勤管理系统
  6. Boolean Satisfiability Problem(SAT)问题介绍
  7. 搜狗词库scel格式转为txt格式(python3版本)
  8. 微信小程序 - 跨域问题
  9. 2PC与Seata简介
  10. 【金万维】金万维天联高级版引入文件的时候看不到本地磁盘