Bean Wrapper

Bean Wrapper

In this tutorial, we are going to discuss bean wrapper in the spring framework. Bean Wrapper provides methods to explicitly create Bean objects to analyze and manipulate standard JavaBeans, like getting and setting property values, getting property descriptors and checking the readability/ writability of properties. BeanWrapper also supports the setting of index properties. Spring Framework has provided a separate predefined implementation class for BeanWrapper in the form of “BeanWrapperImpl.”

Bean Wrapper

To set values to the Bean object through the 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 the Bean object, we have to use the following method from BeanWrapper class.

public Object getProperty(String name)

To get Bean object explicitly through BeanWrapper, we have to use the following method from BeanWrapper.

public Object getWrappedInstance()

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

public void copyProperties(Object source, Object target)

Where Source and target objects may be the same class or different classes having the same property names and property data types, to Check whether the property is readable or writable, we have to use the following methods: 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]

Bean Wrapper
Scroll to top