Bean Manipulation | BeanWrapper Spring Example
July 12, 2022
On this page we will learn using Spring BeanWrapper
interface.
1. The Spring
BeanWrapper
is an interface and normally it is not used directly. It is used by BeanFactory
and DataBinder
. It provides operations to analyze and manipulate standard JavaBeans.
2. The
BeanWrapper
manipulates values of JavaBeans. It sets and gets values from JavaBeans by the method setPropertyValue
and getPropertyValue
respectively.
Example
SpringDemo.javapackage com.concretepage; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; public class SpringDemo { public static void main(String... args) { BeanWrapper employee = new BeanWrapperImpl(new Employee()); employee.setPropertyValue("empName","Ram"); employee.setPropertyValue("age","21"); BeanWrapper college = new BeanWrapperImpl(new College()); college.setPropertyValue("name", "SSIC"); college.setPropertyValue("director", employee.getWrappedInstance()); String name = (String)college.getPropertyValue("director.empName"); System.out.println(name); } }
package com.concretepage; public class College { private String name; private Employee director; public String getName() { return name; } public void setName(String name) { this.name = name; } public Employee getDirector() { return director; } public void setDirector(Employee director) { this.director = director; } }
package com.concretepage; public class Employee { private String empName; private int age; public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }