加载一个由两个不同的类加载器实例化的对象时遇到问题。基本上我实现了一个带有三个不同插件的webapp,每个插件都有自己的类加载器。项目结构如下:如何使用两个不同的类加载器加载同一个类
MyAppService
- ObjectInterface.java
MyAppImpl
- ObjectImplementation.java
MyClass
- MyClass.java
它是一个基于maven的项目。 MyAppImpl
和MyClass
都具有MyAppService
作为依赖性。现在,我使用此代码创建类ObjectImplementation
中类ObjectImplementation
的对象。
ObjectInterface o = new ObjectImplementation();
我想这个对象传递给方法在Myclass
类,在这里我得到这个代码的对象。
ObjectInterface o = (ObjectInterface) passedObject;
但我得到了异常java.lang.ClassCastException: MyAppImpl.ObjectImplementation cannot be cast to MyAppService.ObjectInterface
。我也尝试动态类加载与代码
ObjectInterface o = (ObjectInterface) Class.forName("MyAppImpl.ObjectImplementation").newInstance();
但我得到例外java.lang.ClassNotFoundException: MyAppImpl.ObjectImplementation
。添加MyAppImpl作为MyClass的依赖项目前不是一种选择。有谁知道我的问题是否有解决方案?