克隆具有少量属性值的 Java bean - BeanUtils

发布于 2024-12-14 13:15:19 字数 395 浏览 5 评论 0原文

我有一个 Java bean,我想克隆它以获得一个新实例,但在新实例中,我希望少数属性具有值,其他属性我不想被克隆/复制。

示例:-

Source:- Class ABC, Object src --> field1 = 10, field2 =5, field3 = 6, field4 = 20
Target:- Class ABC, Oject clone --> field1 = 10, field2 = 5, field3 = null, field4 = null

我也可以使用 copyProperties,但是我必须创建一个类似但较小的类结构。

我也可以使用copyProperty,但是我必须多次编写它。

有没有好的解决办法呢。

I am having a Java bean, which i want to clone it to get a new instance, but in the new instance, I want few of the properties to have values, others i don't want to be cloned/copied.

Example:-

Source:- Class ABC, Object src --> field1 = 10, field2 =5, field3 = 6, field4 = 20
Target:- Class ABC, Oject clone --> field1 = 10, field2 = 5, field3 = null, field4 = null

I can also use copyProperties but than, I have to create a similar but small class structure.

I can also use copyProperty but then, I have to write it multiple times.

Is there a good solution to this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2024-12-21 13:15:19

使用这个方法
copyProperties(对象 dest,对象 orig)
对于属性名称相同的所有情况,将属性值从源 bean 复制到目标 bean。

你已经提到你也在做同样的事情。你可以去看文章。我总是使用我的 spring web-app 可能对你也有帮助。如果它解决了你的问题,请发表评论。

http://java.dzone.com/articles/how-copy-bean-properties

Use this method
copyProperties(Object dest, Object orig)
Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

You have mentioned you are doing same.May you go to article.I always using for my spring web-app may be it also helpful to you.Comment if it solves your problem.

http://java.dzone.com/articles/how-copy-bean-properties

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文