如何将属性从一个Java bean复制到另一个Java bean? [英] How to copy properties from one Java bean to another?

查看:78
本文介绍了如何将属性从一个Java bean复制到另一个Java bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Java POJO,可以将属性复制到同一POJO类的另一个实例.

我知道我可以使用BeanUtils.copyProperties()做到这一点,但我想避免使用第三方库.

那么,如何简单,正确,安全地做到这一点呢?

顺便说一句,我正在使用Java 6.

解决方案

我想如果您看一下BeanUtils的源代码,它将向您展示如何在不实际使用BeanUtils的情况下做到这一点.

如果只想创建一个POJO的副本(与将属性从一个POJO复制到另一个POJO不太一样),则可以更改源bean,以实现clone()方法和Cloneable接口. /p>

I have a simple Java POJO that I would copy properties to another instance of same POJO class.

I know I can do that with BeanUtils.copyProperties() but I would like to avoid use of a third-party library.

So, how to do that simply, the proper and safer way ?

By the way, I'm using Java 6.

解决方案

I guess if you look at the source code of BeanUtils, it will show you how to do this without actually using BeanUtils.

If you simply want to create a copy of a POJO (not quite the same thing as copying the properties from one POJO to another), you could change the source bean to implement the clone() method and the Cloneable interface.

这篇关于如何将属性从一个Java bean复制到另一个Java bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