SpringBoot源码阅读(9)——转换服务

news2024/7/30 7:22:45

SpringBoot中加载转换服务的入口是SpringApplication实例方法prepareEnvironment,第341行。

configureEnvironment(environment, applicationArguments.getSourceArgs());
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
	if (this.addConversionService) {
		environment.setConversionService(new ApplicationConversionService());
	}
	configurePropertySources(environment, args);
	configureProfiles(environment, args);
}

注释:

按照该顺序委托给configurePropertySources(ConfigurationEnvironment,String[])和configureProfiles(ConfigurationEnvironment,String]])的模板方法。覆盖此方法以完全控制环境自定义,或者覆盖以上方法之一以分别对属性源或配置文件进行细粒度控制。

addConversionService 默认为true,则创建ApplicationConversionService

ApplicationConversionService

父类:FormattingConversionServicec,爷爷类:GenericConversionService
现接口:FormatterRegistry EmbeddedValueResolverAware ConverterRegistry ``EmbeddedValueResolverAware Aware

主要方法

private ApplicationConversionService(StringValueResolver embeddedValueResolver, boolean unmodifiable) {
	if (embeddedValueResolver != null) {
		setEmbeddedValueResolver(embeddedValueResolver);
	}
	configure(this);
	this.unmodifiable = unmodifiable;
}
public static void configure(FormatterRegistry registry) {
	DefaultConversionService.addDefaultConverters(registry);
	DefaultFormattingConversionService.addDefaultFormatters(registry);
	addApplicationFormatters(registry);
	addApplicationConverters(registry);
}

加转换器的主要方法:addPrinter addParser addFormatter addFormatterForFieldType addConverter addFormatterForFieldType addFormatterForFieldAnnotation addConverter addConverterFactory

添加的转换器工厂

org.springframework.core.convert.support.NumberToNumberConverterFactory
org.springframework.core.convert.support.StringToNumberConverterFactory
org.springframework.core.convert.support.CharacterToNumberFactory

org.springframework.core.convert.support.StringToEnumConverterFactory
org.springframework.core.convert.support.IntegerToEnumConverterFactory

添加的转换器

org.springframework.core.convert.support.ObjectToStringConverter
org.springframework.core.convert.support.StringToCharacterConverter
org.springframework.core.convert.support.NumberToCharacterConverter
org.springframework.core.convert.support.StringToBooleanConverter
org.springframework.core.convert.support.EnumToStringConverter
org.springframework.core.convert.support.EnumToIntegerConverter
org.springframework.core.convert.support.StringToLocaleConverter
org.springframework.core.convert.support.StringToCharsetConverter
org.springframework.core.convert.support.StringToCurrencyConverter
org.springframework.core.convert.support.StringToPropertiesConverter
org.springframework.core.convert.support.StringToUUIDConverter
org.springframework.core.convert.support.PropertiesToStringConverter

集合类转换器

org.springframework.core.convert.support.ArrayToCollectionConverter
org.springframework.core.convert.support.CollectionToArrayConverter
org.springframework.core.convert.support.ArrayToArrayConverter
org.springframework.core.convert.support.CollectionToCollectionConverter
org.springframework.core.convert.support.MapToMapConverter
org.springframework.core.convert.support.ArrayToStringConverter
org.springframework.core.convert.support.StringToArrayConverter
org.springframework.core.convert.support.ArrayToObjectConverter
org.springframework.core.convert.support.ObjectToArrayConverter
org.springframework.core.convert.support.CollectionToStringConverter
org.springframework.core.convert.support.StringToCollectionConverter
org.springframework.core.convert.support.CollectionToObjectConverter
org.springframework.core.convert.support.ObjectToCollectionConverter
org.springframework.core.convert.support.StreamConverter

其他转换器

org.springframework.core.convert.support.ByteBufferConverter
org.springframework.core.convert.support.StringToTimeZoneConverter
org.springframework.core.convert.support.ZoneIdToTimeZoneConverter
org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter
org.springframework.core.convert.support.ObjectToObjectConverter
org.springframework.core.convert.support.IdToEntityConverter
org.springframework.core.convert.support.FallbackObjectToStringConverter
org.springframework.core.convert.support.ObjectToOptionalConverter

属性注解工厂

