Following examples shows how to set nested beans instances by using BeanWrapper .
Examples
Beans
package com.logicbig.example;
public class Department {
private long deptCode;
private String deptName;
public long getDeptCode() {
return deptCode;
}
public void setDeptCode(long deptCode) {
this.deptCode = deptCode;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Department{" +
"deptCode='" + deptCode + '\'' +
", deptName='" + deptName + '\'' +
'}';
}
}
package com.logicbig.example;
public class Employee {
private String name;
private Department department;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", department=" + department +
'}';
}
}
Setting nested bean
package com.logicbig.example;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
public class BeanWrapperNestedBean {
public static void main (String[] args) {
BeanWrapper beanWrapperEmployee = new BeanWrapperImpl(new Employee());
beanWrapperEmployee.setPropertyValue("name", "Joe");
BeanWrapper beanWrapperDept = new BeanWrapperImpl(new Department());
beanWrapperDept.setPropertyValue("deptCode", 1101);
beanWrapperDept.setPropertyValue("deptName", "IT");
beanWrapperEmployee.setPropertyValue("department", beanWrapperDept.getWrappedInstance());
System.out.println(beanWrapperEmployee.getWrappedInstance());
}
}
OutputEmployee{name='Joe', department=Department{deptCode='1101', deptName='IT'}}
Setting nested bean via nested path
package com.logicbig.example;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
public class BeanWrapperNestedBean2 {
public static void main (String[] args) {
BeanWrapper bw = new BeanWrapperImpl(new Employee());
bw.setPropertyValue("name", "Joe");
bw.setPropertyValue("department", new Department());
bw.setPropertyValue("department.deptCode", 1001);
bw.setPropertyValue("department.deptName", "IT");
System.out.println(bw.getWrappedInstance());
}
}
OutputEmployee{name='Joe', department=Department{deptCode='1001', deptName='IT'}}
Setting nested collection elements via index
package com.logicbig.example;
import java.util.List;
import java.util.Map;
public class EmployeeGroup {
private String groupName;
private List<Employee> employeeList;
private Map<String, String> groupProperties;
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public List<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<Employee> employeeList) {
this.employeeList = employeeList;
}
public Map<String, String> getGroupProperties() {
return groupProperties;
}
public void setGroupProperties(Map<String, String> groupProperties) {
this.groupProperties = groupProperties;
}
@Override
public String toString() {
return "EmployeeGroup{" +
"groupName='" + groupName + '\'' +
", employeeList=" + employeeList +
", groupProperties=" + groupProperties +
'}';
}
}
package com.logicbig.example;
import org.springframework.beans.BeanWrapperImpl;
import java.util.ArrayList;
import java.util.HashMap;
public class BeanWrapperNestedIndexProperty {
public static void main(String[] args) {
BeanWrapperImpl bw = new BeanWrapperImpl(new EmployeeGroup());
bw.setPropertyValue("groupName", "TennisTeam");
bw.setPropertyValue("employeeList", new ArrayList<>());
bw.setPropertyValue("employeeList[0]", new Employee());
bw.setPropertyValue("employeeList[0].name", "Mike");
bw.setPropertyValue("employeeList[0].department", new Department());
bw.setPropertyValue("employeeList[0].department.deptCode", 1001);
bw.setPropertyValue("employeeList[0].department.deptName", "Admin");
bw.setPropertyValue("employeeList[1]", new Employee());
bw.setPropertyValue("employeeList[1].name", "Jackie");
bw.setPropertyValue("employeeList[1].department", new Department());
bw.setPropertyValue("employeeList[1].department.deptCode", 1002);
bw.setPropertyValue("employeeList[1].department.deptName", "Account");
bw.setPropertyValue("groupProperties", new HashMap<>());
bw.setPropertyValue("groupProperties[location]", "Texas");
bw.setPropertyValue("groupProperties[matchesPerYear]", "2");
System.out.println(bw.getWrappedInstance());
System.out.println("-- accessing field values --");
Object employeeList = bw.getPropertyValue("employeeList");
System.out.println("employeeList: " + employeeList);
Object firstEmployee = bw.getPropertyValue("employeeList[0]");
System.out.println("employeeList[0]: " + firstEmployee);
Object firstEmployeeDept = bw.getPropertyValue("employeeList[0].department.deptName");
System.out.println("employeeList[0].department.deptName: " + firstEmployeeDept);
Object matchesPerYear = bw.getPropertyValue("groupProperties[matchesPerYear]");
System.out.println("groupProperties[matchesPerYear]: "+matchesPerYear);
}
}
OutputEmployeeGroup{groupName='TennisTeam', employeeList=[Employee{name='Mike', department=Department{deptCode='1001', deptName='Admin'}}, Employee{name='Jackie', department=Department{deptCode='1002', deptName='Account'}}], groupProperties={location=Texas, matchesPerYear=2}} -- accessing field values -- employeeList: [Employee{name='Mike', department=Department{deptCode='1001', deptName='Admin'}}, Employee{name='Jackie', department=Department{deptCode='1002', deptName='Account'}}] employeeList[0]: Employee{name='Mike', department=Department{deptCode='1001', deptName='Admin'}} employeeList[0].department.deptName: Admin groupProperties[matchesPerYear]: 2
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|
|