在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
既然对各种类型的简单/复杂程度有所区分,说明这一点很重要,也一定会在某处使用该概念。