Spring 知识点 : 简单属性/值类型

本文介绍了Spring框架中关于简单值类型和简单属性的概念。简单值类型包括基本数据类型、枚举类型、CharSequence、Number、Date等。简单属性则在此基础上增加了数组类型的支持,但数组元素必须为简单值类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring世界里,有些类型会被认为是简单类型,相应地,有些属性会被认为是简单属性。那么到底哪些类型或者属性会被Spring认为是简单的呢 ?这些概念在BeanUtils工具类中有所定义 :

1. 什么是简单值类型 ?

	/**
	 * Check if the given type represents a "simple" value type:
	 * a primitive, an enum, a String or other CharSequence, a Number, a Date,
	 * a URI, a URL, a Locale or a Class.
	 * @param clazz the type to check
	 * @return whether the given type represents a "simple" value type
	 */
	public static boolean isSimpleValueType(Class<?> clazz) {
		return (ClassUtils.isPrimitiveOrWrapper(clazz) ||
				Enum.class.isAssignableFrom(clazz) ||
				CharSequence.class.isAssignableFrom(clazz) ||
				Number.class.isAssignableFrom(clazz) ||
				Date.class.isAssignableFrom(clazz) ||
				URI.class == clazz || URL.class == clazz ||
				Locale.class == clazz || Class.class == clazz);
	}

由此可见,基本数据类型及其包装类型,枚举类型/CharSequence/Number/Date及其实现子类,URI,URL,Locale,Class等都被Spring认为是简单值类型。

2. 什么是简单属性 ?

	/**
	 * Check if the given type represents a "simple" property:
	 * a primitive, a String or other CharSequence, a Number, a Date,
	 * a URI, a URL, a Locale, a Class, or a corresponding array.
	 * Used to determine properties to check for a "simple" dependency-check.
	 * @param clazz the type to check
	 * @return whether the given type represents a "simple" property
	 */
	public static boolean isSimpleProperty(Class<?> clazz) {
		Assert.notNull(clazz, "Class must not be null");
		return isSimpleValueType(clazz) 
			|| (clazz.isArray() && isSimpleValueType(clazz.getComponentType()));
	}

由此可见,以下类型的属性被Spring认为是简单属性:

  • 简单值类型
  • 数组类型,但数组的每个元素是简单值类型

注意 : 本文在这里仅仅介绍Spring对各种类型简单/复杂程度是如何理解的一个知识点,并不介绍该知识点如何被应用。但是,Spring既然对各种类型的简单/复杂程度有所区分,说明这一点很重要,也一定会在某处使用该概念。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值