Sping源码(九)—— Bean的初始化(非懒加载)— ConversionService

news2024/9/21 19:42:11

序言

经过前面一系列的加载、解析等准备工作,此刻refresh方法的执行已经来到了尾声,接下来我们用几篇文章着重的介绍一下Bean的初始化

代码

着重看refresh()主流程中的finishBeanFactoryInitialization()方法。
finishBeanFactoryInitialization
方法首先会判断beanFactory中是否包含ConversionService,并设置属性。

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
		// Initialize conversion service for this context.
		// 如果包含ConversionService,则赋值给conversionService变量
		if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
				beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
			beanFactory.setConversionService(
					beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
		}
		//省略部分源码...
	}

扩展:ConversionService

我们经常会在页面进行各种各样的输入 abcd,1234等 这些输入是如何变换成指定的类型的呢?。
看看beanFacroty设置的ConversionService是什么?如果我们想要自定义ConversionService改如何实现?

ConversionService
Spring的提供的ConversionService接口,里面包含判断canConvert()方法判断是否可以进行类型转换,convert方法进行类型的转换。接口的实现类有很多,我们主要看DefaultConversionService即可。

/**
 * 类型转换服务
 */
public interface ConversionService {

	/**
	 *  判断sourceType是否能转换成targetType
	 */
	boolean canConvert(@Nullable Class<?> sourceType, Class<?> targetType);
	
	boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType);

	@Nullable
	<T> T convert(@Nullable Object source, Class<T> targetType);

	@Nullable
	Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType);
}

DefaultConversionService
DefaultConversionService下分别有addDefaultConvertersaddCollectionConvertersaddScalarConverters三个方法,会在DefaultConversionService初始化时添加各种类型的Converter,以便我们使用时直接拿。我们看一下源码:

public class DefaultConversionService extends GenericConversionService {

	@Nullable
	private static volatile DefaultConversionService sharedInstance;

	public DefaultConversionService() {
		addDefaultConverters(this);
	}

	public static void addDefaultConverters(ConverterRegistry converterRegistry) {
		addScalarConverters(converterRegistry);
		addCollectionConverters(converterRegistry);
		
		converterRegistry.addConverter(new StringToTimeZoneConverter());
		//添加各种Converter。。。。
	}

	public static void addCollectionConverters(ConverterRegistry converterRegistry) {
		ConversionService conversionService = (ConversionService) converterRegistry;
		
		converterRegistry.addConverter(new ArrayToCollectionConverter(conversionService));
		//添加各种Converter。。。。
	}

	private static void addScalarConverters(ConverterRegistry converterRegistry) {
		//省略部分源码
		converterRegistry.addConverterFactory(new StringToNumberConverterFactory());
		//添加各种ConverterFacroty。。。。
	}
}

Converter接口

addDefaultConverters方法。
StrinToimeZoneConverter
类继承了Converter,而Converter接口中的convert方法会将参数S 转换成 T类型

class StringToTimeZoneConverter implements Converter<String, TimeZone> {

	@Override
	public TimeZone convert(String source) {
		return StringUtils.parseTimeZoneString(source);
	}
}

@FunctionalInterface
public interface Converter<S, T> {

	/** S 转换成 T类型
	 * Convert the source object of type {@code S} to target type {@code T}.
	 * @param source the source object to convert, which must be an instance of {@code S} (never {@code null})
	 * @return the converted object, which must be an instance of {@code T} (potentially {@code null})
	 * @throws IllegalArgumentException if the source cannot be converted to the desired target type
	 */
	@Nullable
	T convert(S source);
}

ConditionalGenericConverter

addCollectionConverters方法。
ArrayToCollectionConverter
类继承关系
ArrayToCollectionConverter -》ConditionalGenericConverter -》 GenericConverter(ConditionalConverter)

其中ConditionalConverter接口中的match方法可以理解成@Confitional注解,根据传入的 sourceType 和 targetType 来判断是否符合转换。

GenericConverter中的convert方法,会将 source 通过 sourceType的描述 转换成 targetType类型

