Spring--BeanUtils工具类--使用/实例
原文网址: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方法注意事项
- BeanUtils是浅拷贝。
- 泛型只在编译期起作用,不能依靠泛型来做运行期的限制;
浅拷贝和深拷贝
- 浅拷贝:对基本数据类型进行值传递,对引用数据类型,使用其引用地址,不拷贝其内容,此为浅拷贝
- 深拷贝:对基本数据类型进行值传递,对引用数据类型,创建一个新的对象,并复制其内容,此为深拷贝。
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支持的类型转换
- 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是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用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工具类--使用/实例相关推荐
- BeanUtils工具类,简化数据封装
login.html中form表单的action路径的写法 * 虚拟目录+Servlet的资源路径 BeanUtils工具类,简化数据封装 * 用于封装JavaBea ...
- BeanUtils工具类
BeanUtils工具类,简化数据封装 用于封装JavaBean的 JavaBean:标准的Java类 要求: 1. 类必须被public修饰 2. 必须提供空参的构造器 3. 成员变量必须使用pri ...
- Spring Redis工具类
/*** spring redis 工具类**/ @Component @SuppressWarnings(value = { "unchecked", "rawtype ...
- Spring 计时工具类 StopWatch
Spring 计时工具类 StopWatch 位于Spring的核心包 设计目标 验证程序性能, 记录过程耗时 隐藏System.currentTimeMillis()的使用提升代码可读性 特性 支持 ...
- Java实现代码计时功能(Spring计时工具类--StopWatch学习总结)
使用场景 计算某段程序的执行时间.计算每段线程所耗时间.计算方法调用的执行时间,等等. 传统方式 使用java代码实现计时功能 long startTime = System.currentTimeM ...
- spring的beanutils工具类_基于spring-beans实现工具类BeanUtils基于Class实例化注入对象及查找方法、复制属性等操作...
一.前言 基于spring-beans(4.1.4)的工具类org.springframework.beans.BeanUtils对注入spring对象按照Class实例化instantiateCla ...
- beanutils工具类_16 个超级实用的 Java 工具类!
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- beanutils工具类_Apache Commons 工具类介绍及简单使用
来源:http://h5ip.cn/9xu3 Apache Commons 工具类大家都有用过,但是可能缺乏系统学习,只用到了一小部分功能,无法发挥极限的价值,肥朝用大白话说就是,一颗好白菜都让猪给拱 ...
- Spring的工具类,方便在非spring管理环境中获取bean
场景 在SpringBoot的后台项目中,如果想要引入并且调用某个bean,可以直接通过注解的方式. 比如在单元测试中引入某业务的Controller @RunWith(SpringJUnit4Cla ...
最新文章
- 机器学习入门(12)— 激活函数层 ReLU、Sigmoid 层的实现
- 【python】一次移动平均算法
- fsum函数测试以及分析
- golang error信息 转 字符串 x := fmt.Sprintf(“%s“, err)
- 关于“习惯”的精彩分析
- flink on yarn模式下释放flink占用yarn的资源
- css --- 兄弟选择器
- java异常—— finally 子句+带资源的 try语句
- 分享一个文件上传工具类
- else应输入一个语句是什么意思_Python学习基础篇 -4: Python中的转弯---分支语句
- postman-SSL证书问题-支持HTTPS请求
- opencv 环境相关
- GitHub GraphQL API已正式可用
- qt怎么连接oracle,Qt连接Oracle数据库详细介绍(QOCI)
- java将明文变为密文,使用java编程实现明文和密文之间的互转
- 关于ZETag云标签你了解多少?
- matlab 表示希腊字母yita,常用希腊字母读法
- Windows XP自动登录
- TL431驱动三线PT100热电阻电路设计(转)
- 阿里云发邮件遇到的问题