Classloader of JVM)

Source: Internet
Author: User

Java has a total of four class loaders. They are called class loaders. They are used to load memory when a program uses a class.
The four class loaders are:Bootstrap ClassLoader,Extension ClassLoader,AppClassLoader
AndURLClassLoaderIn fact, their role can be inferred from the name. AppClassLoader is calledSystem ClassLoader

Bootstrap ClassLoaderThe core class of java is loaded when the JVM starts to run. It is written in C ++ and used to load the core class library. In the JVM source code, it writes:
Static const char classpathFormat [] =
"%/Lib/rt. jar :"
"%/Lib/i18n. jar :"
"%/Lib/sunrsasign. jar :"
"%/Lib/jsse. jar :"
"%/Lib/jce. jar :"
"%/Lib/charsets. jar :"
"%/Classes ";
Extension ClassLoaderIs used to load extension classes, that is/Lib/ext.
AppClassLoaderUsed to loadClasspathIs the class most closely related to us.
URLClassLoaderThis class is used to load remote classes on the network. It is not discussed for the moment.

Relationship between them:

1.Parent-ChildIn ascending order. It is not a simple inheritance relationship.

2. ClassLoader hasGetParentBut what Ext ClassLoader gets after calling isNull, Bootstrap is the JVM's own, which is invisible to users.

3. classloader'sDelegation Mechanism: When a lower-level ClassLoader needs to load a class, it first requests the Parent loader to load the class, and the Parent then requests its Parent
For example, if Ext is to be loaded, it sends a request to IOT platform. If the largest Bootstrap cannot be found, the Boot will ask Ext to search for it and Ext to find it.NoLet the App at the next level find the ClassNotFoundException.

4. Class A calls Class B, and B will require the class loader of its class to load it, that is, B will require the loader of Class A to load B. This will cause a problem. If they are together, it doesn't matter. It is sure that a classloader will load both of them. But what if A is in the/lib/ext folder and B is in Classpath? The process is to load A first, so the Bootstrap Classloader and the boot fail to find ext, so ext finds it and there is no problem. Load B because A calls B, so we also found it from bootstrap. We didn't find it, and then we still couldn't find the ext classloader of A, but we won't call it any more, so we reported ClassNotFoundException.
But there are many applications in real life. For example, the JDBC core method must be in the core library while the driver must be in the extension library. What should we do? To use Context ClassLoader, we can specify a proper classloader for this Thread through the setContextClassLoader method when setting a Thread. When this Thread is running, we can use the getContextClassLoader method to obtain the context classloader and use it to load the Class we need. The default value is system classloader. With this feature, we can "break" The classloader delegation mechanism. The parent classloader can obtain the context classloader of the current thread, and the context classloader can be its sub-classloader or other classloader, then, the parent classloader can obtain the required Class from it, which breaks the restrictions that can only be requested from the parent classloader. This mechanism can meet the requirements of system classloader (that is, in jvm classpath) when our classpath is determined at runtime and loaded by the custom classloader) you can use the context classloader to obtain the custom classloader and load it into a specific class (usually abstract classes and interfaces, which are implemented in the Custom classloader ), for example, servlets in web applications are loaded using this mechanism.

 

Http://hi.baidu.com/qmiao128/item/c983bffca5ca6fec1b111f26

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.