final class ArrayToCollectionConverter implements ConditionalGenericConverter {
	@Override
	public Set<ConvertiblePair> getConvertibleTypes() {
		return Collections.singleton(new ConvertiblePair(Object[].class, Collection.class));
	}

	@Override
	public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
		return ConversionUtils.canConvertElements(
				sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor(), this.conversionService);
	}
	
	public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
		// 。。。。。。。省略
	}
}
public interface ConditionalGenericConverter extends GenericConverter, ConditionalConverter {

}
public interface ConditionalConverter {

	boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType);
}
public interface GenericConverter {

	/**
	 * Return the source and target types that this converter can convert between.
	 * <p>Each entry is a convertible source-to-target type pair.
	 * <p>For {@link ConditionalConverter conditional converters} this method may return
	 * {@code null} to indicate all source-to-target pairs should be considered.
	 */
	@Nullable
	Set<ConvertiblePair> getConvertibleTypes();

	/**
	 * Convert the source object to the targetType described by the {@code TypeDescriptor}.
	 * @param source the source object to convert (may be {@code null})
	 * @param sourceType the type descriptor of the field we are converting from
	 * @param targetType the type descriptor of the field we are converting to
	 * @return the converted object
	 */
	@Nullable
	Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType);


	/**
	 * suorceType 和 targetType的键值对
	 * Holder for a source-to-target class pair.
	 */
	final class ConvertiblePair {

		private final Class<?> sourceType;

		private final Class<?> targetType;
		public ConvertiblePair(Class<?> sourceType, Class<?> targetType) {
			this.sourceType = sourceType;
			this.targetType = targetType;
		}

		public Class<?> getSourceType() {
			return this.sourceType;
		}

		public Class<?> getTargetType() {
			return this.targetType;
		}
	}
}

TypeDescriptor
包含常见基础的类型类和包装类。

public class TypeDescriptor implements Serializable {

	private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];

	private static final Map<Class<?>, TypeDescriptor> commonTypesCache = new HashMap<>(32);

	private static final Class<?>[] CACHED_COMMON_TYPES = {
			boolean.class, Boolean.class, byte.class, Byte.class, char.class, Character.class,
			double.class, Double.class, float.class, Float.class, int.class, Integer.class,
			long.class, Long.class, short.class, Short.class, String.class, Object.class};

	static {
		for (Class<?> preCachedClass : CACHED_COMMON_TYPES) {
			commonTypesCache.put(preCachedClass, valueOf(preCachedClass));
		}
	}
}

ConverterFactory

StringToNumberConverterFactory

final class StringToNumberConverterFactory implements ConverterFactory<String, Number> {

	@Override
	public <T extends Number> Converter<String, T> getConverter(Class<T> targetType) {
		return new StringToNumber<>(targetType);
	}

	private static final class StringToNumber<T extends Number> implements Converter<String, T> {

		private final Class<T> targetType;

		public StringToNumber(Class<T> targetType) {
			this.targetType = targetType;
		}

		@Override
		public T convert(String source) {
			if (source.isEmpty()) {
				return null;
			}
			return NumberUtils.parseNumber(source, this.targetType);
		}
	}
}

ConverterFactory
ConverterFactory转换会支持将 S 转换成 T 类型 (T instance of R)。

public interface ConverterFactory<S, R> {

	/**
	 * 获取转换器
	 *
	 * Get the converter to convert from S to target type T, where T is also an instance of R.
	 * @param <T> the target type
	 * @param targetType the target type to convert to
	 * @return a converter from S to T
	 */
	<T extends R> Converter<S, T> getConverter(Class<T> targetType);
}

图解

在这里插入图片描述

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

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

相关文章

python深入探索斐波那契数列:代码示例与不满足的外围条件

新书上架~&#x1f447;全国包邮奥~ python实用小工具开发教程http://pythontoolsteach.com/3 欢迎关注我&#x1f446;&#xff0c;收藏下次不迷路┗|&#xff40;O′|┛ 嗷~~ 目录 一、斐波那契数列的初步实现 二、外围条件的不满足情况 总结 一、斐波那契数列的初步实现 …

