Java获取ClassLoader的方式

homePage

ClassLoader.getSystemClassLoader();
Thread.currentThread().getContextClassLoader();

classLoader.getResources(FILTER_PROPERTY_NAME);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private Properties loadFilterConfig() throws IOException {
Properties filterProperties = new Properties();

loadFilterConfig(filterProperties, ClassLoader.getSystemClassLoader());
loadFilterConfig(filterProperties, Thread.currentThread().getContextClassLoader());

return filterProperties;
}

private void loadFilterConfig(Properties filterProperties, ClassLoader classLoader) throws IOException {
if (classLoader == null) {
return;
}

for (Enumeration<URL> e = classLoader.getResources(FILTER_PROPERTY_NAME); e.hasMoreElements();) {
URL url = e.nextElement();

Properties property = new Properties();

InputStream is = null;
try {
is = url.openStream();
property.load(is);
} finally {
if (is != null) {
is.close();
}
}

filterProperties.putAll(property);
}
}