扩展 Bean 属性填充时的逻辑
1.PropertyEditor
编写一个 PropertyEditor 接口实现 属性编辑器, 包含转换的逻辑代码;
2.PropertyEditorRegistrar
实现一个 PropertyEditorRegistrar 属性编辑器的注册器, 负责将这个 PropertyEditor 注册到 PropertyEditorRegistry;
3.将PropertyEditorRegistrar添加到 BeanFactory
继承 CustomEditorConfigurer 辅助类; 将PropertyEditorRegistrar 给它的 PropertyEditorRegistrar[] 属性设置, 或者给它的 customEditors 属性设置也行
CustomEditorConfigurer 实现了 BeanFactoryPostProcessor 会调用beanFactory.addPropertyEditorRegistrar(propertyEditorRegistrar); 添加到 BeanFactory (propertyEditorRegistrars)中
4.在填充时
- BeanFactory 在实例化 Bean 时, 在外面用 BeanWrapper 包装了一下(装饰模式), initBeanWrapper 时, 会将当前 bean factory (propertyEditorRegistrars)中的全部 PropertyEditor 注册到 BeanWrapper 中
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#instantiateBean
- 在填充时会调用, 通过BeanWrapper包装类进行设置值
BeanWrapper: 会根据类型找到 PropertyEditor, 调用setValue设置值逻辑, 再getValue 拿到该值, 最后反射调用bean的setting方法设置值
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyPropertyValues
org.springframework.beans.TypeConverterDelegate#convertIfNecessary(String, Object, Object, Class<T>,TypeDescriptor)
org.springframework.beans.TypeConverterDelegate#doConvertValue
Spring 中的属性填充
