struts里總用到Thread.currentThread().getContextClassLoader()
而我們一般只用到Class.forName()或Class.getClassLoader()
所以想知道二者有什么區別,以下是從網上找到的:
Classloader存在下面問題:
在一個JVM中可能存在多個ClassLoader,每個ClassLoader擁有自己的NameSpace。
一個ClassLoader只能擁有一個class對象類型的實例,但是不同的ClassLoader可能
擁有相同的class對象實例,這時可能產生致命的問題。如ClassLoaderA,裝載了
類A的類型實例A1,而ClassLoaderB,也裝載了類A的對象實例A2。邏輯上講A1=
A2,但是由于A1和A2來自于不同的ClassLoader,它們實際上是完全不同的,
如果A中定義了一個靜態變量c,則c在不同的ClassLoader中的值是不同的。
Class.getClassLoader() returns the ClassLoader that loaded the class it
is invoked on.
Thread.getContextClassLoader() returns the ClassLoader set as the
context ClassLoader for the Thread it is invoked on, which can be
different from the ClassLoader that loaded the Thread class itself if
the Thread's setContextClassLoader(ClassLoader) method has been invoked.
This can be used to allow the object starting a thread to specify a
ClassLoader that objects running in the thread should use, but the
cooperation of some of those objects is required for that to work.