雙親委派的優點:
如果有人想要篡改程序的類實現,在這種機制下是無效的,因為這些系統的類已經被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