Properties能方便的读取properties文件内容。
但是在使用中要注意:避免在其他地方直接使用getProperty(key)。以下是关键代码:
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;
}
public synchronized V get(Object key) {
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return e.value;
}
}
return null;
}
说明:因为加了锁的原因,在并发条件下,会有性能问题。解决方法:可以采用存入HashMap的方式,统一获取方法,直接从map中获取。