android 双亲委托机制,Android ClasslLoader双亲委派加载机制

本文详细介绍了Java的类加载器,包括BootstrapClassLoader、ExtensionClassLoader和AppClassLoader的层次结构,以及双亲委派机制的工作原理。在类加载过程中,先由父类加载器尝试加载,只有当父类加载失败时,子类加载器才会尝试加载,以此确保系统类的安全性,防止恶意代码篡改。此外,还提到了自定义类加载器的使用场景。
摘要由CSDN通过智能技术生成

双亲委派的优点:

如果有人想要篡改程序的类实现,在这种机制下是无效的,因为这些系统的类已经被BootStrapClassLoader加载过了,不会再次加载,从一定程度上防止了危险代码的植入。

java文件编译完全会生成 .class文件, .class文件就是通过类加载器ClassLoader加载的。ClassLoader在加载过程中会用双亲委派机制加载 .class文件。

一一一一一一一一一一一

CustomClassLoader ( 自己搞的,可以不用,用appClassLoader基本够用)

一一一一一一一一一一一

👇

一一一一一一一一一一一

AppClassLoader (应用程序类加载器,appclassloader会加载*Java环境变量 CLASSPATH所指定的路径下的类库 * ,而CLASSPATH所指定的路径可以通过 System.getProperty("java.class.path") 获取,该变量可被覆盖,可以使用 -cp 参数)

一一一一一一一一一一一

👇

一一一一一一一一一一一

ExtClassLoader(扩展类加载器,会加载 ( $ JAVA_HOME/jre/lib/ext)下的类库)

一一一一一一一一一一一

👇

一一一一一一一一一一一

BootStrapClassLoader(启动类加载器,JVM启动时创建,用于加载

math?formula=JAVA_HOME /JRE/LIB 下面的类库)

一一一一一一一一一一一

ClassLoader的双亲委派机制:

1.当appclassloader加载一个class时,会先把类加载请求委派给父类的加载器ExtClassLoader去完成加载;

2.当ExtClassLoader加载一个class时,会先把类加载请求委派给父类的加载器BootStrapClassLoader去完成加载;

3.如果BootStrapClassLoader加载失败,会使用ExtClassLoader来尝试加载;

4.如果ExtClassLoader加载失败,会使用AppClassLoader来 加载,如果AppClassLoader也加载失败,则会抛出异常ClassNotFoundException

/**

* Loads the class with the specified binary name. The

* default implementation of this method searches for classes in the

* following order:

*

*

*

*

Invoke {@link #findLoadedClass(String)} to check if the class

* has already been loaded.

*

*

Invoke the {@link #loadClass(String) loadClass} method

* on the parent class loader. If the parent is null the class

* loader built-in to the virtual machine is used, instead.

*

*

Invoke the {@link #findClass(String)} method to find the

* class.

*

*

*

*

If the class was found using the above steps, and the

* resolve flag is true, this method will then invoke the {@link

* #resolveClass(Class)} method on the resulting Class object.

*

*

Subclasses of ClassLoader are encouraged to override {@link

* #findClass(String)}, rather than this method.

*

*

* @param name

* The binary name of the class

*

* @param resolve

* If true then resolve the class

*

* @return The resulting Class object

*

* @throws ClassNotFoundException

* If the class could not be found

*/

// Android-removed: Remove references to getClassLoadingLock

// Remove perf counters.

//

//

Unless overridden, this method synchronizes on the result of

// {@link #getClassLoadingLock getClassLoadingLock} method

// during the entire class loading process.

protected Class> loadClass(String name, boolean resolve)

throws ClassNotFoundException

{

// First, check if the class has already been loaded

Class> c = findLoadedClass(name);

if (c == null) {

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.

c = findClass(name);

}

}

return c;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值