类加载器

作者: 强某某 | 来源:发表于2020-03-05 19:18 被阅读0次
  1. 类加载器
    类加载器负责装入类,搜索网络、jar、zip、文件夹、二进制数据、内存等指定位置的类资源。一个java程序运行,最少有三个类加载器实例,负责不同类的加载


    1.png

通过JDK-API进行查看:java.lang.Class.getClassLoader()
返回装载类的类加载器
如果这个类是由bootstrapClassLoader加载的,那么这个方法在这种实现中将返回null

public class ClassLoaderView {

    public static void main(String[] args) throws ClassNotFoundException {
        //加载核心类库的BootStrap ClassLoader
        System.out.println("核心类库加载器:"+ClassLoaderView.class.getClassLoader().loadClass("java.lang.String").getClassLoader());
        //加载扩展库的Extension ClassLoader
        System.out.println("扩展类库加载器:"+ClassLoaderView.class.getClassLoader().loadClass("com.sun.nio.zipfs.ZipCoder").getClassLoader());
        //加载应用程序的
        System.out.println("应用程序加载器:"+ClassLoaderView.class.getClassLoader());
        //双亲委派机制 Parents Delegation Model
        System.out.println("应用程序加载器的父类:"+ClassLoaderView.class.getClassLoader().getParent());
        System.out.println("应用程序加载器的父类的父类:"+ClassLoaderView.class.getClassLoader().getParent().getParent());
        /**
         * 核心类库加载器:null
         * 扩展类库加载器:sun.misc.Launcher$ExtClassLoader@17327b6
         * 应用程序加载器:sun.misc.Launcher$AppClassLoader@b4aac2
         * 应用程序加载器的父类:sun.misc.Launcher$ExtClassLoader@17327b6
         * 应用程序加载器的父类的父类:null
         */
    }
}
  1. 双亲委派模型


    2.png

由上图可知:双亲委派可以解决安全问题,例如,有用于自己定的Object类,如果不是双亲委派从上到下查找,则Object会自动覆盖系统的Object,这样就很危险了

相关文章

网友评论

    本文标题:类加载器

    本文链接:https://www.haomeiwen.com/subject/pjucrhtx.html