Spring 属性注入(二)BeanWrapper 结构
Spring 属性注入(二)BeanWrapper 结构
Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html)
BeanWrapper 位于 org.springframework.beans 包中,默认实现为 BeanWrapperImpl,提供分析和处理标准 JavaBean 用于 get 和 set 属性,取得属性描述,查询属性的读/写能力。
beans 包还提供了一个 PropertyValues 用于保存多个属性值,默认的实现 MutablePropertyValues。另外还有两个有用的工具类,BeanUtils 和 PropertyAccessorUtils。
一、BeanWrapper 类图

PropertyEditorRegistryPropertyEditor 注册、查找。TypeConverter类型转换,其主要的工作由 TypeConverterDelegate 这个类完成的。PropertyAccessor属性读写。ConfigurablePropertyAccessor配置一些属性,如设置 ConversionService、是否暴露旧值、嵌套注入时属性为 null 是否自动创建。BeanWrapper对 bean 进行封装。AbstractNestablePropertyAccessor实现了对嵌套属性注入的处理,其它实现见名知义就不介绍了。
BeanWrapper 的层次结构还是比较清晰的,继承于 ConfigurablePropertyAccessor 和 PropertyAccessor 接口。
有一点不太明白,为什么 BeanWrapper 要继承于 PropertyEditorRegistry。从字面意义上来说,PropertyEditorRegistry 提供了 PropertyEditor,而 TypeConverter 进行类型转换,和 BeanWrapper 不是继承关系,使用组合感觉更好。
二、PropertyEditorRegistry
PropertyEditor 将 String 转换为其它类型,PropertyEditorRegistry 统一管理所有的 PropertyEditor 注册和查找。
public interface PropertyEditorRegistry {
// 1. 根据类型注册 PropertyEditor
void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor);
// 2. 根据类型(requiredType)和属性(propertyPath)注册 PropertyEditor
// 查找时优先根据 propertyPath 查找对应的 PropertyEditor,propertyPath 可以为嵌套属性
void registerCustomEditor(@Nullable Class<?> requiredType,
@Nullable String propertyPath, PropertyEditor propertyEditor);
// 3. 根据类型和属性查找 PropertyEditor
PropertyEditor findCustomEditor(@Nullable Class<?> requiredType, @Nullable String propertyPath);
}
PropertyEditorRegistry 的实现类 PropertyEditorRegistrySupport 分析见这两篇文章:
三、TypeConverter
TypeConverter 负责类型转换,其主要的工作由 TypeConverterDelegate 这个类完成的。
public interface TypeConverter {
// 1. value 转换成 requiredType
<T> T convertIfNecessary(Object value, Class<T> requiredType) throws TypeMismatchException;
// 2. methodParam 同下
<T> T convertIfNecessary(Object value, Class<T> requiredType,
MethodParameter methodParam) throws TypeMismatchException;
// 3. field 是 value 转换成 requiredType 后需要赋值的 field 字段
// 可以从该 field 字段拿到其泛型信息,从而进一步判断是否可以转换,毕竟 requiredType 只有 Class 信息
<T> T convertIfNecessary(Object value, Class<T> requiredType, Field field)
throws TypeMismatchException;
}
TypeConverter 的实现类 TypeConverterSupport 的所有功能,实际是都是委托给 TypeConverterDelegate 完成的。
四、PropertyAccessor
PropertyAccessor 提供了对 JavaBean 的基本操作。
public interface PropertyAccessor {
boolean isReadableProperty(String propertyName);
boolean isWritableProperty(String propertyName);
Class<?> getPropertyType(String propertyName) throws BeansException;
TypeDescriptor getPropertyTypeDescriptor(String propertyName) throws BeansException;
Object getPropertyValue(String propertyName) throws BeansException;
void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException;
void setPropertyValue(PropertyValue pv) throws BeansException;
void setPropertyValues(Map<?, ?> map) throws BeansException;
void setPropertyValues(PropertyValues pvs) throws BeansException;
void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown)
throws BeansException;
void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean ignoreInvalid)
throws BeansException;
}
PropertyAccessor 的实现类是 AbstractPropertyAccessor,这个类中 setPropertyValue 进行了处理,最终都是调用 setPropertyValue(String propertyName, Object value) 完成属性注入。
四、ConfigurablePropertyAccessor
public interface ConfigurablePropertyAccessor extends
PropertyAccessor, PropertyEditorRegistry, TypeConverter {
void setConversionService(@Nullable ConversionService conversionService);
ConversionService getConversionService();
// PropertyEditor 使用时是否暴露修改前后值
void setExtractOldValueForEditor(boolean extractOldValueForEditor);
boolean isExtractOldValueForEditor();
// 嵌套注入时当属性为 null 时是否自动生成对象
void setAutoGrowNestedPaths(boolean autoGrowNestedPaths);
boolean isAutoGrowNestedPaths();
}
ConfigurablePropertyAccessor 的实现也类是 AbstractPropertyAccessor,这个类中默认 extractOldValueForEditor 和 autoGrowNestedPaths 都是 false,即不暴露旧值,也不支持嵌套注入时属性为 null 就自动创建对象。
五、BeanWrapper
public interface BeanWrapper extends ConfigurablePropertyAccessor {
// @since 4.1 设置集合或数组自动生成对象的最大嵌套深度
void setAutoGrowCollectionLimit(int autoGrowCollectionLimit);
int getAutoGrowCollectionLimit();
Object getWrappedInstance();
Class<?> getWrappedClass();
PropertyDescriptor[] getPropertyDescriptors();
PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException;
}
- Spring 属性注入(三)AbstractNestablePropertyAccessor
- BeanWrapperImpl 源码分析如下:
BeanWrapper 的实现类是 BeanWrapperImpl,主要完成了 JavaBean 的内省,包括 PropertyDescriptor 的获取,属性的赋值等。
(1) JavaBean 的内省
private CachedIntrospectionResults cachedIntrospectionResults;
// 获取
private CachedIntrospectionResults getCachedIntrospectionResults() {
if (this.cachedIntrospectionResults == null) {
this.cachedIntrospectionResults = CachedIntrospectionResults.forClass(getWrappedClass());
}
return this.cachedIntrospectionResults;
}
JavaBean 的内省都是由 CachedIntrospectionResults 完成,通过 cachedIntrospectionResults 就可以获取对应属性的 PropertyDescriptor。例如下面两个方法:
@Override
public PropertyDescriptor[] getPropertyDescriptors() {
return getCachedIntrospectionResults().getPropertyDescriptors();
}
@Override
public PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException {
BeanWrapperImpl nestedBw = (BeanWrapperImpl) getPropertyAccessorForPropertyPath(propertyName);
// 获取属性名称
String finalPath = getFinalPath(nestedBw, propertyName);
PropertyDescriptor pd = nestedBw.getCachedIntrospectionResults().getPropertyDescriptor(finalPath);
return pd;
}
(2) BeanPropertyHandler
@Override
protected BeanPropertyHandler getLocalPropertyHandler(String propertyName) {
PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(propertyName);
return (pd != null ? new BeanPropertyHandler(pd) : null);
}
BeanPropertyHandler 是 BeanWrapperImpl 的内部类,是对 PropertyDescriptor 的封装,完成对属性的各种操作,如赋值。
(3) convertForProperty
convertForProperty 在 AbstractAutowireCapableBeanFactory#applyPropertyValues 属性注入时使用
public Object convertForProperty(@Nullable Object value, String propertyName) throws TypeMismatchException {
CachedIntrospectionResults cachedIntrospectionResults = getCachedIntrospectionResults();
PropertyDescriptor pd = cachedIntrospectionResults.getPropertyDescriptor(propertyName);
if (pd == null) {
throw new InvalidPropertyException(getRootClass(), getNestedPath() + propertyName,
"No property '" + propertyName + "' found");
}
TypeDescriptor td = cachedIntrospectionResults.getTypeDescriptor(pd);
if (td == null) {
td = cachedIntrospectionResults.addTypeDescriptor(pd, new TypeDescriptor(property(pd)));
}
// 父类 AbstractNestablePropertyAccessor 进行类型转换
return convertForProperty(propertyName, null, value, td);
}
// Property 也是对 PropertyDescriptor 封装,在 Android 等环境中不存在 PropertyDescriptor 类
private Property property(PropertyDescriptor pd) {
GenericTypeAwarePropertyDescriptor gpd = (GenericTypeAwarePropertyDescriptor) pd;
return new Property(gpd.getBeanClass(), gpd.getReadMethod(), gpd.getWriteMethod(), gpd.getName());
}
每天用心记录一点点。内容也许不重要,但习惯很重要!
Spring 属性注入(二)BeanWrapper 结构的更多相关文章
- Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用
Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...
- Spring 属性注入(三)AbstractNestablePropertyAccessor
Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...
- Spring 属性注入(四)属性键值对 - PropertyValue
Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...
- spring 属性注入
Spring的核心技术室依赖注入,下面是依赖注入之属性注入的实现过程,牛刀小试,请看效果. 1.首先添加Spring.Web引用.本例中是使用分层思想来演示的,下面是项目的结构和UserModel类的 ...
- java spring属性注入
一.创建对象时候,向类里面属性设置值:一般有三个方式 1) .有参构造, 2). set**** 3).接口注入 二. 在spring框架里面,支持前面两种方式: 1).有参构造方法 用constr ...
- Spring属性注入、构造方法注入、工厂注入以及注入参数(转)
Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...
- 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式
Spring的属性注入: 构造方法的属性注入 set方法的属性注入
- spring属性注入
1,set方法注入 (1)对于值类型的属性: 在对象中一定要有set方法 package com.songyan.demo1; import com.songyan.injection.Car; /* ...
- Spring 依赖注入(二、注入参数)
注入参数基本分7类: 1.基本类型值 2.注入bean 3.内部bean 4.注入null值 5.级联属性 6.List,Set,Map集合的注入 7.properties文件的注入(和集合注入基本是 ...
随机推荐
- 对程序"加料"
我们如果想对已有的程序做手脚,就要在原有的结构中添加自己的代码,这样当用户在打开这个做过手脚的程序时就会自动运行其中我们加进去的代码,至于这些代码能做什么,你懂得.这个实验的目的是在一个EXE可执行文 ...
- 解题(StringJiaMi--字符串加密)
题目描述 有一种技巧可以对数据进行加密,它使用一个单词作为它的密匙.下面是它的工作原理:首先,选择一个单词作为密匙,如TRAILBLAZERS.如果单词中包含有重复的字母,只保留第1个,其余几个丢弃. ...
- 六、Prototype 原型设计模式
需求:使用 new 生成实例需要指定类名,在不指定类的情况下生成实例 代码清单: 原型接口 Product: public interface Product extends Cloneable{ v ...
- display:none和visibility:hidden
display:none和visibility:hidden的区别在哪儿? “这个问题简单?”我心里头暗自得意,按耐住自己得意又紧张的小心脏,自信满满地说,“这两个声明都可以让元素隐藏,不同之处在于d ...
- CSS clip:rect矩形剪裁功能及应用
.clip{ position:absolute; clip: rect(10px 30px 20px 10px); } 最后有必要说明下:clip:rect矩形剪裁只能作用于position:abs ...
- dedecms(织梦系统)如何更新手机版首页模板文件
https://jingyan.baidu.com/article/ad310e80e4b1dd1849f49e8f.html
- serv-U使用
该软件是设置ftp服务器的 可以百度查询ftp服务器安装攻略,如 https://jingyan.baidu.com/article/cb5d6105c00bba005c2fe0ca.html 问题: ...
- docker容器和镜像
这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行中的容器之间的区别. 当我对Docker技术还是一知半解的时候,我 ...
- PHP连接数据库(mysql)
前端链接后台,数据库几乎必不可少.所以本文总结了PHP链接数据库的常用方法步骤. 首先 链接数据库:mysqli_connect参数①主机地址 ②mysql用户名③nysql密码④选择连接的数据库⑤端 ...
- Delphi: TLabel设置EllipsisPosition属性用...显示过长文本时,以Hint显示其全文本
仍然是处理多语言中碰到问题. Delphi自2006版以后,TLabel有了EllipsisPosition属性,当长文本超过其大小时,显示以...,如下图: 这样虽然解决显示问题,但很显然,不知道. ...