Spring源码:ConversionService类解析
程序员文章站
2022-05-20 09:37:25
...
整体类图:一般来说,用户可以使⽤用内置的或者⾃己实现Converter接⼝来实现类型转换,ConversionService类接口内部调用Converter接口实现类来实现类型转换
-
ConversionService接口:类型转换的服务接口,这是转换系统的入口,调用convert(Object, Class)进行一次线程安全的类型转换。
public interface ConversionService { boolean canConvert(Class<?> sourceType, Class<?> targetType); boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType); // 转换过程:首先获取合适的转换器(DefaultConversionService会配置默认的转换器,也可自定义),然后进行转换 <T> T convert(Object source, Class<T> targetType); Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType); }
-
ConverterRegistry接口:对转换器进行注册,对于ConversionService和ConverterRegistry的主要实现类是GenericConversionService。
public interface ConverterRegistry { <S, T> void addConverter(Class<S> sourceType, Class<T> targetType, Converter<? super S, ? extends T> converter); void addConverter(GenericConverter converter); void addConverterFactory(ConverterFactory<?, ?> factory); void removeConvertible(Class<?> sourceType, Class<?> targetType); }
-
**GenericConversionService类:**ConversionService和ConverterRegistry的主要实现类,GenericConversionService维护了一个内部类的对象Converters。
public class GenericConversionService implements ConfigurableConversionService { private final Converters converters = new Converters(); private final Map<ConverterCacheKey, GenericConverter> converterCache = new ConcurrentReferenceHashMap<ConverterCacheKey, GenericConverter>(64); // ...... } public interface ConfigurableConversionService extends ConversionService, ConverterRegistry { }
-
DefaultConversionService类:默认转换器配置的GenericConversionService继承类,适用于大多数环境的转换器。spring的注册类型转换器时,是通过GenericConversionService的子类DefaultConversionService实现的
public class DefaultConversionService extends GenericConversionService { // ...... }
-
ConversionServiceFactoryBean类:一个工厂类,可以方便地访问配置了适用于大多数环境的转换器的ConversionService。
public class ConversionServiceFactoryBean implements FactoryBean<ConversionService>, InitializingBean { private Set<?> converters; private GenericConversionService conversionService; public void setConverters(Set<?> converters) { this.converters = converters; } @Override public void afterPropertiesSet() { this.conversionService = createConversionService(); ConversionServiceFactory.registerConverters(this.converters, this.conversionService); } protected GenericConversionService createConversionService() { return new DefaultConversionService(); } @Override public ConversionService getObject() { return this.conversionService; } @Override public Class<? extends ConversionService> getObjectType() { return GenericConversionService.class; } @Override public boolean isSingleton() { return true; } }
用法,如:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="example.MyCustomConverter"/> </list> </property> </bean>
推荐阅读
-
【spring-boot 源码解析】spring-boot 依赖管理
-
【spring-boot 源码解析】spring-boot 依赖管理梳理图
-
spring5 源码深度解析----- 事务的回滚和提交(100%理解事务)
-
Spring5源码解析5-ConfigurationClassPostProcessor (上)
-
spring5 源码深度解析----- AOP代理的生成
-
Spring5源码解析4-refresh方法之invokeBeanFactoryPostProcessors
-
源码解析Spring 数据库异常抽理知识点总结
-
spring5 源码深度解析----- 被面试官给虐懵了,竟然是因为我不懂@Configuration配置类及@Bean的原理
-
Spring MVC源码(三) ----- @RequestBody和@ResponseBody原理解析
-
基于Spring注解的上下文初始化过程源码解析(一)