org.springframework.format.number.NumberFormatAnnotationFormatterFactory
根据类路径判断加载javax.money.MonetaryAmount
org.springframework.format.number.money.CurrencyUnitFormatter
org.springframework.format.number.money.MonetaryAmountFormatter
org.springframework.format.number.money.Jsr354NumberFormatAnnotationFormatterFactory

日期转换器

org.springframework.format.datetime.DateFormatterRegistrar.DateToLongConverter
org.springframework.format.datetime.DateFormatterRegistrar.DateToCalendarConverter
org.springframework.format.datetime.DateFormatterRegistrar.CalendarToDateConverter
org.springframework.format.datetime.DateFormatterRegistrar.CalendarToLongConverter
org.springframework.format.datetime.DateFormatterRegistrar.LongToDateConverter
org.springframework.format.datetime.DateFormatterRegistrar.LongToCalendarConverter

org.springframework.format.datetime.standard.DateTimeConverters.LocalDateTimeToLocalDateConverter
org.springframework.format.datetime.standard.DateTimeConverters.LocalDateTimeToLocalTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.ZonedDateTimeToLocalDateConverter
org.springframework.format.datetime.standard.DateTimeConverters.ZonedDateTimeToLocalTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.ZonedDateTimeToLocalDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.ZonedDateTimeToOffsetDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.ZonedDateTimeToInstantConverter
org.springframework.format.datetime.standard.DateTimeConverters.OffsetDateTimeToLocalDateConverter
org.springframework.format.datetime.standard.DateTimeConverters.OffsetDateTimeToLocalTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.OffsetDateTimeToLocalDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.OffsetDateTimeToZonedDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.OffsetDateTimeToInstantConverter
org.springframework.format.datetime.standard.DateTimeConverters.CalendarToZonedDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.CalendarToOffsetDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.CalendarToLocalDateConverter
org.springframework.format.datetime.standard.DateTimeConverters.CalendarToLocalTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.CalendarToLocalDateTimeConverter
org.springframework.format.datetime.standard.DateTimeConverters.CalendarToInstantConverter
org.springframework.format.datetime.standard.DateTimeConverters.LongToInstantConverter
org.springframework.format.datetime.standard.DateTimeConverters.InstantToLongConverter

org.springframework.format.datetime.standard.InstantFormatter
org.springframework.format.datetime.standard.PeriodFormatter
org.springframework.format.datetime.standard.DurationFormatter
org.springframework.format.datetime.standard.YearFormatter
org.springframework.format.datetime.standard.MonthFormatter
org.springframework.format.datetime.standard.YearMonthFormatter
org.springframework.format.datetime.standard.MonthDayFormatter

org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory

日期解析和打印转换器,成对出现

org.springframework.format.datetime.standard.TemporalAccessorPrinter
org.springframework.format.datetime.standardTemporalAccessorParser

根据类路径判断加载org.joda.time.YearMonth

org.springframework.format.datetime.DateFormatterRegistrar.DateToLongConverter
org.springframework.format.datetime.DateFormatterRegistrar.DateToCalendarConverter
org.springframework.format.datetime.DateFormatterRegistrar.CalendarToDateConverter
org.springframework.format.datetime.DateFormatterRegistrar.CalendarToLongConverter
org.springframework.format.datetime.DateFormatterRegistrar.LongToDateConverter
org.springframework.format.datetime.DateFormatterRegistrar.LongToCalendarConverter

org.springframework.format.datetime.joda.JodaTimeConverters.DateTimeToLocalDateConverter
org.springframework.format.datetime.joda.JodaTimeConverters.DateTimeToLocalTimeConverter
org.springframework.format.datetime.joda.JodaTimeConverters.DateTimeToLocalDateTimeConverter
org.springframework.format.datetime.joda.JodaTimeConverters.DateTimeToDateMidnightConverter
org.springframework.format.datetime.joda.JodaTimeConverters.DateTimeToMutableDateTimeConverter
org.springframework.format.datetime.joda.JodaTimeConverters.DateTimeToInstantConverter
org.springframework.format.datetime.joda.JodaTimeConverters.DateTimeToDateConverter
org.springframework.format.datetime.joda.JodaTimeConverters.DateTimeToCalendarConverter
org.springframework.format.datetime.joda.JodaTimeConverters.DateTimeToLongConverter
org.springframework.format.datetime.joda.JodaTimeConverters.DateToReadableInstantConverter
org.springframework.format.datetime.joda.JodaTimeConverters.CalendarToReadableInstantConverter
org.springframework.format.datetime.joda.JodaTimeConverters.LongToReadableInstantConverter
org.springframework.format.datetime.joda.JodaTimeConverters.LocalDateTimeToLocalDateConverter
org.springframework.format.datetime.joda.JodaTimeConverters.LocalDateTimeToLocalTimeConverter

