使用场景:项目中遇到使用不同版本的类时候因为同路径同名类的冲突可以使用自定义类加载器进行加载规避这一问题
相关代码如下:
代码
1. 类加载器:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @Description TODO
* @Date 2020/3/10 15:05
* @Created by hzhu
*/
public class MyClassLoader extends ClassLoader {
public MyClassLoader() {
}
public MyClassLoader(ClassLoader parent) {
super(parent);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
File file = new File("F:\\javaworkspace\\baseplat\\Car.class");
try {
byte[] classBytes = getContent(file);
Class<?> aClass = this.defineClass(name, classBytes, 0, classBytes.length);
return aClass;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 读取class文件
* @param file
* @return
* @throws Exception
*/
private byte[] getContent(File file) throws Exception {
long length = file.length();
if (length > Integer.MAX_VALUE) {
throw new RuntimeException("文件太大!");
}
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[(int)length];
int offset = 0;
int num = 0;
while (offset < buffer.length && (num = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += num;
}
if (offset != buffer.length) {
throw new IOException("无法完全读取");
}
fis.close();
return buffer;
}
}
2. 使用示例
public class Demo {
public static void main(String[] args) {
MyClassLoader loader = new MyClassLoader();
try {
Class<?> aClass = Class.forName("Car", true, loader);
Object o = aClass.newInstance();
System.out.println(o);
System.out.println(o.getClass().getClassLoader());
Method method = o.getClass().getDeclaredMethod("getName");
Object invoke = method.invoke(o);
System.out.println(invoke);
} catch (Exception e) {
e.printStackTrace();
}
}
}
局限
但是该方法对于整个jar包的加载及有依赖的方法调用情况下有很大局限,因此可以使用URLClassLoader进行加载