1 Star 0 Fork 156

南风不竞 / Spring-Analysis

forked from huifer / Code-Analysis 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Spring-DefaultConversionService.md 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
huifer 提交于 2020-11-09 16:53 . :fire:core/ convert + attributeAccessor

Spring DefaultConversionService

  • 类全路径: org.springframework.core.convert.support.DefaultConversionService
  • 类图

DefaultConversionService.png

  • 构造方法分析
    • org.springframework.core.convert.support.DefaultConversionService#DefaultConversionService
public DefaultConversionService() {
   addDefaultConverters(this);
}

	public static void addDefaultConverters(ConverterRegistry converterRegistry) {
		addScalarConverters(converterRegistry);
		addCollectionConverters(converterRegistry);

		converterRegistry.addConverter(new ByteBufferConverter((ConversionService) converterRegistry));
		converterRegistry.addConverter(new StringToTimeZoneConverter());
		converterRegistry.addConverter(new ZoneIdToTimeZoneConverter());
		converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter());

		converterRegistry.addConverter(new ObjectToObjectConverter());
		converterRegistry.addConverter(new IdToEntityConverter((ConversionService) converterRegistry));
		converterRegistry.addConverter(new FallbackObjectToStringConverter());
		converterRegistry.addConverter(new ObjectToOptionalConverter((ConversionService) converterRegistry));
	}
  • 从类图上我们可以知道 DefaultConversionService 实现了ConverterRegistry , 这这里构建阶段的时候会放入spring中编写好的convert实现类. 方便后续使用

  • 该类还有一个对外方法 getSharedInstance

public static ConversionService getSharedInstance() {
   DefaultConversionService cs = sharedInstance;
   if (cs == null) {
      synchronized (DefaultConversionService.class) {
         cs = sharedInstance;
         if (cs == null) {
            cs = new DefaultConversionService();
            sharedInstance = cs;
         }
      }
   }
   return cs;
}
  • 从代码上也不难看出也是创建 DefaultConversionService 将spring中编写好的convert放入容器等待后续使用.
Java
1
https://gitee.com/limengcanyu/spring-analysis.git
git@gitee.com:limengcanyu/spring-analysis.git
limengcanyu
spring-analysis
Spring-Analysis
master

搜索帮助