BeanWrapper

BeanWrapper

BeanWrapper provides methods to create Bean objects explicitly, to analyze and manipulate standard JavaBeans like the ability to get and set property values, get property descriptors and checks the readability/ writability of properties. BeanWrapper is also supports setting of index properties. Spring Framework has provided a separate predefined implementation class for BeanWrapper in the form of “BeanWrapperImpl”.

To set values to the Bean object through Bean Wrapper class we have to use the following method from BeanWrapper class.

public void setPropertyValue(String prop_Name, Object value)

Note

If we want to set all the properties at a time to Bean object, first, we have to set property names and their values in the form of Map object then set that Map object to BeanWrapper object, for this, we have to use the following method.

public void setPropertyValues(Map map)

To get property value explicitly from Bean object we have to use the following method from BeanWrapper class.

public Object getProperty(String name)

To get Bean object explicitly through BenWrapper we have to use the following method from BeNWrapper.

public Object getWrappedInstance()

To copy the properties values from one Bean object to another Bean object we have to use the following method from “org.springframework.beans. BeanUtils” class.

public void copyProperties(Object source, Object target)

Where Source object and target objects may be the objects of Same class or different classes having same property names and same property data types. To Check whether the property is readable or writable then we have to use the following methods from BeanWrapper class.

public boolean isReadableProperty(String prop_Name)
public boolean isWritableProperty(String prop_Name)

E.g

package com.ashok.spring.core.bean.wrapper.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;
   private double salary;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
            + "]";
   }
}
package com.ashok.spring.core.bean.wrapper.test;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import com.ashok.spring.core.bean.wrapper.beans.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class BeanWrapperTest {
   public static void main(String[] args) {
      BeanWrapper bw = new BeanWrapperImpl(Employee.class);
      bw.setPropertyValue("empId", 111);
      bw.setPropertyValue("empName", "Ashok Kumar");
      bw.setPropertyValue("salary", 50000);
      bw.setPropertyValue("empAddress", "Bhimavaram");
      Employee emp = (Employee) bw.getWrappedInstance();
      System.out.println(emp);

      System.out.println();
      Map map = new HashMap<>();
      map.put("empId", "222");
      map.put("empName", "Vinod Kumar");
      map.put("salary", "120000");
      map.put("empAddress", "Banglore");
      bw.setPropertyValues(map);
      System.out.println(emp);

      System.out.println("Employee Details");
      System.out.println("--------------------------");
      System.out.println("Employee No :" + bw.getPropertyValue("empId"));
      System.out.println("Employee Name :" + bw.getPropertyValue("empName"));
      System.out.println("Employee Salary :" + bw.getPropertyValue("salary"));
      System.out.println("Employee Address :" + bw.getPropertyValue("empAddress"));
      
      Employee emp1 = new Employee();
      bw = new BeanWrapperImpl(emp1);
      bw.setPropertyValue("empId", 333);
      bw.setPropertyValue("empName", "Ashok Kumar");
      bw.setPropertyValue("salary", 50000);
      bw.setPropertyValue("empAddress", "Bhimavaram");
      Employee emp2 = new Employee();
      BeanUtils.copyProperties(emp1, emp2);
      System.out.println(emp2);
   }
}

Output

Employee [empName=Ashok Kumar, empId=111, empAddress=Bhimavaram, salary=50000.0]

Employee [empName=Vinod Kumar, empId=222, empAddress=Banglore, salary=120000.0]
Employee Details
--------------------------
Employee No :222
Employee Name :Vinod Kumar
Employee Salary :120000.0
Employee Address :Banglore
Employee [empName=Ashok Kumar, empId=333, empAddress=Bhimavaram, salary=50000.0]

BeanWrapper
Scroll to top