Spring Framework Using BeanUtils.copyProperties for the same bean type. package com.logicbig.example.beanutils;
import com.logicbig.example.TestBean; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl;
public class CopyPropsToSameType { public static void main (String[] args) { BeanWrapper bw = new BeanWrapperImpl(new TestBean()); bw.setPropertyValue("aString", "someString"); bw.setPropertyValue("anInt", 3);
TestBean testBean2 = new TestBean();
BeanUtils.copyProperties(bw.getWrappedInstance(), testBean2);
System.out.println(testBean2); } }
OutputTestBean{aString='someString', anInt=3, date=Mon May 01 16:08:07 CDT 2017}
BeanUtils#copyProperties for different beans type example. package com.logicbig.example.beanutils;
import com.logicbig.example.TestBean; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl;
public class CopyPropsToDifferentType { public static void main (String[] args) { BeanWrapper bw = new BeanWrapperImpl(new TestBean()); bw.setPropertyValue("aString", "someString"); bw.setPropertyValue("anInt", 3);
TestBeanDifferent testBean2 = new TestBeanDifferent();
//only properties of same name will be copied BeanUtils.copyProperties(bw.getWrappedInstance(), testBean2);
System.out.println(testBean2); }
private static class TestBeanDifferent { private String aString; private int differentInt;
public String getAString () { return aString; }
public void setAString (String aString) { this.aString = aString; }
public int getDifferentInt () { return differentInt; }
public void setDifferentInt (int differentInt) { this.differentInt = differentInt; }
@Override public String toString () { return "TestBeanDifferent{" + "aString='" + aString + '\'' + ", differentInt=" + differentInt + '}'; } } }
OutputTestBeanDifferent{aString='someString', differentInt=0}
Using BeanUtils.getPropertyDescriptors and comparing with Introspector alternative. package com.logicbig.example.beanutils;
import com.logicbig.example.TestBean; import org.springframework.beans.BeanUtils;
import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.util.Arrays;
public class PropDescriptorExample { public static void main (String[] args) throws IntrospectionException { PropertyDescriptor[] actual = Introspector.getBeanInfo(TestBean.class) .getPropertyDescriptors(); PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(TestBean.class);
System.out.println(Arrays.toString(actual)); System.out.println(Arrays.toString(descriptors)); } }
Output[java.beans.PropertyDescriptor[name=AString; propertyType=class java.lang.String; readMethod=public java.lang.String com.logicbig.example.TestBean.getAString(); writeMethod=public void com.logicbig.example.TestBean.setAString(java.lang.String)], java.beans.PropertyDescriptor[name=anInt; propertyType=int; readMethod=public int com.logicbig.example.TestBean.getAnInt(); writeMethod=public void com.logicbig.example.TestBean.setAnInt(int)], java.beans.PropertyDescriptor[name=class; propertyType=class java.lang.Class; readMethod=public final native java.lang.Class java.lang.Object.getClass()], java.beans.PropertyDescriptor[name=date; propertyType=class java.util.Date; readMethod=public java.util.Date com.logicbig.example.TestBean.getDate()]]
Using BeanUtils.resolveSignature example. Parsing string to do that. package com.logicbig.example.beanutils;
import org.springframework.beans.BeanUtils;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
public class ResolveSignature {
public static void main (String[] args) throws InvocationTargetException, IllegalAccessException { Method doSomething = BeanUtils.resolveSignature("aMethod(java.lang.String, int)", LocalBean.class); doSomething.invoke(new LocalBean(), "some string value", 100);
doSomething = BeanUtils.resolveSignature("aMethod(java.lang.Integer)", LocalBean.class); doSomething.invoke(new LocalBean(), 200); }
private static class LocalBean { public void aMethod (String str, int anInt) { System.out.println(str); System.out.println(anInt); }
public void aMethod (Integer anInt) { System.out.println(anInt); } } }
Outputsome string value 100 200
Using BeanUtils.resolveSignature example. Resolving method with no arguments. package com.logicbig.example.beanutils;
import org.springframework.beans.BeanUtils;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
public class ResolveSignature2 {
public static void main (String[] args) throws InvocationTargetException, IllegalAccessException { Method doSomething = BeanUtils.resolveSignature("doSomething", LocalBean.class); doSomething.invoke(new LocalBean()); }
private static class LocalBean { public void doSomething () { System.out.println("-- doing something -- ");
} } }
Output-- doing something --
Using BeanUtils.getPropertyDescriptors. package com.logicbig.example;
import org.springframework.beans.BeanUtils; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor;
public class BeanUtilPropDescriptorExample { public static void main (String[] args) throws IntrospectionException { PropertyDescriptor[] javaDescriptor = Introspector.getBeanInfo(Person.class) .getPropertyDescriptors(); System.out.println("-- jdk property descriptor --"); printPropertyDescriptor(javaDescriptor); PropertyDescriptor[] springDescriptor = BeanUtils.getPropertyDescriptors(Person.class); System.out.println("-- spring property descriptor --"); printPropertyDescriptor(springDescriptor);
}
private static void printPropertyDescriptor(PropertyDescriptor[] descriptors) { for (PropertyDescriptor descriptor : descriptors) { System.out.println("Name: "+descriptor.getName()); System.out.println("Reader: "+descriptor.getReadMethod()); System.out.println("Writer: "+descriptor.getWriteMethod()); System.out.println("------"); } } }
Output-- jdk property descriptor -- Name: class Reader: public final native java.lang.Class java.lang.Object.getClass() Writer: null ------ Name: name Reader: public java.lang.String com.logicbig.example.Person.getName() Writer: null ------ -- spring property descriptor -- Name: class Reader: public final native java.lang.Class java.lang.Object.getClass() Writer: null ------ Name: name Reader: public java.lang.String com.logicbig.example.Person.getName() Writer: public java.lang.String com.logicbig.example.Person.setName(java.lang.String) ------
Original Post
package com.logicbig.example;
import org.springframework.beans.BeanUtils;
public class CopyPropsExample { public static void main (String[] args) { Person person = new Person(); person.setName("Mike"); person.setAge(34);
Employee employee = new Employee(); BeanUtils.copyProperties(person, employee); System.out.println(employee); } }
OutputEmployee{name='Mike', age=34, dept='null'}
Original Post
package com.logicbig.example;
import org.springframework.beans.BeanUtils;
public class CopyPropsExample2 { public static void main(String[] args) { Employee employee = new Employee(); employee.setName("Mike"); employee.setAge(34); employee.setDept("IT");
Person person = new Person(); BeanUtils.copyProperties(employee, person); System.out.println(person); } }
OutputPerson{name='Mike', age=34}
Original Post
Using BeanUtils.resolveSignature example. Parsing string to do that. package com.logicbig.example;
import org.springframework.beans.BeanUtils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
public class ResolveSignature {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { Method method = BeanUtils.resolveSignature("aMethod(java.lang.String, int)", LocalBean.class); System.out.println("resolved method: " + method); method.invoke(new LocalBean(), "some string value", 100);
method = BeanUtils.resolveSignature("aMethod(java.lang.Integer)", LocalBean.class); System.out.println("resolved method: " + method); method.invoke(new LocalBean(), 200); }
private static class LocalBean { public void aMethod(String str, int anInt) { System.out.printf("aMethod invoked with params, str: %s, anInt: %s%n", str, anInt); }
public void aMethod(Integer anInt) { System.out.printf("aMethod invoked with param, anInt: %s%n", anInt); } } }
Outputresolved method: public void com.logicbig.example.ResolveSignature$LocalBean.aMethod(java.lang.String,int) aMethod invoked with params, str: some string value, anInt: 100 resolved method: public void com.logicbig.example.ResolveSignature$LocalBean.aMethod(java.lang.Integer) aMethod invoked with param, anInt: 200
Original Post
Using BeanUtils.resolveSignature example. Resolving method with no arguments. package com.logicbig.example;
import org.springframework.beans.BeanUtils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
public class ResolveSignature2 {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { Method method = BeanUtils.resolveSignature("doSomething", LocalBean.class); System.out.println("resolved method: "+method); method.invoke(new LocalBean()); }
private static class LocalBean { public void doSomething() { System.out.println("-- doing something -- "); } } }
Outputresolved method: public void com.logicbig.example.ResolveSignature2$LocalBean.doSomething() -- doing something --
Original Post
package com.logicbig.example;
import org.springframework.beans.BeanUtils;
public class CopyPropsEditableExample { public static void main(String[] args) { Rectangle rectangle = new Rectangle(); rectangle.setColor("blue"); rectangle.setLength(5); rectangle.setWidth(4);
Square square = new Square(); BeanUtils.copyProperties(rectangle, square, Shape.class); System.out.println(square); } }
OutputSquare{length=null, color='blue'}
Original Post
import org.springframework.beans.BeanUtils;
public class CopyPropsIgnoreExample { public static void main(String[] args) { Rectangle rectangle = new Rectangle(); rectangle.setColor("blue"); rectangle.setLength(5); rectangle.setWidth(4);
Square square = new Square(); BeanUtils.copyProperties(rectangle, square, "color"); System.out.println(square); } }
OutputSquare{length=5, color='null'}
Original Post
|
|