Home  >  Spring Core

Bean Manipulation | BeanWrapper Spring Example

By Arvind Rai, August 18, 2013
org.springframework.beans.BeanWrapper is an interface and normally is not used directly. It is used by BeanFactory and DataBinder. BeanWrapper plays with java beans to manipulate it. It sets and gets value from java beans by the method BeanWrapper.setPropertyValue and BeanWrapper.getPropertyValue. Find an example to understand BeanWrapper.

SpringDemo.java
package 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);
   } 
}
 


College.java
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;
	}
}
 


Employee.java
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;
	}
}
 
Download Source Code
bean-manipulation-beanwrapper-spring-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us