spring中type转换框架设计与实现

1、概略图

2、接口设计

主要包含Converter及Formatter

ConverterRegistry:主要是添加、删除Converter以及ConverterFactory。 

ConversionService:是作转换及判断是否可以作转换的服务

ConfigurableConversionService:继承了ConverterRegistry和ConversionService,既提供添加Converter功能,也提供转换功能

GenericConversionService:ConfigurableConversionService接口的实现类,提供通用的转换服务

FormatterRegistry:支持添加Printer,Parser,Formatter

GenericConverter:通用的转换器,提供转换对

ConditionalConverter:有条件的转换器

ConditionalGenericConverter:有条件的通用转换器

DefaultConversionService:GenericConversionService的子类,提供基础的转换,即默认添加以下转换

  • NumberToNumberConverterFactory(数字之间的转换)
  • StringToNumberConverterFactory(字符串到数字的转换)
  • StringToCharacterConverter(String转Character)
  • ObjectToStringConverter(Character转String)
  • NumberToCharacterConverter(数字转Character)
  • CharacterToNumberFactory(Character转数字)
  • StringToBooleanConverter(字符串转布尔)
  • ObjectToStringConverter(布尔转字符串)
  • StringToEnumConverterFactory(字符串转枚举)
  • EnumToStringConverter(枚举转字符串)
  • IntegerToEnumConverterFactory(整数转枚举)
  • EnumToIntegerConverter(枚举转整数)
  • StringToLocaleConverter(字符串转Locale)
  • ObjectToStringConverter(Loccal转字符串)
  • StringToCharsetConverter(字符串转Charset)
  • ObjectToStringConverter(Charset转字符串)
  • StringToCurrencyConverter(字符串转Currrency)
  • ObjectToStringConverter(Currency转字符串)
  • StringToPropertiesConverter(字符串转Properties)
  • PropertiesToStringConverter(Properties转字符串)
  • StringToUUIDConverter(字符串转UUID)
  • ObjectToStringConverter(UUID转字符串)
  • ArrayToCollectionConverter(数组转集合)
  • CollectionToArrayConverter(集合转数组)
  • ArrayToArrayConverter(数组转数组)
  • CollectionToCollectionConverter(集合转集合)
  • MapToMapConverter(映射转映射)
  • ArrayToStringConverter(数组转字符串)
  • StringToArrayConverter(字符串转数组)
  • ArrayToObjectConverter(数组转Object)
  • ObjectToArrayConverter(Object转数组)
  • CollectionToStringConverter(集合转字符串)
  • StringToCollectionConverter(字符串转集合)
  • CollectionToObjectConverter(集合转Object)
  • ObjectToCollectionConverter(Object转集合)
  • StreamConverter(流与集合或者数组转换)
  • ByteBufferConverter(ByteBuffer与Object或者byte[]之间转换)
  • StringToTimeZoneConverter(字符串转TimeZone)
  • ZoneIdToTimeZoneConverter(ZoneId转TimeZone)
  • ZonedDateTimeToCalendarConverter(ZonedDateTime转Calendar)
  • ObjectToObjectConverter(Object转Object)
  • IdToEntityConverter(Id转Entity)
  • FallbackObjectToStringConverter(对象转字符串)
  • ObjectToOptionalConverter(对象转Optional)

 FormattingConversionService:实现FormatterRegistry并继承DefaultConversionService,除了提供DefaultConversionService功能,还提供对象与String之间的转换,即Printer和Parser,以及支持基于注解的转换。

DefaultFormattingConversionService:除了DefaultConversionService有的转换器外,还提供了

  • NumberFormatAnnotationFormatterFactory,基于注解NumberFormat的转换
  • CurrencyUnitFormatter,CurrencyUnit的打印及解析
  • MonetaryAmountFormatter,MonetaryAmount的打印及解析
  • Jsr354NumberFormatAnnotationFormatterFactory, 基于Jsr35的注解NumberFormat的转换
  • DateTimeFormatterRegistrar,提供LocalTime,LocalTime,LocalDateTime,ZonedDateTime,OffsetDateTime,OffsetTime,Instant,
    Period,Duration,Year,Month,YearMonth,MonthDay,Jsr310DateTimeFormatAnnotationFormatterFactory的解析及打印
  • JodaTimeFormatterRegistrar,提供Joda的打印及解析
  • DateFormatterRegistrar:提供Calender及DateTimeFormatAnnotationFormatterFactory

 Printer将对象转为字符串

Parser将字符串转为对象

Formatter是Printer和Parser接口的继承,支持对象与字符串的互转

组件关系图为

3、配置ConversionService

通过ConversionServiceFactory或者ConversionServiceFactoryBean

4、SpringBoot中的BeanFactory中ConversionService初始化

在SpringApplication中的postProcessApplicationContext

其时序 为

5、mvc中ConversionService初始化

mvc中使用的是FormattingConversionService

 6、使用场景

6.1 数据绑定

protected void doBind(MutablePropertyValues mpvs) {
	checkAllowedFields(mpvs);
	checkRequiredFields(mpvs);
	applyPropertyValues(mpvs);
}

 最终调用 typeConverterDelegate.convertIfNecessary

6.2 创建bean

填充属性时AbstractAutowireCapableBeanFactory.applyPropertyValues
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值