源码分析:Spring 属性设置 - PropertyAccessorFactory

总结

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
----------------------------------------------------------------------------------------------------------
一句话:BeanWrapperImpl只可以setter方法注入,DirectFieldAccessor反射属性注入(更强大)
----------------------------------------------------------------------------------------------------------
AbstractNestablePropertyAccessor.setPropertyValue(String, Object)
// 1. 获取Object
nestedPa = getPropertyAccessorForPropertyPath(propertyName);
含有 [ ] . 先取到外层对象, 再递归 例如:friend.name 先拿取到 friend对象,再解析 name属性
===> 默认内层对象不存在,根据 isAutoGrowNestedPaths() 标识创建默认对象
不含有 [ ] . 直接获取当前对象
// 2. 进行setter(不同子类实现)
nestedPa.setPropertyValue(tokens, new PropertyValue(propertyName, value));
BeanWrapperImpl BeanPropertyHandler.setValue
---> ReflectionUtils.makeAccessible(writeMethod);
---> writeMethod.invoke(getWrappedInstance(), value);
DirectFieldAccessor FieldPropertyHandler.setValue
---> ReflectionUtils.makeAccessible(this.field);
---> this.field.set(getWrappedInstance(), value);

----------------------------------------------------------------------------------------------------------

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class MyTest {

// @Data
public static class Student {
public Student() {
System.out.println("创建了..." + this.hashCode());// 用于debug
}

private String name;
private Integer age;

private Student friend;
private List<String> hobbies;
}

// 2024-05-08 17:33
public static void main(String[] args) throws Throwable {
// Student student = new Student();
// BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(student);
// beanWrapper.setPropertyValue("name", "陶攀峰");
// beanWrapper.setPropertyValue("age", 18);
//
// beanWrapper.setAutoGrowNestedPaths(true);// friend 属性为null时,自动创建
//
// // beanWrapper.setPropertyValue("friend", new Student());
// beanWrapper.setPropertyValue("friend.name", "二狗");
// beanWrapper.setPropertyValue("friend.age", 20);
//
// // beanWrapper.setPropertyValue("hobbies", new ArrayList<>());
// beanWrapper.setPropertyValue("hobbies[0]", "a");
// beanWrapper.setPropertyValue("hobbies[1]", "b");
// beanWrapper.setPropertyValue("hobbies[2]", "c");
// System.out.println(student);
//-----------------------------------------------------------------------------
Student student = new Student();
ConfigurablePropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(student);
accessor.setPropertyValue("name", "陶攀峰");
accessor.setPropertyValue("age", 18);

accessor.setAutoGrowNestedPaths(true);// friend 属性为null时,自动创建

// accessor.setPropertyValue("friend", new Student());
accessor.setPropertyValue("friend.name", "二狗");
accessor.setPropertyValue("friend.age", 20);

// accessor.setPropertyValue("hobbies", new ArrayList<>());
accessor.setPropertyValue("hobbies[0]", "a");
accessor.setPropertyValue("hobbies[1]", "b");
accessor.setPropertyValue("hobbies[2]", "c");
System.out.println(student);
}
}