类加载(二)

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 ClassLoader context 

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 loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader,

the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String). Before delegation, an absolute resource name is

constructed from the given resource name using this algorithm:

  • If the name begins with a '/' ('u002f'), then the absolute name of the resource is the portion of the name following the '/'.
  • Otherwise, the absolute name is of the following form:
    modified_package_name/name

    Where the modified_package_name is the package name of this object with '/' substituted for '.' ('u002e').

 

原文地址:https://www.cnblogs.com/yuyutianxia/p/3487219.html