类加载(二)
1.Class 的getClassLoader()
Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader.
This method will return null in such implementations if this class was loaded by the bootstrap class loader.
2.Thread 的getContextClassLoader()
Returns the context ClassLoader for this Thread. The context ClassLoader is provided by the creator of the thread for
use by code running in this thread when loading classes and resources. If not set, the default is the ClassLoadercontext
of the parent Thread. The context ClassLoader of the primordial thread is typically set to the class loader used to load
the application.
For example,JNDI and JAXP used thread's ClassLoader.You had better to use thread's ClassLoader in your own code when your
code need to deployed on J2EE container.
3.ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别
Class.getResourceAsStream()
Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining
class loaderof the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader,
the method delegates toClassLoader.getSystemResourceAsStream(java.lang.String)
.Before delegation, an absolute resource name is
constructed from the given resource name using this algorithm:
modified_package_name/name
Where themodified_package_nameis the package name of this object with'/'substituted for'.'('\u002e').