JAVA SpringUtil.copyProperties和BeanUtils.copyProperties效率

本文探讨了Spring中使用SpringUtil和BeanUtils进行属性复制的方法。分析了两种方式在处理泛型和父类属性时的表现,指出SpringUtil在无泛型问题时更优,而BeanUtils能避免泛型导致的复制失败。
部署运行你感兴趣的模型镜像

(1):SpringUtil.copyProperties(class1,class2);// id属性泛型复制不到,由于继承的父类属性是泛型。
(2):BeanUtils.copyProperties(class1,class2);// 此方式效率极慢,spring的copyproties稍可接受,get,set几乎1ms

第一种方法,如果复制的类继承了父类,父类的ID属性是泛型并且序列化了会导致复制失败(详情可以debug跟代码);
第二种方法:没有遇到上述问题。

如果复制需要的属性不存在泛型问题,建议使用第一个,第二个所消耗的时间稍长。

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

根据提供的引用内容,可以得知BeanUtils.copyProperties()方法存在于两个不同的类中,分别为org.springframework.beans.BeanUtilsorg.apache.commons.beanutils.BeanUtils。这两个类的copyProperties()方法传递参数的赋值是相反的,即在org.springframework.beans.BeanUtils中,copyProperties(A,B)的结果是将A拷贝到B;而在org.apache.commons.beanutils.BeanUtils中,copyProperties(A,B)的结果是将B拷贝到A。 因此,如果需要使用BeanUtils.copyProperties()方法,需要先确定使用的是哪个类中的方法,并根据需要传递正确的参数。 下面是两个类中copyProperties()方法的使用示例: 1. org.springframework.beans.BeanUtils中的copyProperties()方法,将A对象的属性值拷贝到B对象中: ```python from myapp.models import A, B from django.shortcuts import get_object_or_404 from django.http import HttpResponse from org.springframework.beans import BeanUtils def my_view(request, pk): a = get_object_or_404(A, pk=pk) b = B() BeanUtils.copyProperties(a, b) b.save() return HttpResponse('Copy successful!') ``` 2. org.apache.commons.beanutils.BeanUtils中的copyProperties()方法,将B对象的属性值拷贝到A对象中: ```python from myapp.models import A, B from django.shortcuts import get_object_or_404 from django.http import HttpResponse from org.apache.commons.beanutils import BeanUtils def my_view(request, pk): a = A() b = get_object_or_404(B, pk=pk) BeanUtils.copyProperties(a, b) a.save() return HttpResponse('Copy successful!') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值