org.springframework.format.datetime.joda.PeriodFormatter
org.springframework.format.datetime.joda.DurationFormatter
org.springframework.format.datetime.joda.YearMonthFormatter
org.springframework.format.datetime.joda.MonthDayFormatter

org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory
根据类路径加载org.joda.time.YearMonth成对出现
org.springframework.format.datetime.joda.ReadablePartialPrinter
org.springframework.format.datetime.joda.LocalDateParser

org.springframework.format.datetime.joda.ReadablePartialPrinter
org.springframework.format.datetime.joda.LocalTimeParser

org.springframework.format.datetime.joda.ReadablePartialPrinter
org.springframework.format.datetime.joda.LocalDateTimeParser

org.springframework.format.datetime.joda.ReadableInstantPrinter
org.springframework.format.datetime.joda.DateTimeParser
如果类路径中没有org.joda.time.YearMonth
org.springframework.format.datetime.DateFormatterRegistrar.DateToLongConverter
org.springframework.format.datetime.DateFormatterRegistrar.DateToCalendarConverter
org.springframework.format.datetime.DateFormatterRegistrar.CalendarToDateConverter
org.springframework.format.datetime.DateFormatterRegistrar.CalendarToLongConverter
org.springframework.format.datetime.DateFormatterRegistrar.LongToDateConverter
org.springframework.format.datetime.DateFormatterRegistrar.LongToCalendarConverter

org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory
formatter
org.springframework.boot.convert.CharArrayFormatter
org.springframework.boot.convert.InetAddressFormatter
org.springframework.boot.convert.IsoOffsetFormatter

支持分隔字符串的转换器

org.springframework.boot.convert.ArrayToDelimitedStringConverter	 
org.springframework.boot.convert.CollectionToDelimitedStringConverter
org.springframework.boot.convert.DelimitedStringToArrayConverter
org.springframework.boot.convert.DelimitedStringToCollectionConverter

springboot其他的转换器

org.springframework.boot.convert.StringToDurationConverter
org.springframework.boot.convert.DurationToStringConverter
org.springframework.boot.convert.NumberToDurationConverter
org.springframework.boot.convert.DurationToNumberConverter
org.springframework.boot.convert.StringToPeriodConverter
org.springframework.boot.convert.PeriodToStringConverter
org.springframework.boot.convert.NumberToPeriodConverter
org.springframework.boot.convert.StringToDataSizeConverter
org.springframework.boot.convert.NumberToDataSizeConverter
org.springframework.boot.convert.StringToFileConverter
org.springframework.boot.convert.InputStreamSourceToByteArrayConverter

org.springframework.boot.convert.LenientStringToEnumConverterFactory
org.springframework.boot.convert.LenientBooleanToEnumConverterFactory

这些类或者放入或者经过包装放入到GenericConversionServiceconverters
然后ApplicationConversionService经过AbstractEnvironmentpropertyResolver放入了AbstractPropertyResolverconversionService属性中。

propertyResolver 类型是ConfigurationPropertySourcesPropertyResolver,父类是AbstractPropertyResolver.
conversionService类型是ConfigurableConversionService

protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
	MutablePropertySources sources = environment.getPropertySources();
	if (!CollectionUtils.isEmpty(this.defaultProperties)) {
		DefaultPropertiesPropertySource.addOrMerge(this.defaultProperties, sources);
	}
	if (this.addCommandLineProperties && args.length > 0) {
		String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
		if (sources.contains(name)) {
			PropertySource<?> source = sources.get(name);
			CompositePropertySource composite = new CompositePropertySource(name);
			composite.addPropertySource(
					new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
			composite.addPropertySource(source);
			sources.replace(name, composite);
		}
		else {
			sources.addFirst(new SimpleCommandLinePropertySource(args));
		}
	}
}

如果defaultProperties默认属性有值,则合并到sources
如果命令行参数有,则合并到sources中。

protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {}

注释:

