Spring beans creation and manipulation is based on standard JavaBeans. The package org.springframework.beans contains interfaces and classes for manipulating beans.
In this tutorial we are going to quickly walk through important interfaces/classes.
BeanWrapper interface
Spring's BeanWrapper is central interface of JavaBeans infrastructure.
BeanWrapper extends various interfaces. In this tutorial we will focus on PropertyAccessor interface which provides methods to get and set property values, get property descriptors, and query the readability/writability of properties. It also supports setting of index properties.
Definition of BeanWrapper(Version: spring-framework 6.1.2) package org.springframework.beans;
........
public interface BeanWrapper extends ConfigurablePropertyAccessor {
.............
Object getWrappedInstance(); 1
Class<?> getWrappedClass(); 2
PropertyDescriptor[] getPropertyDescriptors(); 3
PropertyDescriptor getPropertyDescriptor(String propertyName)
throws InvalidPropertyException; 4
}
Definition of PropertyAccessor(Version: spring-framework 6.1.2) package org.springframework.beans;
........
public interface PropertyAccessor {
.............
boolean isReadableProperty(String propertyName); 1
boolean isWritableProperty(String propertyName); 2
@Nullable
Class<?> getPropertyType(String propertyName) throws BeansException; 3
@Nullable
TypeDescriptor getPropertyTypeDescriptor(String propertyName) throws BeansException; 4
@Nullable
Object getPropertyValue(String propertyName) throws BeansException; 5
void setPropertyValue(String propertyName, @Nullable Object value)
throws BeansException; 6
void setPropertyValue(PropertyValue pv) throws BeansException; 7
void setPropertyValues(Map<?, ?> map) throws BeansException; 8
void setPropertyValues(PropertyValues pvs) throws BeansException; 9
void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown)
throws BeansException; 10
void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean ignoreInvalid)
throws BeansException; 11
}
Implementations of BeanWrapper/ConfigurablePropertyAccessor
BeanWrapperImpl is implementation of BeanWrapper . DirectFieldAccessor is another concrete class which implements ConfigurablePropertyAccessor . This class is used to set field values directly.
PropertyAccessorFactory class
PropertyAccessorFactory is another way to get BeanWrapper/DirectFieldAccessor instances. It's based on factory pattern so we don't have to worry about what implementation to use.
Following methods of PropertyAccessorFactory can be used:
public static BeanWrapper forBeanPropertyAccess(Object target)
public static ConfigurablePropertyAccessor forDirectFieldAccess(Object target)
Examples
A bean
package com.logicbig.example;
class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Using setPropertyValue(String propertyName, Object value)
package com.logicbig.example;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyValue;
public class BeanWrapperExample {
public static void main (String[] args) {
BeanWrapper beanWrapper = new BeanWrapperImpl(new Person());
beanWrapper.setPropertyValue("name", "John");
beanWrapper.setPropertyValue("age", 33);
//the next commented line will also work, auto conversion is performed by Spring
/* beanWrapper.setPropertyValue("age", "33");*/
System.out.println("bean: "+beanWrapper.getWrappedInstance());
//getting property value
Object value = beanWrapper.getPropertyValue("name");
System.out.println("person.name: "+value);
}
}
Outputbean: Person{name='John', age=33} person.name: John
Using setPropertyValue(PropertyValue pv)
package com.logicbig.example;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyValue;
public class BeanWrapperPropertyValueExample {
public static void main (String[] args) {
BeanWrapper beanWrapper = new BeanWrapperImpl(new Person());
PropertyValue pv = new PropertyValue("name", "John");
beanWrapper.setPropertyValue("name", "John");
PropertyValue pv2 = new PropertyValue("age", 33);
beanWrapper.setPropertyValue(pv);
beanWrapper.setPropertyValue(pv2);
System.out.println("bean: "+beanWrapper.getWrappedInstance());
}
}
Outputbean: Person{name='John', age=33}
Using setPropertyValues(Map<?, ?> map)
package com.logicbig.example;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.util.HashMap;
import java.util.Map;
public class BeanWrapperMapExample {
public static void main (String[] args) {
BeanWrapper beanWrapper = new BeanWrapperImpl(new Person());
Map<String, Object> map = new HashMap<>();
map.put("name", "Tina");
map.put("age", "32");
beanWrapper.setPropertyValues(map);
System.out.println(beanWrapper.getWrappedInstance());
}
}
OutputPerson{name='Tina', age=32}
Using setPropertyValues(PropertyValues pvs)
package com.logicbig.example;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.MutablePropertyValues;
public class BeanWrapperMutablePropertyExample {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new Person());
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("name", "Diana");
mpv.add("age", "30");
bw.setPropertyValues(mpv);
System.out.println(bw.getWrappedInstance());
}
}
OutputPerson{name='Diana', age=30}
Using isReadableProperty(String propertyName)
package com.logicbig.example;
import java.util.Date;
public class TestBean {
private String aString;
private int anInt = 5;
private Date date = new Date(12000000000L);
public String getAString () {
return aString;
}
public void setAString (String aString) {
this.aString = aString;
}
public int getAnInt () {
return anInt;
}
public void setAnInt (int anInt) {
this.anInt = anInt;
}
@Override
public String toString() {
return "TestBean{" +
"aString='" + aString + '\'' +
", anInt=" + anInt +
", date=" + date +
'}';
}
}
package com.logicbig.example;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
public class BeanWrapperReadOnlyCheckExample {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new TestBean());
bw.setPropertyValue("aString", "someString");
System.out.println("date read only: " + bw.isReadableProperty("date"));
//similarly we have bw.isWritableProperty(..) method
System.out.println(bw.getWrappedInstance());
}
}
Outputdate read only: false TestBean{aString='someString', anInt=5, date=Tue May 19 16:20:00 CDT 1970}
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());
}
}
OutputPerson{name='Raj', age=26}
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|