自定义类加载器并打破双亲委派机制
【摘要】
前言 今天我们来自定义一个类加载器 第一步 继承ClassLoader类
public class MyClassLoader extends ClassLoader
1
第二步 重写findCl...
前言
今天我们来自定义一个类加载器
第一步
继承ClassLoader类
public class MyClassLoader extends ClassLoader
- 1
第二步
重写findClass方法
public Class<?> findClass(String name) throws ClassNotFoundException{
try {
byte[] data = loadByte(name);
return defineClass(name, data ,0,data.length);
}catch (Exception e){
e.printStackTrace();
throw new ClassNotFoundException();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
其中defineClass类是核心,需要传入类名以及类的路径data,所以需要对类进行一个类路径的获取
private byte[] loadByte(String name) throws Exception{
name = name.replaceAll("\\.","/");
FileInputStream fis = new FileInputStream("/" + name + ".class");
int len = fis.available();
byte[] data = new byte[len];
fis.read(data);
fis.close();
return data;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
最后写一个main方法
public static void main(String[] args) throws Exception{
MyClassLoaderTest.MyClassLoader classLoader = new MyClassLoaderTest.MyClassLoader("D:\test");
Class clazz = classLoader.loadClass("User");
Object obj = clazz.newInstance();
Method method = clazz.getDeclaredMethod("sout",null);
method.invoke(obj,null);
System.out.println(clazz.getClassLoader().getClass().getName());
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
其中我们将User类的class文件放到D:\test文件夹下,其中User类中有一个sout方法,方法中输出一句话证明我们调用成功了,总代码如下
import java.io.FileInputStream;
import java.lang.reflect.Method;
public class MyClassLoader extends ClassLoader{
private byte[] loadByte(String name) throws Exception{
name = name.replaceAll("\\.","/");
FileInputStream fis = new FileInputStream("/" + name + ".class");
int len = fis.available();
byte[] data = new byte[len];
fis.read(data);
fis.close();
return data;
}
public Class<?> findClass(String name) throws ClassNotFoundException{
try {
byte[] data = loadByte(name);
return defineClass(name, data ,0,data.length);
}catch (Exception e){
e.printStackTrace();
throw new ClassNotFoundException();
}
}
public static void main(String[] args) throws Exception{
MyClassLoaderTest.MyClassLoader classLoader = new MyClassLoaderTest.MyClassLoader("D:\test");
Class clazz = classLoader.loadClass("User");
Object obj = clazz.newInstance();
Method method = clazz.getDeclaredMethod("sout",null);
method.invoke(obj,null);
System.out.println(clazz.getClassLoader().getClass().getName());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
运行结果如下
但是每一次都会使用自定义的类加载器的父类去加载文件,所以我们要打破双亲委派机制,我们找到jdk自带的类ClassLoader下的loadClass方法,将他复制到我们的项目中
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
其中这几句代码的作用就是实现了双亲委派机制,很明显他的意思是如果父加载器不为空就调用父类加载器,如果父类加载器为空就使用引导类BootstrapClass加载器加载,我们将这端代码删除就可以打破双亲委派机制了。
文章来源: xiaoqijava.blog.csdn.net,作者:学无止境小奇,版权归原作者所有,如需转载,请联系作者。
原文链接:xiaoqijava.blog.csdn.net/article/details/112723618
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)