Java basics-understanding of ClassLoader

Source: Internet
Author: User

Java basics-understanding of ClassLoader
By default, the three class loaders in Java have three ClassLoader by default, which are in the order of hierarchy: Bootstarp ClassLoaderExt ClassLoaderSystem ClassLoaderBootstrap ClassLoader is the top-level ClassLoader, which is special, it is written and integrated in JVM using C ++, which is used to load some core classes at JVM startup, such as rt. jar, resources. jar, charsets. jar, jce. jar and so on, you can run the following code to see what are available: URL [] urls = sun. misc. launcher. getBootstrapClassPath (). getURLs (); for (int I = 0; I <urls. length; I ++) {System. out. println (urls [I]. toExternalForm ();} the remaining two Cl AssLoader is inherited from the ClassLoader class. Java class loading adopts a method called "parent-parent delegation" (explained later). Therefore, all ClassLoader except Bootstarp ClassLoader has a "parent" class loader, it is not integrated, but an inclusive relationship. // ClassLoader. javapublic abstract class ClassLoader {... // The parent class loader for delegation private ClassLoader parent ;... the so-called "parent-parent delegation" means that when a class is loaded, it is first delegated to the parent class loader for loading, when the parent class loader cannot be loaded, it tries to load it by itself. Therefore, the entire class is loaded from top to bottom. If none of them are loaded, the ClassNotFoundException exception is thrown. As mentioned above, Bootstarp ClassLoader is the top-level class loader. In fact, Ext ClassLoader and System ClassLoader are loaded by it at the beginning. ClassLoader is called an extension class loader and is responsible for loading Java extension class libraries. By default, all the jar files under JAVA_HOME/jre/lib/ext/directory are loaded (including the jar packages manually put in ). System ClassLoader is called the System class loader. It is responsible for loading all the jar and class files under the application classpath directory, including the jar packages under the cp parameter specified by the jar package. Run the following code to verify the above content: ClassLoader loader = Debug. class. getClassLoader (); while (loader! = Null) {System. out. println (loader); loader = loader. getParent ();} System. out. println (loader); the role of "parent-parent delegation" is mainly to ensure security and avoid the Dynamic Replacement of some Java core classes such as String, at the same time, duplicate loading is also avoided, because JVM distinguishes different classes, not only according to the class name, the same class file is loaded by different ClassLoader is different two classes, if they are mutually transformed, java will be thrown. lang. classCaseException. in addition to the three default class loaders mentioned above, the user can inherit the ClassLoader class to create a custom class loader, the reason why we need a custom classloader is that sometimes we need to create classes, such as networks, in some special ways. As for how the custom Class Loader works, the loadClass method of the ClassLoader Class has defined the algorithm: protected synchronized 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) ;}}if (resolve) {resolveClass (c) ;}return c ;}> 1. invoke fin DLoadedClass (String) to check if the class has already been loaded.> 2. invoke the loadClass method on the parent class loader. if the parent is null the class loader built-in to the virtual machine is used, instead.> 3. invoke the findClass (String) method to find the class. you can see from the above Javadoc that the custom Class Loader only needs to overload findClass. Context ClassLoader first refers to the four ClassLoader mentioned above in Java, Bootstrap ClassLoader, Ext ClassLoader, System ClassLoader and user-defined. Therefore, Context ClassLoader is not a new class loader, it must be one of these four types. First, we need to add the following to the class loading: if Class A is loaded by A loader, B referenced in Class A is also loaded by this loader (if B is not loaded). Generally, Class B must be in the classpath of Class. However, considering that different objects in A multi-threaded environment may be loaded by different ClassLoader, when object A loaded by ClassLoaderC is transferred from one thread to another ThreadB, threadB is loaded by ClassLoaderD. At this time, if A wants to obtain resources other than its own classpath, it can use Thread. currentThread (). getContextClassLoader () is used to obtain the ClassLoader of the Thread context, which is generally ClassLoaderD. You can use the Thread. currentThread (). setContextClassLoader (ClassLoader) to display the settings. Why Contex ClassLoader has the Context ClassLoader because the "parent-parent delegation" mechanism of Java has limitations: for example,> JNDI, the JNDI class is initiated by bootstarp ClassLoader from rt. jar is loaded in the middle, but the core driver of JNDI is provided by the formal implementation, and is usually under the-cp parameter (Note: that is, the default System ClassLoader management ), this requires bootstartp ClassLoader to load classes only visible to SystemClassLoader, and the normal logic cannot be processed. What should we do? Parent can get the> Context ClassLoder of the calling Thread by obtaining the method of the currently called Thread to load the class. The example of loading resources I mentioned above. Contex ClassLoader provides a backdoor that breaks through this mechanism. Context ClassLoader is usually used in some framework code. You can use ClassLoader of the class when writing code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.