二叉树——堆的实现

一.前言 前面我们讲解了二叉树的概念以及二叉树的存储结构&#xff1a;https://blog.csdn.net/yiqingaa/article/details/139224974?spm1001.2014.3001.5502 今天我们主要讲讲二叉树的存储结构&#xff0c;以及堆的实现。 二.正文 1.二叉树的顺序结构及实现 1.1二叉树的顺序…

五款屏幕监控软件测评,哪一款屏幕监控软件适合企业

市场上有多款屏幕监控软件&#xff0c;其中一些因其实用的功能、良好的用户体验和广泛的用户基础而获得了较高的市场占有率和口碑。以下几款是较为受欢迎且功能全面的屏幕监控软件&#xff1a; 1、安企神软件&#xff1a; 领取7天试用版&#xff1a;https://work.weixin.qq.co…

【NumPy】关于numpy.sort()函数,看这一篇文章就够了

&#x1f9d1; 博主简介&#xff1a;阿里巴巴嵌入式技术专家&#xff0c;深耕嵌入式人工智能领域&#xff0c;具备多年的嵌入式硬件产品研发管理经验。 &#x1f4d2; 博客介绍&#xff1a;分享嵌入式开发领域的相关知识、经验、思考和感悟&#xff0c;欢迎关注。提供嵌入式方向…

一致 VS 正确

“代码命名时&#xff0c;一致大于正确” 本文适合以下小伙伴阅读&#xff1a; 经常会有疑惑&#xff1a;这谁的单词拼错了&#xff0c;我之后要将错就错吗&#xff1f;时常感觉到&#xff1a;项目中的命名好乱啊&#xff0c;明明是一个东西怎么一堆不同的名字 好了&#xf…

企业选择定制化MES管理系统时需要考虑的核心功能

在当今制造业的数字化转型浪潮中&#xff0c;企业对于实现生产现场透明管理的需求愈发迫切。为了满足这一需求&#xff0c;MES管理系统成为了众多企业的首选解决方案。MES管理系统以其高度的灵活性和可定制性&#xff0c;能够根据不同行业的特性&#xff0c;为企业提供量身定制…

告别繁琐!Xinstall助你轻松实现APP地推结算,提升推广效率

随着移动互联网的迅猛发展&#xff0c;APP市场竞争日益激烈。面对线上推广转化率下降、成本上升的挑战&#xff0c;越来越多的APP厂商开始尝试线下地推这一更为直接、有效的推广方式。然而&#xff0c;地推结算过程中的种种问题却让许多企业头痛不已。今天&#xff0c;我们将为…

信息化项目必须进行验收测试吗?软件测试公司验收测试流程分享

信息化项目验收是指在软件开发完成之后&#xff0c;对其进行独立检查和确认&#xff0c;以确定它是否达到了预期的质量和功能需求。在进行验收之前&#xff0c;必须进行验收测试&#xff0c;这是非常重要的一步。 为什么要进行验收测试呢&#xff1f;好处可不少哦&#xff01;…

【uniapp】CSS实现多行文本展开收起的文字环绕效果

1. 效果图 收起状态 展开状态 2. 代码实现 <view class"word-wrap" id"descriptionTxt"><view class"fold-text" v-if"isFold"><text class"fold-btn" click"changFold">全文</text&g…

人工智能应用层岗位—AI项目经理/AI产品经理

AI是一门技术&#xff0c;最终落实成产品才能具备商业价值 应用层&#xff0c;就是面向特定应用场景&#xff0c;形成人工智能软硬件产品或解决方案。主要包括行业AI解决方案和热门产品&#xff0c;如自动驾驶、机器人、智能家居、可穿戴的智能设备等 相应的&#xff0c;就会…

猫狗分类识别模型建立①数据标记