配置此应用程序环境的活动配置文件(或默认情况下为活动配置文件)。在配置文件处理过程中,可以通过spring.profiles.active属性激活其他配置文件

public static void attach(Environment environment) {
	Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
	MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources();
	PropertySource<?> attached = getAttached(sources);
	if (attached == null || !isUsingSources(attached, sources)) {
		attached = new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME,
				new SpringConfigurationPropertySources(sources));
	}
	sources.remove(ATTACHED_PROPERTY_SOURCE_NAME);
	sources.addFirst(attached);
}

查询名称为configurationProperties属性集合,如果没有就把sources包装成ConfigurationPropertySourcesPropertySource放入sources,放在首位。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1917230.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Firealpaca 解锁版下载及安装教程 (火焰羊驼绘画软件)

前言 FireAlpaca是一款简单易用的电脑绘画软件&#xff0c;采用了类似于Photoshop的图层绘画方式。对于喜欢手绘和创作漫画的朋友来说&#xff0c;FireAlpaca的多图层功能使得绘画过程更加便捷和简单。作为一个小型图像编辑软件&#xff0c;它能够轻松处理多个图层或手绘图&am…

拥抱UniHttp,规范Http接口对接之旅

前言 如果你项目里还在用传统的编程式Http客户端比如HttpClient、Okhttp去直接对接第三方Http接口&#xff0c; 那么你项目一定充斥着大量的对接逻辑和代码&#xff0c; 并且针对不同的对接渠道方需要每次封装一次调用的简化&#xff0c; 一旦封装不好系统将会变得难以维护&am…

策略模式(大话设计模式)C/C++版本

策略模式 商场收银软件 根据客户所购买商品的单价和数量来收费 需求分析&#xff1a; 1. 输入单价数量 > 界面逻辑 2. 计算&#xff08;可能打折或者促销&#xff09; > 业务逻辑 3. 输出结果 > 界面逻辑感觉和计算器的逻辑流程差不多&#xff0c;可以用简单工厂模式…

浪潮天启防火墙TQ2000远程配置方法SSL-xxx、L2xx 配置方法

前言 本次设置只针对配置VXX&#xff0c;其他防火墙配置不涉及。建议把防火墙内外网都调通后再进行Vxx配置。 其他配置可参考&#xff1a;浪潮天启防火墙配置手册 配置SSLVxx 在外网端口开启SSLVxx信息 开启SSLVxx功能 1、勾选 “启用SSL-Vxx” 2、设置登录端口号&#xff0…

Unity3D 太空大战射击游戏

一、前言 本案例是初级案例&#xff0c;意在帮助想使用unity的初级开发者能较快的入门&#xff0c;体验unity开发的方便性和简易性能。 本次我们将使用团结引擎进行开发&#xff0c;帮助想体验团结引擎的入门开发者进行较快的环境熟悉。 本游戏案例以太空作战为背景&#xff0c…

如何分析软件测试中发现的Bug!

假如你是一名软件测试工程师&#xff0c;每天面对的就是那些“刁钻”的Bug&#xff0c;它们像是隐藏在黑暗中的敌人&#xff0c;时不时跳出来给你一个“惊喜”。那么&#xff0c;如何才能有效地分析和处理这些Bug&#xff0c;让你的测试工作变得高效且有趣呢&#xff1f;今天我…

Threadlocal使用获取最后更新人信息

Threadlocal 的作用范围是一个线程&#xff0c;tomcat启动默认开启一个线程 首先点击登录&#xff0c;登录方法会返回token 拿到token后放在请求头中发送商品的插入请求&#xff0c;在插入是设置拿到token中的nickName&#xff08;花名&#xff09;放入&#xff08;lastUpdate…

C 语言中如何实现字符串的拼接?

&#x1f345;关注博主&#x1f397;️ 带你畅游技术世界&#xff0c;不错过每一次成长机会&#xff01; &#x1f4d9;C 语言百万年薪修炼课程 【https://dwz.mosong.cc/cyyjc】通俗易懂&#xff0c;深入浅出&#xff0c;匠心打磨&#xff0c;死磕细节&#xff0c;6年迭代&…

轻松搭建RAG:澳鹏RAG开发工具

