现象
在使用URLClassLoader进行远程类加载的时候,有时候强转的时候发现明明使用的是同一个类,也会报强转失败,类似于com.abc.demo.ClassA can not cast to com.abc.demo.ClassA。
原因
这是因为在使用URLClassLoader进行远程加载时,和本身加载与内存中的类的加载器(ClassLoader)不一致导致的。如果使用URLClassLoader不指定父类加载器的话所有类的加载都是URLClassLoader,而制定了父类加载器的时候则优先使用父类加载器去进行加载,如果没有该类则使用URLClassLoader去加载
解决方案
使用URLClassLoader进行记载类的时候指定父类加载器(父-类加载器,断句在父后边)。
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{
url}, this.getClass().getClassLoader());
远程加载jar包例子
File jarFile = new File(deviceProtocol.getProtocolUrl());
URL url;
try {
url = jarFile.toURI().toURL();
} catch (Exception e) {
throw new ServiceException("URL错误,加载不到jar");
}
if (null != url) {
try {
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{
url}, this.getClass().getClassLoader());
Class<?> codec = urlClassLoader.loadClass(deviceProtocol.getMainClass());
Object instance = codec.getClass().forName(deviceProtocol.getMainClass(), true, urlClassLoader).newInstance();
Method method = codec.getMethod("create");
// 加载协议包,创建实体对象
ProtocolSupport protocolSupport = (ProtocolSupport) method.invoke(instance);
if (!StringUtils.equals(protocolSupport.getId(), deviceProtocol.getProtocolId())) {
throw new ServiceException("jar包内定义的ID与协议ID不匹配");
}
memoryProtocolSupportRegistry.publishProtocol(protocolSupport);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new ServiceException("jar包解析异常");
}
}