一、labelImg库说明 LabelImg是一款非常流行的图像标注工具&#xff0c;广泛用于机器学习和计算机视觉领域。以下是关于LabelImg的详细介绍&#xff1a; 主要功能和特点 1.图像标注 允许用户在图像中标注物体&#xff0c;选择特定区域&#xff0c;并为这些区域添加标签或类…

C语言基础——数组

{\▁/} ( / 。\ ) / ⊃&#x1f494;\⊃ 为什么我那么努力还是得不到那么多赞 ʕ • ᴥ • ʔ づ♡ど &#x1f389; 欢迎点赞支持&#x1f389; 个人主页&#xff1a;励志不掉头发的内向程序员&#xff1b; 专栏主页&#xff1a;C语言基础&#xff1b; 文章目录 前言…

Linux基础知识点总结!超详细

Linux 的学习对于一个IT工程师的重要性是不言而喻的&#xff0c;学好它是工程师必备修养之一。 Linux 基础 操作系统 操作系统Operating System简称OS&#xff0c;是软件的一部分&#xff0c;它是硬件基础上的第一层软件&#xff0c;是硬件和其它软件沟通的桥梁。 操作系统…

部署Prometheus + Grafana实现监控数据指标

1.1 Prometheus安装部署 Prometheus监控服务 主机名IP地址系统配置作用Prometheus192.168.110.27/24CentOS 7.94颗CPU 8G内存 100G硬盘Prometheus服务器grafana192.168.110.28/24CentOS 7.94颗CPU 8G内存 100G硬盘grafana服务器 监控机器 主机名IP地址系统配置k8s-master-0…

链接预测.

在某些场景中&#xff0c;用户可能希望预测给定节点之间是否存在边&#xff0c;这样的任务称作链接预测任务。 假设输入结点之间是全链接图&#xff0c;连接预测的目的是给边打上标签。 挑战 最简单的是用图的邻接矩阵来表示结点之间的边&#xff0c;但当节点数量多的时候&am…

【C++】从零开始map与set的封装

送给大家一句话&#xff1a; 今日的事情&#xff0c;尽心、尽意、尽力去做了&#xff0c;无论成绩如何&#xff0c;都应该高高兴兴地上床恬睡。 – 三毛 《亲爱的三毛》 &#x1f303;&#x1f303;&#x1f303;&#x1f303;&#x1f303;&#x1f303;&#x1f303;&#x…

自适应容积卡尔曼滤波|(自适应CKF)的MATLAB源代码

介绍 容积卡尔曼滤波在理论上拥有比UKF更高的精度和稳定性&#xff0c;本自适应算法通过对观测残差的计算&#xff0c;在观测协方差R不准确或无法获得时&#xff0c;对R进行调节&#xff0c;以起到降低估计误差的作用。 模型 使用的是三维的非线性模型&#xff0c;经过适当修…

Hunyuan-DiT环境搭建推理测试

引子 最近鹅厂竟然开源了一个多模态的大模型&#xff0c;之前分享福报厂的多模态视觉大模型&#xff08;Qwen-VL环境搭建&推理测试-CSDN博客&#xff09;感兴趣的可以移步。鹅厂开源的&#xff0c;我还是头一回部署。好的&#xff0c;那就让我们看看这个多模态视觉大模型有…

今日选题。

诱导读者点开文章的9引真经&#xff08;一&#xff09; 标题重要么&#xff1f;新媒体、博客文通常在手机上阅读。首先所有的内容不同于纸媒&#xff0c;手机只展现标题&#xff0c;而内容都是折叠。其次读者能像看内容一样看4、5条或者7、8条标题&#xff08;区别于不同的主流…

LayUI使用(一)点击树组件的右边空白区域也可响应事件

前提&#xff1a; 如下&#xff0c;希望能够点击右边的空白区域也能够响应&#xff0c;而不仅仅是点击文本才响应 分析流程 一开始问了chatgpt&#xff0c;但它给的方案太麻烦了&#xff0c;而且还有错误&#xff0c;因此自己上手F12进入调试模式&#xff0c;点击查看最终渲…