欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring源码:ConversionService类解析

程序员文章站 2022-05-20 09:37:25
...

整体类图:一般来说,用户可以使⽤用内置的或者⾃己实现Converter接⼝来实现类型转换,ConversionService类接口内部调用Converter接口实现类来实现类型转换

Spring源码:ConversionService类解析

  • 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源码