两难!到底用Apache BeanUtils还是Spring BeanUtils?

简单来说:

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

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

两难!到底用Apache BeanUtils还是Spring BeanUtils?

# BeanUtils

===================================================================================

前面简单讲了一下对象拷贝的一些知识,下面就来具体看下两种 BeanUtils 工具

# Apache 的 BeanUtils

============================================================================================

首先来看一个非常简单的BeanUtils的例子

publicclass PersonSource {

private Integer id;

private String username;

private String password;

private Integer age;

// getters/setters omiited

}

publicclass PersonDest {

private Integer id;

private String username;

private Integer age;

// getters/setters omiited

}

publicclass TestApacheBeanUtils {

public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {

//下面只是用于单独测试

PersonSource personSource = new PersonSource(1, “pjmike”, “12345”, 21);

PersonDest personDest = new PersonDest();

BeanUtils.copyProperties(personDest,personSource);

System.out.println("persondest: "+personDest);

}

}

persondest: PersonDest{id=1, username=‘pjmike’, age=21}

从上面的例子可以看出,对象拷贝非常简单,BeanUtils最常用的方法就是:

//将源对象中的值拷贝到目标对象//将源对象中的值拷贝到目标对象

public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

BeanUtilsBean.getInstance().copyProperties(dest, orig);

}

但是由于 Apache下的BeanUtils对象拷贝性能太差,不建议使用,而且在阿里巴巴Java开发规约插件上也明确指出:

Ali-Check | 避免用Apache Beanutils进行属性的copy。

commons-beantutils 对于对象拷贝加了很多的检验,包括类型的转换,甚至还会检验对象所属的类的可访问性,可谓相当复杂,这也造就了它的差劲的性能,具体实现代码如下:

public void copyProperties(final Object dest, final Object orig)

throws IllegalAccessException, InvocationTargetException {

// Validate existence of the specified beans

if (dest == null) {

thrownew IllegalArgumentException

(“No destination bean specified”);

}

if (orig == null) {

thrownew IllegalArgumentException(“No origin bean specified”);

}

if (log.isDebugEnabled()) {

log.debug(“BeanUtils.copyProperties(” + dest + ", " +

orig + “)”);

}

// Copy the properties, converting as necessary

if (orig instanceof DynaBean) {

final DynaProperty[] origDescriptors =

((DynaBean) orig).getDynaClass().getDynaProperties();

for (DynaProperty origDescriptor : origDescriptors) {

final String name = origDescriptor.getName();

// Need to check isReadable() for WrapDynaBean

// (see Jira issue# BEANUTILS-61)

if (getPropertyUtils().isReadable(orig, name) &&

getPropertyUtils().isWriteable(dest, name)) {

final Object value = ((DynaBean) orig).get(name);

copyProperty(dest, name, value);

}

}

} elseif (orig instanceof Map) {

@SuppressWarnings(“unchecked”)

final

// Map properties are always of type <String, Object>

Map<String, Object> propMap = (Map<String, Object>) orig;

for (final Map.Entry<String, Object> entry : propMap.entrySet()) {

final String name = entry.getKey();

if (getPropertyUtils().isWriteable(dest, name)) {

copyProperty(dest, name, entry.getValue());

}

}

} else/* if (orig is a standard JavaBean) */ {

final PropertyDescriptor[] origDescriptors =

getPropertyUtils().getPropertyDescriptors(orig);

for (PropertyDescriptor origDescriptor : origDescriptors) {

final String name = origDescriptor.getName();

if (“class”.equals(name)) {

continue; // No point in trying to set an object’s class

}

if (getPropertyUtils().isReadable(orig, name) &&

getPropertyUtils().isWriteable(dest, name)) {

try {

final Object value =

getPropertyUtils().getSimpleProperty(orig, name);

copyProperty(dest, name, value);

} catch (final NoSuchMethodException e) {

// Should not happen

}

}

}

}

}

# Spring 的 BeanUtils

============================================================================================

使用spring的BeanUtils进行对象拷贝:

publicclass TestSpringBeanUtils {

public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {

//下面只是用于单独测试

PersonSource personSource = new PersonSource(1, “pjmike”, “12345”, 21);

PersonDest personDest = new PersonDest();

BeanUtils.copyProperties(personSource,personDest);

System.out.println("persondest: "+personDest);

}

}

Spring下的BeanUtils也是使用 copyProperties方法进行拷贝,只不过它的实现方式非常简单,就是对两个对象中相同名字的属性进行简单的get/set,仅检查属性的可访问性。具体实现如下:

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
转存中…(img-wxYX4i4t-1715197800299)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值