双亲委派的优点:
如果有人想要篡改程序的类实现,在这种机制下是无效的,因为这些系统的类已经被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启动时创建,用于加载 /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 <a href="#name">binary name</a>. The
* default implementation of this method searches for classes in the
* following order:
*
* <ol>
*
* <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
* has already been loaded. </p></li>
*
* <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
* on the parent class loader. If the parent is <tt>null</tt> the class
* loader built-in to the virtual machine is used, instead. </p></li>
*
* <li><p> Invoke the {@link #findClass(String)} method to find the
* class. </p></li>
*
* </ol>
*
* <p> If the class was found using the above steps, and the
* <tt>resolve</tt> flag is true, this method will then invoke the {@link
* #resolveClass(Class)} method on the resulting <tt>Class</tt> object.
*
* <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link
* #findClass(String)}, rather than this method. </p>
*
*
* @param name
* The <a href="#name">binary name</a> of the class
*
* @param resolve
* If <tt>true</tt> then resolve the class
*
* @return The resulting <tt>Class</tt> object
*
* @throws ClassNotFoundException
* If the class could not be found
*/
// Android-removed: Remove references to getClassLoadingLock
// Remove perf counters.
//
// <p> Unless overridden, this method synchronizes on the result of
// {@link #getClassLoadingLock <tt>getClassLoadingLock</tt>} 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;
}
打破双亲委派机制:
https://blog.csdn.net/cy973071263/article/details/104129163