Spring中有个很重要的知识点,就是Porperties。
spring中的配置文件XXX.properties天然是key-value的映射,文件读取后存储在SpringProperties对象中。
java.util中有个Properties类专门用于这种场景配置文件的读取和存储
查看源码:
public class Properties extends Hashtable<Object,Object> {
/**
* use serialVersionUID from JDK 1.1.X for interoperability
*/
private static final long serialVersionUID = 4112578634029874840L;
/**
* A property list that contains default values for any keys not
* found in this property list.
*
* @serial
*/
protected Properties defaults;
/**
* Creates an empty property list with no default values.
*/
public Properties() {
this(null);
}
/**
* Creates an empty property list with the specified defaults.
*
* @param defaults the defaults.
*/
public Properties(Properties defaults) {
this.defaults = defaults;
}
}
很显然Properties类继承了之前讲过的Hashtable,而且作为Hashtable的子类,直接使用了Hashtable的get/put方法。
普通的获取配置方法 getProperty(String key)
/**
* Searches for the property with the specified key in this property list.
* If the key is not found in this property list, the default property list,
* and its defaults, recursively, are then checked. The method returns
* {@code null} if the property is not found.
*
* @param key the property key.
* @return the value in this property list with the specified key value.
* @see #setProperty
* @see #defaults
*/
public String getProperty(String key) {
Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}
就是调用了父类的get方法,
其余的load/store方法是为了读取配置/保存配置使用的,不在这次的集合讲解范围之内。研究spring的时候再仔细的看一下。