0.1、Springソースの学習-JVMクラスのキャリア-両親の委任モデル-Springクラスの負荷メカニズム
記事の目次 JVMクラス負荷機構 両親の委任モデル カスタムクラスのキャリアの原則:組み合わせは を引き継ぐのではなく Spring類負荷機構 入口AbstractAppration Contact.prepare BenFactory Default Resource Loader.get Class Loader() Class Utils.get Default Class Loader() 拡張読書:親の委任モデルを破壊する JVMクラスローディング機構
簡単に言えば、先に自分の親に必要なクラスをロードして、ずっと上に伝達しています.もし自分でキャリアをロードしなかったら、書き換えることができます.しかし、コアクラスは自分の種類のキャリアを使ってロードすることができません.例えば、jdkコアバッグjava.lang.Stringは、bootstrap Class Loaderだけがロードできます. BootStrap Class Loader:ブートクラスブートローダーと呼ばれる、Javaクラスローディングレベルの最上階のクラスローダーであり、JDKの中核クラスライブラリ、JAVA_をロードする責任があります.HOME/JRE/lib/例えば、rt.jar、reourcess.jar、charsets.jarなどの Extension Class Loader:拡張クラスブースターと称され、Javaの拡張クラスライブラリのローディングを担当しています.デフォルトではJAVA_をローディングします.HOME/JRE/lib/ext/目のすべてのjar App Class Loader:システムクラスのキャリアと称して、アプリケーションのクラスパスディレクトリのすべてのjarとクラスファイル をロードすることに責任を負います.
カスタムクラスのキャリアの原則:引き継ぎではなく組み合わせ
このようなキャリア間の親子関係は、一般的に継承された関係ではなく、親キャリアのコードを多重化するために組合せ関係を使用する.これは、あなたがカスタムクラスのキャリアにいる時でも、先に親のキャリア、つまりApp Class Loaderにローディングを依頼しなければならないという意味です.問題が来ました.両親がモデルを任命するのは一つの仕様です.直接にカスタムキャリアを使ってString類をロードしてもいいですか?いけません.JVMは検査ができますので、両親の委任モデルを守らなくても、コアクラスのクラスをロードできません.JVMは自分で検査します.
Springクラスローディング機構
入口AbstractAplication Contact.prepare BenFactory
org.spring frame ewark.co re.io.Default Resource Loader
org.springframe ework.util.lassUtils.get Default Class Loader()は中国語の注釈を見ます.
Tomcat類のキャリアはなぜ両親の委任モデルに背いていますか?
/**
*
*/
@Test
public void test2() {
// Test sun.misc.Launcher$AppClassLoader@4dc63996
ClassLoader c = Test.class.getClassLoader();
System.out.println(c);
// c sun.misc.Launcher$ExtClassLoader@28a418fc
ClassLoader c1 = c.getParent();
System.out.println(c1);
// getClassLoader() returning null indicates the bootstrap ClassLoader
// c1 ,null, Bootstrp loader C++ , java , Bootstrap Loader
// :Bootstrp loader
// C++ , Java , %JAVA_HOME%/jre/lib,
//* -Xbootclasspath %JAVA_HOME%/jre/classes 。
ClassLoader c2 = c1.getParent();
System.out.println(c2);
// ClassLoader sun.misc.Launcher$AppClassLoader@4dc63996
ClassLoader c3=ClassLoader.getSystemClassLoader();
System.out.println(c3);
//
ClassLoader c4= Thread.currentThread().getContextClassLoader();
System.out.println(c4);
}
両親委任モデル簡単に言えば、先に自分の親に必要なクラスをロードして、ずっと上に伝達しています.もし自分でキャリアをロードしなかったら、書き換えることができます.しかし、コアクラスは自分の種類のキャリアを使ってロードすることができません.例えば、jdkコアバッグjava.lang.Stringは、bootstrap Class Loaderだけがロードできます.
カスタムクラスのキャリアの原則:引き継ぎではなく組み合わせ
このようなキャリア間の親子関係は、一般的に継承された関係ではなく、親キャリアのコードを多重化するために組合せ関係を使用する.これは、あなたがカスタムクラスのキャリアにいる時でも、先に親のキャリア、つまりApp Class Loaderにローディングを依頼しなければならないという意味です.問題が来ました.両親がモデルを任命するのは一つの仕様です.直接にカスタムキャリアを使ってString類をロードしてもいいですか?いけません.JVMは検査ができますので、両親の委任モデルを守らなくても、コアクラスのクラスをロードできません.JVMは自分で検査します.
Springクラスローディング機構
入口AbstractAplication Contact.prepare BenFactory
beanFactory.setBeanClassLoader(getClassLoader());
Default Resource Loader.get Class Loader()org.spring frame ewark.co re.io.Default Resource Loader
/**
* Return the ClassLoader to load class path resources with.
* Will get passed to ClassPathResource's constructor for all
* ClassPathResource objects created by this resource loader.
* @see ClassPathResource
*/
@Override
public ClassLoader getClassLoader() {
// , -> ClassUtils ->
return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader());
}
Class Utils.get Default Class Loader()org.springframe ework.util.lassUtils.get Default Class Loader()は中国語の注釈を見ます.
/**
* Return the default ClassLoader to use: typically the thread context
* ClassLoader, if available; the ClassLoader that loaded the ClassUtils
* class will be used as fallback.
* Call this method if you intend to use the thread context ClassLoader
* in a scenario where you clearly prefer a non-null ClassLoader reference:
* for example, for class path resource loading (but not necessarily for
* {@code Class.forName}, which accepts a {@code null} ClassLoader
* reference as well).
* @return the default ClassLoader (only {@code null} if even the system
* ClassLoader isn't accessible)
* @see Thread#getContextClassLoader()
* @see ClassLoader#getSystemClassLoader()
*/
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
//
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back...
}
// null
if (cl == null) {
// No thread context class loader -> use class loader of this class.
//
cl = ClassUtils.class.getClassLoader();
if (cl == null) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
//getClassLoader() null bootstrap ClassLoader , C++ , Java null
try {
//
cl = ClassLoader.getSystemClassLoader();
}
catch (Throwable ex) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
}
return cl;
}
両親の委任モデルを破壊するTomcat類のキャリアはなぜ両親の委任モデルに背いていますか?