同步操作将从 turnon/blog 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
title: Spring 类型转换
date: 2022-12-22 19:43:59
categories:
- Java
- 框架
- Spring
- Spring核心
tags:
- Java
- 框架
- Spring
permalink: /pages/6662dc/
场景 | 基于 JavaBeans 接口的类型转换实现 | Spring 3.0+ 通用类型转换实现 |
---|---|---|
数据绑定 | YES | YES |
BeanWrapper | YES | YES |
Bean 属性类型转换 | YES | YES |
外部化属性类型转换 | NO | YES |
核心职责
扩展原理
內建扩展(org.springframework.beans.propertyeditors 包下)
转换场景 | 实现类 |
---|---|
String -> Byte 数组 | org.springframework.beans.propertyeditors.ByteArrayPropertyEditor |
String -> Char | org.springframework.beans.propertyeditors.CharacterEditor |
String -> Char 数组 | org.springframework.beans.propertyeditors.CharArrayPropertyEditor |
String -> Charset | org.springframework.beans.propertyeditors.CharsetEditor |
String -> Class | org.springframework.beans.propertyeditors.ClassEditor |
String -> Currency | org.springframework.beans.propertyeditors.CurrencyEditor |
扩展模式
java.beans.PropertyEditorSupport
类实现 org.springframework.beans.PropertyEditorRegistrar
registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)
方法PropertyEditorRegistrar
实现注册为 Spring Bean向 org.springframework.beans.PropertyEditorRegistry
注册自定义 PropertyEditor 实现
registerCustomEditor(Class<?>, PropertyEditor)
registerCustomEditor(Class<?>, String, PropertyEditor)
违反职责单一原则
java.beans.PropertyEditor
接口职责太多,除了类型转换,还包括 Java Beans 事件和 Java GUI 交
互java.beans.PropertyEditor
实现类型局限
java.lang.String
类型java.beans.PropertyEditor
实现缺少类型安全
类型转换接口 - org.springframework.core.convert.converter.Converter<S,T>
通用类型转换接口 - org.springframework.core.convert.converter.GenericConverter
內建扩展
转换场景 | 实现类所在包名(package) |
---|---|
日期/时间相关 | org.springframework.format.datetime |
Java 8 日期/时间相关 | org.springframework.format.datetime.standard |
通用实现 | org.springframework.core.convert.support |
局限一:缺少 Source Type 和 Target Type 前置判断
局限二:仅能转换单一的 Source Type 和 Target Type
org.springframework.core.convert.converter.GenericConverter
核心要素 | 说明 |
---|---|
使用场景 | 用于“复合”类型转换场景,比如 Collection、Map、数组等 |
转换范围 | Set<ConvertiblePair> getConvertibleTypes() |
配对类型 | org.springframework.core.convert.converter.GenericConverter.ConvertiblePair |
转换方法 | convert(Object,TypeDescriptor,TypeDescriptor) |
类型描述 | org.springframework.core.convert.TypeDescriptor |
GenericConverter 局限性
GenericConverter 优化接口 - ConditionalGenericConverter
org.springframework.core.convert.converter.GenericConverter
org.springframework.core.convert.converter.ConditionalConverter
实现转换器接口
org.springframework.core.convert.converter.Converter
org.springframework.core.convert.converter.ConverterFactory
org.springframework.core.convert.converter.GenericConverter
注册转换器实现
ConversionServiceFactoryBean
Spring Beanorg.springframework.core.convert.ConversionService API
org.springframework.core.convert.ConversionService
实现类型 | 说明 |
---|---|
GenericConversionService |
通用 ConversionService 模板实现,不内置转化器实现 |
DefaultConversionService |
基础 ConversionService 实现,内置常用转化器实现 |
FormattingConversionService |
通用 Formatter + GenericConversionService 实现,不内置转化器和 Formatter 实现 |
DefaultFormattingConversionService |
DefaultConversionService + 格式化 实现(如:JSR-354 Money & Currency, JSR-310 Date-Time) |
类型转换器底层接口 - org.springframework.beans.TypeConverter
org.springframework.beans.TypeConverterSupport
org.springframework.beans.SimpleTypeConverter
类型转换器底层抽象实现 - org.springframework.beans.TypeConverterSupport
org.springframework.beans.TypeConverter
org.springframework.beans.PropertyEditorRegistrySupport
org.springframework.beans.TypeConverterDelegate
类型转换器底层委派实现 - org.springframework.beans.TypeConverterDelegate
org.springframework.beans.AbstractNestablePropertyAccessor
实现
org.springframework.beans.BeanWrapperImpl
java.beans.PropertyEditor
实现
PropertyEditorRegistrySupport#registerDefaultEditors
org.springframework.core.convert.ConversionService
实现Spring 类型转换实现有哪些?
Spring 类型转换器接口有哪些?
org.springframework.core.convert.converter.Converter
org.springframework.core.convert.converter.GenericConverter
org.springframework.core.convert.converter.ConditionalConverter
org.springframework.core.convert.converter.ConditionalGenericConverter
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。