Close

Spring Framework - PropertyAccessorFactory Examples

Spring Framework 

Creating BeanWrapper using PropertyAccessorFactory

package com.logicbig.example.propertyacessorfactory;

import com.logicbig.example.TestBean;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;

public class PropertyAccessorFactoryExample {

public static void main (String[] args) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(new TestBean());
bw.setPropertyValue("aString", "anotherString");
System.out.println(bw.getWrappedInstance());
}
}

Output

TestBean{aString='anotherString', anInt=5, date=Mon May 01 16:08:01 CDT 2017}




Creating BeanWrapper using PropertyAccessorFactory

package com.logicbig.example;

import com.logicbig.example.TestBean;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;

public class PropertyAccessorFactoryExample {

public static void main (String[] args) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(new Person());
bw.setPropertyValue("name", "Raj");
bw.setPropertyValue("age", "26");
System.out.println(bw.getWrappedInstance());
}
}

Output

Person{name='Raj', age=26}
Original Post




See Also