BeanWrapper 提供了设置和获取属性值(批量或单独)获取属性描述符以及查询属性以确定他们是可读还是可写的功能。
1.定义测试bean
@Data
public class Company {
private String name;
private Person person;
}
@Data
public class Person {
private String name;
private int age;
}
2.使用BeanWrapper设置Bean的属性
@GetMapping(value = "/get1")
public Object get1() throws IOException {
BeanWrapper company = new BeanWrapperImpl(new Company());
company.setPropertyValue("name", "Some Company Inc.");
//一样
PropertyValue value = new PropertyValue("name", "Some Company Inc.");
company.setPropertyValue(value);
BeanWrapper person = new BeanWrapperImpl(new Person());
person.setPropertyValue("name", "Person name");
person.setPropertyValue("age", 110);
company.setPropertyValue("person", person.getWrappedInstance());
System.out.println(company.getWrappedInstance());
System.out.println(company.getPropertyValue("person.name"));
return "obj";
}
属性表达式的例子
表达式 解释
name 表示属性name 与getName() 或 isName setName相对应
account.name 表示属性account的嵌套属性name 与 getAccount().getName 相对应
account[2] 表示索引属性account的第三个元素 索引属性为 数组或者集合
account[COMPANYNAME] 表示映射属性account被键COMPANYNAME索引到的映射项值