Saturday, October 29, 2022

Spring - Bean Manipulations or 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 correspecding 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)
    • EX: BeanInfo beanInfo = Interospector.getBeanInfo(MyBean.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()
    • EX: PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();

Example:

public class Employee {
	private int eno; 
	private String ename; 
	private float esal; 
	private String eaddr;
	
	setXXX()  
	getXXX() 
}

Test.java

package com.cloud.core;
import java.beans.BeanInfo;
import java.beans.Introspector; 
import java.beans.PropertyDescriptor;

public class Test {
 public static void main(String[] args)throws Exception {
	BeanInfo beanInfo = Introspector.getBeanInfo(Employee.class); 
	PropertyDescriptor[] property_desc = beanInfo.getPropertyDescriptors(); 

	for(PropertyDescriptor p: property_desc){
		System.out.println(p); 
	}

	MethodDescriptor[] meths = beanInfo.getMethodDescriptors(); 

	for(MethodDescriptor m: meths){
		System.out.println(m.getName()); 
	}
  } 
}

  • 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,.... 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)

  • BeanInfoFactory implementation
    •  org.springframework.beans.ExtendedBeanInfoFactory, 
    • accepts JavaBeans "non- standard" setter methods as 'writable' which returns some values instead of void.

Example:

Employee.java

package com.cloud.beans; 
public class Employee {
   private int eno; 
   private String ename; 
   private float esal; 
   private String eaddr;
   
   public int getEno() {
      return eno; 
   }

   public int setEno(int eno) { 
      this.eno = eno;
      return eno;
   }

   public String getEname() {
      return ename; 
   }

   public void setEname(String ename) {
      this.ename = ename; 
   }

   public float getEsal() {
      return esal; 
   }

   public void setEsal(float esal) {
      this.esal = esal; 
   }

   public String getEaddr() {
      return eaddr; 
   }

   public void setEaddr(String eaddr) {
      this.eaddr = eaddr; 
   }
}

Test.java

package com.cloud.test;

import com.cloud.beans.Employee;
import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import org.springframework.beans.BeanInfoFactory;
import org.springframework.beans.ExtendedBeanInfoFactory;

public class Test {

	public static void main(String[] args)throws Exception {

	BeanInfoFactory factory = new ExtendedBeanInfoFactory(); 
	BeanInfo bean_Info = factory.getBeanInfo(Employee.class); 
	System.out.println(bean_Info);

	PropertyDescriptor[] props = bean_Info.getPropertyDescriptors(); 
	for(PropertyDescriptor p: props){
		System.out.println(p); }
		MethodDescriptor[] meths = beanInfo.getMethodDescriptors(); 

		for(MethodDescriptor m: meths) {
			System.out.println(m.getName()); 
		}
	} 
}

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 seperate 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)

Example:

Employee.java

package com.cloud.beans; public class Employee {
	private int eno; 
	private String ename; 
	private float esal; 
	private String eaddr;

	public int getEno() {
		return eno; 
	}

	public void setEno(int eno) {
		this.eno = eno; 
	}

	public String getEname() {
		return ename; 
	}

	public void setEname(String ename) {
		this.ename = ename; 
	}

	public float getEsal() {
		return esal; 
	}

	public void setEsal(float esal) {
		this.esal = esal; 
	}

	public String getEaddr() {
		return eaddr; 
	}

	public void setEaddr(String eaddr) {
		this.eaddr = eaddr; 
	}

	public void displayEmpDetails(){ 
		System.out.println("Employee Details"); 
		System.out.println("-------------------"); 
		System.out.println("Employee Id :"+eno);
		System.out.println("Employee Name :"+ename); 
		System.out.println("Employee Salary :"+esal); 
		System.out.println("Employee Address:"+eaddr);

	} 
}

Test1.java

package com.cloud.test;

import com.cloud.beans.Employee;
import org.springframework.beans.BeanWrapper; 
import org.springframework.beans.BeanWrapperImpl;

public class Test1 {

	public static void main(String[] args)throws Exception {

		BeanWrapper bw = new BeanWrapperImpl(Employee.class); 
		bw.setPropertyValue("eno", 111); 
		bw.setPropertyValue("ename", "AAA"); 
		bw.setPropertyValue("esal", 5000.0f); 
		bw.setPropertyValue("eaddr", "Hyd");

		Employee emp = (Employee) 
		bw.getWrappedInstance(); 
		System.out.println(emp);

		emp.displayEmpDetails();
		System.out.println();

		Map<String, String> map = new HashMap<String, String>(); 
		map.put("eno", "222");
		map.put("ename", "BBB");
		map.put("esal", "6000");
		map.put("eaddr", "Hyd"); 

		bw.setPropertyValues(map); 
		System.out.println(emp);

		emp.displayEmpDetails();

		System.out.println("Employee Details"); 
		System.out.println("--------------------------");
		System.out.println("Employee No :"+bw.getPropertyValue("eno")); 
		System.out.println("Employee Name :"+bw.getPropertyValue("ename")); 
		System.out.println("Employee Salary :"+bw.getPropertyValue("esal")); 
		System.out.println("Employee Address :"+bw.getPropertyValue("eaddr"));

	}
}

Test2.java

package com.cloud.test;

import com.cloud.beans.Employee;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper; 
import org.springframework.beans.BeanWrapperImpl;

public class Test2 {

	public static void main(String[] args)throws Exception {

		Employee emp1 = new Employee();

		BeanWrapper bw = new BeanWrapperImpl(emp1); 
		bw.setPropertyValue("eno", 111); 
		bw.setPropertyValue("ename", "AAA"); 
		bw.setPropertyValue("esal", 5000.0f); 
		bw.setPropertyValue("eaddr", "Hyd"); 

		System.out.println(emp1); 
		emp1.displayEmpDetails();
		
		Employee emp2 = new Employee(); 
		BeanUtils.copyProperties(emp1, emp2); 
		System.out.println(emp2); 
		emp2.displayEmpDetails();
	} 
}


You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS