简体   繁体   English

如何在Java中将子集的属性子集从Bean A复制到Bean B?

[英]How to copy a subset of properties from Bean A to Bean B in Java?

Essentially what I am looking for is an easy method like: 本质上,我正在寻找一种简单的方法,例如:

BeanUtils.copyProperties(dest, orig);

This comes from apache but copies all properties from the origin to the destination. 这来自apache,但是将所有属性从起点复制到目的地。 I need something that will only copy a certain subset of properties... something like the following 我需要只复制某些属性子集的东西...类似下面的东西

String[] propertyNamesToCopy = {"firstName", "lastName"};
BeanUtils.copyProperties(dest, orig, propertyNamesToCopy);

Any suggestions? 有什么建议么?

You can use copyProperty from BeanUtils for copying a single property. 您可以使用BeanUtils的copyProperty复制单个属性。 Just loop over your properties and use it. 只需循环使用属性即可。 You can extract it into a method. 您可以将其提取为方法。

You could do it with reflection. 您可以通过反射来实现。

public void copyProperties(Object orig, Object dest, String[] props){

    Class<?> class = orig.getClass().getFields();
    for(String fieldName : props){
       Field field = class.getField( fieldName );
       field.set(dest, field.get(orig));
    }
}

I didn't try it out, and you might have several problems with this method. 我没有尝试过,这种方法可能会导致一些问题。

First you need to add try/catch in case the field does not exist. 首先,您需要添加try / catch,以防该字段不存在。 Then it only works for public fields. 然后,它仅适用于public领域。 Also, the get() method form Field returns only objects and I don't know if it will autobox/unbox them or not which might generate runtimes erros and force you to use getInt() , etc... depending on FieldType (you can get it with field.getGenericType() ). 另外,表单Fieldget()方法仅返回对象,我不知道它是否会自动装箱/拆箱,这可能会生成运行时错误并迫使您使用getInt()等,具体取决于FieldType (可以通过field.getGenericType()获得它。 You might see getDeclaredField() which get all the field (even privates) but only for the class you have, not the field inherited from parents. 您可能会看到getDeclaredField() ,它获取所有字段(甚至私有),但仅针对您拥有的类,而不是从父项继承的字段。

If you want to access private field, either set them to public using reflection ( field.setaccessible(true) if I am correct) or access them through their getters and setters (also using reflection): 如果要访问私有字段,请使用反射将它们设置为公共field.setaccessible(true)如果我正确的话,请使用field.setaccessible(true) ),也可以通过它们的getter和setter访问它们(也可以使用反射):

Method get = class.getMethod("get"+fieldNameWithCaps); 
Object newValue = get.invoke(orig)
Method set = class.getMethod("set"+fieldNameWithCaps, newValue.getClass()); 
set.invoke(dest, newValue);

Still not sure here as I don't know what newValue.getClass() will return, but you get the idea. 仍然不确定此处,因为我不知道newValue.getClass()将返回什么,但是您明白了。 Try looking around reflection, but be carefull, it's quite slow and can be messy. 尝试环顾四周的反射,但要小心,它的速度很慢,可能会很杂乱。

The BeanUtils class from Spring provides essentially the inverse of what I was looking for. Spring的BeanUtils类实质上提供了我所寻找的逆。 So I take the list of all properties - list of properties to copy to get the ignore list but it works :-) 因此,我获取所有属性的列表-要复制的属性列表以获取忽略列表,但它可以正常工作:-)

org.springframework.beans.BeanUtils.copyProperties(Object source, Object target, String... ignoreProperties)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM