Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy specific fields by using BeanUtils.copyProperties?

springframework.beans.BeanUtils is very useful to copy objects, and I use the "ignoreProperties" option frequently. However, sometimes I want to copy only specific objects (basically, the opposite of "ignore Properties"). Does anyone know how can I do that? Any help will be appreciated.

import org.springframework.beans.BeanUtils;  public class Sample {         public static void main(String[] args) {             DemoADto demoADto = new DemoADto();         demoADto.setName("Name of Demo A");         demoADto.setAddress("Address of Demo A");          DemoBDto demoBDto = new DemoBDto();          // This is "ignoreProperties" option         // But I want to know how I can copy only name field by using BeanUtils or something.         BeanUtils.copyProperties(demoADto, demoBDto, new String[] {"address"});          System.out.println(demoBDto.getName());         System.out.println(demoBDto.getAddress());         }     }  public class DemoADto {         private String name;         private String address;      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }      public String getAddress() {         return address;     }      public void setAddress(String address) {         this.address = address;     }     }  public class DemoBDto {         private String name;         private String address;      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }      public String getAddress() {         return address;     }      public void setAddress(String address) {         this.address = address;     }     } 
like image 936
zono Avatar asked Feb 22 '11 14:02

zono


People also ask

How do you use copyProperties with BeanUtils?

BeanUtils class provides a copyProperties method that copies the properties of source object to target object where the property name is same in both objects. Now we will copy the properties of Course object to CourseEntity object: Course course = new Course(); course. setName("Computer Science"); course.

Does BeanUtils copyProperties do deep copy?

But BeanUtils. copyProperties does not perform a deep copy of fields and each level has to be created and separately copied.

What is Commons BeanUtils used for?

Commons BeanUtils is a collection of utilities that makes working with beans and bean properties much easier. This project contains utilities that allow one to retrieve a bean property by name, sort beans by a property, translate beans to maps, and more.

How do I copy one bean to another bean?

Use PropertyUtils. copyProperties() to copy the properties from one bean to another. The first parameter is the destination bean, and the second parameter is the bean to copy properties from: import org.


2 Answers

You can use the BeanWrapper technology. Here's a sample implementation:

public static void copyProperties(Object src, Object trg, Iterable<String> props) {      BeanWrapper srcWrap = PropertyAccessorFactory.forBeanPropertyAccess(src);     BeanWrapper trgWrap = PropertyAccessorFactory.forBeanPropertyAccess(trg);      props.forEach(p -> trgWrap.setPropertyValue(p, srcWrap.getPropertyValue(p)));  } 

Or, if you really, really want to use BeanUtils, here's a solution. Invert the logic, gather excludes by comparing the full property list with the includes:

public static void copyProperties2(Object src, Object trg, Set<String> props) {     String[] excludedProperties =              Arrays.stream(BeanUtils.getPropertyDescriptors(src.getClass()))                   .map(PropertyDescriptor::getName)                   .filter(name -> !props.contains(name))                   .toArray(String[]::new);      BeanUtils.copyProperties(src, trg, excludedProperties); } 
like image 93
Sean Patrick Floyd Avatar answered Sep 21 '22 17:09

Sean Patrick Floyd


If you don't want to use Commons BeanUtils you can still use Spring by using the BeanWrapper.

You will have to manually loop through all the properties so you will want to make a static helper method.

You can always just copy what copyProperties is doing and adjust to your liking: http://tinyurl.com/BeanUtils-copyProperties

like image 29
Adam Gent Avatar answered Sep 25 '22 17:09

Adam Gent