Spring源码:ConversionService类解析

 2019-10-17 21:31  阅读(3079)
文章分类:Spring boot

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

2019101710066\_1.png

  • **ConversionService接口:**类型转换的服务接口,这是转换系统的入口,调用convert(Object, Class)进行一次线程安全的类型转换。

    public interface ConversionService {
    
        boolean canConvert(Class sourceType, Class targetType);
    
        boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType);
    
          // 转换过程:首先获取合适的转换器(DefaultConversionService会配置默认的转换器,也可自定义),然后进行转换
         T convert(Object source, Class targetType);
    
        Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
        }
    
  • **ConverterRegistry接口:**对转换器进行注册,对于ConversionService和ConverterRegistry的主要实现类是GenericConversionService。

    public interface ConverterRegistry {
    
         void addConverter(Class sourceType, Class targetType, Converter 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 converterCache =
                new ConcurrentReferenceHashMap(64);
    
          // ......
        }
    
        public interface ConfigurableConversionService extends ConversionService, ConverterRegistry {
    
        }
    
  • **DefaultConversionService类:**默认转换器配置的GenericConversionService继承类,适用于大多数环境的转换器。spring的注册类型转换器时,是通过GenericConversionService的子类DefaultConversionService实现的

    public class DefaultConversionService extends GenericConversionService {
        // ......
        }
    
  • **ConversionServiceFactoryBean类:**一个工厂类,可以方便地访问配置了适用于大多数环境的转换器的ConversionService。

    public class ConversionServiceFactoryBean implements FactoryBean, 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 getObjectType() {
            return GenericConversionService.class;
        }
    
        @Override
        public boolean isSingleton() {
            return true;
        }
        }
    

    用法,如:


来源:[]()

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> Spring源码:ConversionService类解析

相关推荐