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