类加载器的父亲委托机制
- 在父亲委托机制中,各个类加载器按照父子关系形成了树形结构,除了根类加载器之外,其余的类加载器都有且只有一个父加载器。
- Bootstrap ClassLoader 启动类加载器
- Extension ClassLoader 扩展类加载器
- App ClassLoader 系统类加载器
eg1:
public class MyTest7 {
public static void main(String[] args)throws Exception {
Class<?> clazz=Class.forName("java.lang.String");
System.out.println(clazz.getClassLoader());
Class<?> clazzc=Class.forName("com.aaa.test.c");
System.out.println(clazzc.getClassLoader());
}
}
class c{
}
//输出结果:
null
sun.misc.Launcher$AppClassLoader@18b4aac2
不同类加载器作用和加载器动作分析
- 获取一个类的父加载器
public class MyTest13 {
public static void main(String[] args) {
ClassLoader classLoader=ClassLoader.getSystemClassLoader();
System.out.println(classLoader);
while(classLoader!=null){
classLoader=classLoader.getParent();
System.out.println(classLoader);
}
}
}
//输出结果:
sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$ExtClassLoader@512ddf17
null
- 获取资源路径
/**
* @author lillcol
* 2019/5/22-17:09
*/
public class MyTEST14 {
public static void main(String[] args) throws IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
System.out.println(classLoader);
String resourceName = "com/aaa/test/MyTest13.class";
Enumeration<URL> urls = classLoader.getResources(resourceName);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
System.out.println(url);
}
}
}
//输出结果:
sun.misc.Launcher$AppClassLoader@18b4aac2
file:/E:/IdeaProjects/AAAE2E2/target/scala-2.11/classes/com/aaa/test/MyTest13.class
- 几种加载器的获取方法
/**
* @author lillcol
* 2019/5/22-17:09
*/
public class MyTEST14 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Class<?> clazz=Class.forName("com.aaa.test.MyTEST14");
ClassLoader classLoader1 = clazz.getClassLoader(); //获得当前类的ClassLoader
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); //获得当前线程上下文的ClassLoader
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); //获得系统的ClassLoader
System.out.println(classLoader1);
System.out.println(contextClassLoader);
System.out.println(systemClassLoader);
}
}
//输出结果:
sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$AppClassLoader@18b4aac2
/**
* @author lillcol
* 2019/5/22-17:09
*/
public class MyTEST14 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Class<?> clazz=MyTEST14.class;
System.out.println(clazz.getClassLoader());
System.out.println("----------");
Class<?> clazz1=String.class;
System.out.println(clazz1.getClassLoader());
}
}
//输出结果:
sun.misc.Launcher$AppClassLoader@18b4aac2
----------
null
本文为学习张龙老师深入理解JVM的笔记与心得,转载请注明出处!!!