【发布时间】:2013-08-19 09:25:27
【问题描述】:
我正在尝试动态加载一个 java 类。基本思想是,一个 jar 包含在运行时动态加载的模块。我就是这样做的(我知道这很 hacky,但是没有其他方法可以将 jar 动态添加到已经存在的类加载器 afaik):
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
method.setAccessible(true);
method.invoke(moduleLoader, new Object[] { file.toURI().toURL() });
Class fooClass = moduleLoader.loadClass("com.coderunner.Foo");
Object foo = fooClass.newInstance();
每个模块都使用@Module 注释进行注释。因此,为了获得有关模块的更多信息,我尝试获取注释。问题是 foo 上的注释是 com.sun.$Proxy$27 类型而不是 com.coderunner.Module ,因此我得到一个
ClassCastException: Cannot cast com.sun.proxy.$Proxy42 (id=64) to com.coderunner.Module
我不得不说我有点困惑这里发生了什么。我想做的事可能吗?怎么样?
编辑:我也许还应该提到我正在 spring/spring-mvc 和 tomcat 环境中尝试这个。
【问题讨论】:
标签: java spring annotations classloader urlclassloader