如果项目在本地的类路径中并且我执行 getClass().getClassLoader(),我有以下代码可以很好地工作。但是,当我尝试从外部 jar 动态加载时,它会失败。
它无法加载 MyType,这是方法参数上的类型,如下所示:
@MyAnnotation("FOO.BAR")
public Object echo(@Valid MyType param) throws Exception {
return...;
}
此代码将失败,并显示“org.reflections.ReflectionsException:无法获取名称 MyType 的类型”:
myLoader = new URLClassLoader(new URL[]{new URL("file:///"+jarfile)});
myLoaders.Add(myLoader);
ConfigurationBuilder builder = new ConfigurationBuilder()
.addClassLoaders(loaders)
.setScanners(new MethodAnnotationsScanner())
.setUrls(ClasspathHelper.forPackage(myPackage, myLoaders));
Reflections reflections = new Reflections(builder);
return reflections.getMethodsAnnotatedWith(MyAnnotation.class);
我相信问题出在类加载器上。有没有另一种方法来加载一个罐子,这样它就可以拾取所有类型?