Recent Posts

Wednesday, 19 June 2019

Bean Manipulations and Bean Wrappers

Bean Manipulations and Bean Wrappers
     In Bean Manipulation, we are able to perform the following actions.
1. Getting Beans Information explicitly like properties and their setXXX() and getXXX() methods.
2. Creating JavaBean Objects, checking bean property types, copying bean properties, etc.
3. Accessing fields without standard getters and setters.
4. Analyze and manipulate standard JavaBeans like to get and set property values, get property descriptors, and query the readability/writability of properties and setting of index properties.

     In Java, we are able to get beans descriptions like bean properties information like their names and the corresponding setXXX() and getXXX() methods information by using "Beans Introspection". If we want to get beans data explicitly then we have to java.beans.BeanInfo interface, to get BeanInfo object then we have to use the following method from java.beans.Introspector class.
public BeanInfo getBeanInfo(Class bean_class_type)
E.g.
BeanInfo beanInfo = Interospector.getBeanInfo(Employee.class);
     To get All properties information of the Bean object we have to use "java.beans.PropertyDescriptor" class. To get all properties description in the form of PropertyDescriptor objects in an array then we have to use the following method from BeanInfo.
public PropertyDescriptor[] getPropertyDescriptors()
E.g.
package com.ashok.spring.core.bean.manipulation.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.manipulation.test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;

import org.springframework.beans.BeanInfoFactory;
import org.springframework.beans.ExtendedBeanInfoFactory;

import com.ashok.spring.core.bean.manipulation.beans.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestSpringApplication {
   public static void main(String[] args) throws IntrospectionException {
      BeanInfo beanInfo = Introspector.getBeanInfo(Employee.class);
      System.out.println(beanInfo);
      PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
      for (PropertyDescriptor p : props) {
         System.out.println(p);
      }
      MethodDescriptor[] meths = beanInfo.getMethodDescriptors();
      for (MethodDescriptor m : meths) {
         System.out.println(m.getName());
      }
   }
}
Output
java.beans.GenericBeanInfo@1b28cdfa
java.beans.PropertyDescriptor[name=class; propertyType=class java.lang.Class; readMethod=public final native java.lang.Class java.lang.Object.getClass()]
java.beans.PropertyDescriptor[name=empAddress; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpAddress(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpAddress(java.lang.String)]
java.beans.PropertyDescriptor[name=empId; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpId(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpId(java.lang.String)]
java.beans.PropertyDescriptor[name=empName; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpName(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpName(java.lang.String)]
java.beans.PropertyDescriptor[name=salary; propertyType=double; readMethod=public double com.ashok.spring.core.bean.manipulation.beans.Employee.getSalary(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setSalary(double)]
getClass
getSalary
getEmpId
setEmpId
getEmpAddress
wait
notifyAll
notify
wait
getEmpName
setEmpName
hashCode
setEmpAddress
setSalary
wait
equals
toString

     In Spring framework, to create beans and to manipulate beans explicitly Spring Framework has provided predefined library in the form of "org.springframework.beans".
     In spring "org.springframework.beans" package has provided the following classes and interfaces to perform manipulations on beans.
BeanInfoFactory
     It is an alternative to "Beans Introspection" provided by Spring Framework, it can be used to get details about the Bean objects like properties details, events details etc. by using java.beans.BeanInfo object internally. Spring Framework has provided a seperate predefined implementation class for BeanInfoFactory interface in the form of "org.springframework.beans.ExtendedBeanInfoFactory" class. To get BeanInfo object we have to use the following method from BeanInfoFactory interface.
public BeanInfo getBeanInfo(Class bean_Class_Type)
Note: BeanInfoFactory implementation, org.springframework.beans.ExtendedBeanInfoFactory, accepts JavaBeans "non-standard" setter methods as 'writable' which returns some values instead of void.
package com.ashok.spring.core.bean.manipulation.test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;

import org.springframework.beans.BeanInfoFactory;
import org.springframework.beans.ExtendedBeanInfoFactory;

import com.ashok.spring.core.bean.manipulation.beans.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestSpringApplication {
   public static void main(String[] args) throws IntrospectionException {
      BeanInfoFactory factory = new ExtendedBeanInfoFactory();
      BeanInfo beanInfo = factory.getBeanInfo(Employee.class);
      System.out.println(beanInfo);
      PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
      for (PropertyDescriptor p : props) {
         System.out.println(p);
      }
      MethodDescriptor[] meths = beanInfo.getMethodDescriptors();
      for (MethodDescriptor m : meths) {
         System.out.println(m.getName());
      }
   }
}
Output
java.beans.GenericBeanInfo@1b28cdfa
java.beans.PropertyDescriptor[name=class; propertyType=class java.lang.Class; readMethod=public final native java.lang.Class java.lang.Object.getClass()]
java.beans.PropertyDescriptor[name=empAddress; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpAddress(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpAddress(java.lang.String)]
java.beans.PropertyDescriptor[name=empId; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpId(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpId(java.lang.String)]
java.beans.PropertyDescriptor[name=empName; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpName(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpName(java.lang.String)]
java.beans.PropertyDescriptor[name=salary; propertyType=double; readMethod=public double com.ashok.spring.core.bean.manipulation.beans.Employee.getSalary(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setSalary(double)]
getClass
getSalary
getEmpId
setEmpId
getEmpAddress
wait
notifyAll
notify
wait
getEmpName
setEmpName
hashCode
setEmpAddress
setSalary
wait
equals
toString
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)
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);
   }
}
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]

Next Tutorial : Property Editors

Previous Tutorial : Internationalization in Spring

No comments:

Post a Comment