我们很高兴地宣布推出RAG开发工具&#xff0c;这是澳鹏大模型智能开发平台的一项新功能。此功能可帮助团队轻松创建高质量的检索增强生成 (RAG) 模型。 什么是 RAG&#xff1f; 检索增强生成 (RAG) 通过利用大量外部数据源&#xff08;例如企业的知识库&#xff09;显著增强了…

git查看版本,查看安装路径、更新版本

git version 查看版本 git update-git-for-windows 更新版本 git version 查看版本

美客多卖家必备:自养号测评补单技术的实战策略

构建美客多&#xff08;MercadoLibre&#xff09;自养号测评体系的稳健策略 一、确立目标与前期筹备 深入理解平台规范&#xff1a;首要任务是深入研究美客多平台的规则与指导方针&#xff0c;确保所有行动均符合平台要求&#xff0c;避免任何违规行为导致账号受限。 明确测评…

光电门验证动量守恒实验

本实验所需器件与第二个实验相同。但是连线方式有所区别&#xff0c;先将Arduino的电源输出接到两个光电门&#xff0c;然后再将光电门1的信号输出线接到Arduino的第10个端口&#xff0c;光电门2的信号输出线接到Arduino的第11个端口。对Arduino写入下列程序&#xff08;只有主…

删除【此电脑】中设备和驱动器下的迅雷下载方法

删除【此电脑】中设备和驱动器下的迅雷下载方法 我们安装迅雷下载、百度网盘、WPS等软件后&#xff0c;在【此电脑】–> 【设备和驱动器】目录下会看到这些驱动器的快捷方式&#xff0c;可以使用删除注册表的方式删除这些东西 启动注册表管理器 首先使用键盘快捷键 Win …

新零售起盘案例「半藏酱酒」布局路径,半藏总院分院招商模式

在当前白酒市场中&#xff0c;一款名为半藏酒的酒品以其独特的新零售模式引起了广泛关注。这种模式不同于传统销售方式&#xff0c;通过多种创新玩法&#xff0c;实现了销售与品牌推广的双重目标&#xff0c;让我们一起来看看细节。 半藏酒的分级代理制度将代理商分为两个层级&…

“未来城市发展之窗”2024上海城博会

随着2024年上海城市博览会的临近&#xff0c;招商工作正火热进行中&#xff0c;且已逐渐接近尾声。这场被誉为“城市未来之窗”的盛会&#xff0c;汇聚了全球各地的城市管理者、建筑师、规划师、投资者以及科技创新者&#xff0c;共同探讨城市发展的未来趋势和解决方案。 一、城…

JavaWeb-js(4)

js事件 在前端页面中&#xff0c;js程序大多数是由事件来驱动的&#xff0c;当触发某些事件的时候&#xff0c;可以使用js负责响应。 js事件由三部分组成: 事件源——》指的是被触发的对象; 事件类型——》如何触发的事件&#xff0c;如:鼠标单击、双击、键盘操作等;…

2024最新最全【Java】全栈,零基础入门到精通

Java基础 本文章是作者的学习笔记&#xff0c;帮助初学者快速入门&#xff0c;内容讲的不是很细&#xff0c;适合初学者&#xff0c;不定时更新。 目录 Java基础数据类型1.基本类型(primitive type)1-1 整数类型1-2 浮点类型1-3 字符类型1-4 boolean类型 2.引用数据类型3.类型…

2024.7.11最新版IDM破解,操作简单

前言 IDM的强劲对手&#xff0c;100%免费&#xff0c;如果破解IDM失败&#xff0c;推荐使用FDM&#xff0c;下载地址&#xff1a;Free Download Manager 破解步骤 打开PowerShell&#xff0c;非CMD 在左下角开始菜单右键点击后选择PowerShell&#xff0c;注意不是打开CMD。…

大模型时代的基础架构:大模型算力中心建设指南

&#x1f482; 个人网站:【 摸鱼游戏】【网址导航】【神级代码资源网站】&#x1f91f; 一站式轻松构建小程序、Web网站、移动应用&#xff1a;&#x1f449;注册地址&#x1f91f; 基于Web端打造的&#xff1a;&#x1f449;轻量化工具创作平台&#x1f485; 想寻找共同学习交…

hbase学习

hbase学习 hbase概述&#xff1a; HBase 是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统&#xff0c;用于存储海量的结构化或者半结构化&#xff0c;非结构化的数据&#xff08;底层是字节数组做存储的&#xff09; HBase是Hadoop的生态系统之一&#xff0c;是建立在…