类似于 Java 1.5 中的 ServiceLoader 吗? [英] Is something similar to ServiceLoader in Java 1.5?
问题描述
如何在运行时在实现定义接口的类路径中发现类?
How do I discover classes at runtime in the classpath which implements a defined interface?
ServiceLoader 很适合(我想,我还没用过),但我需要在 Java 1.5 中使用它.
ServiceLoader suits well (I think, I haven't used it), but I need do it in Java 1.5.
推荐答案
Java 1.5 中没有为此内置任何内容.我自己实现了;这不是太复杂.但是,当我们升级到 Java 6 时,我将不得不用对 ServiceLoader
的调用替换对我的实现的调用.我本可以在应用程序和加载器之间定义一个小桥梁,但我只在少数地方使用它,而包装器本身将是 ServiceLoader 的一个很好的候选者.
There's nothing built into Java 1.5 for this. I implemented it myself; it's not too complicated. However, when we upgrade to Java 6, I will have to replace calls to my implementation with calls to ServiceLoader
. I could have defined a little bridge between the app and the loader, but I only use it in a few places, and the wrapper itself would be a good candidate for a ServiceLoader.
这是核心思想:
public <S> Iterable<S> load(Class<S> ifc) throws Exception {
ClassLoader ldr = Thread.currentThread().getContextClassLoader();
Enumeration<URL> e = ldr.getResources("META-INF/services/" + ifc.getName());
Collection<S> services = new ArrayList<S>();
while (e.hasMoreElements()) {
URL url = e.nextElement();
InputStream is = url.openStream();
try {
BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while (true) {
String line = r.readLine();
if (line == null)
break;
int comment = line.indexOf('#');
if (comment >= 0)
line = line.substring(0, comment);
String name = line.trim();
if (name.length() == 0)
continue;
Class<?> clz = Class.forName(name, true, ldr);
Class<? extends S> impl = clz.asSubclass(ifc);
Constructor<? extends S> ctor = impl.getConstructor();
S svc = ctor.newInstance();
services.add(svc);
}
}
finally {
is.close();
}
}
return services;
}
更好的异常处理留给读者作为练习.此外,该方法可以参数化以接受调用者选择的 ClassLoader.
Better exception handling is left as an exercise for the reader. Also, the method could be parameterized to accept a ClassLoader of the caller's choosing.
这篇关于类似于 Java 1.5 中的 ServiceLoader 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!