类加载器

什么是类加载器

就是类的快递员

有几种

  • 启动类加载器 Bootstrap (C++)

加载最基本的Java类,例如Java.lang中的类,提供用户的基本使用环境,是根加载器

  • 拓展类加载器 Extension  (Java)

加载的是随着时代变化,新添加到Java中的拓展类,例如Javax库中的类就是由其加载。

  • 应用程序类加载器 AppClassLoader

用户自定义的类加载。
image.png

Test t = new Test();
t.getClass().getClassLoader().getParent().getParent();	=> null (Bootstrap)
t.getClass().getClassLoader().getParent();				=> ExtClassLoader
t.getClass().getClassLoader();							=> AppClassLoader

双亲委派

双亲委派就是指类加载的过程中,先不会加载本类,而是先把加载请求委派给父类。所以实际上类的加载是自顶向下,从最顶部的启动类加载器部分开始找,先到先得。
优先往上找,先到先用。作用是保护源代码,防止用户定义的重名的类污染Java原生代码。

沙箱安全

双亲委派就是为了沙箱安全机制,保护原生Java的源码

posted @ 2020-03-27 18:19  Bankarian  阅读(131)  评论(0编辑  收藏  举报