The first simple introduction of the Java ClassLoader, online data A lot, just say a little key.
The ClassLoader in Java can be broadly divided into two categories, one for the system, the other for Java application developers. There are three main types of ClassLoader available in the system:
Boot class loader (bootstrap class loader): It is used to load Java's core library, which is implemented with native code and does not inherit from Java.lang.ClassLoader.
Extension class loader (Extensions class loader): It is used to load the Java extension library. The implementation of the Java virtual machine provides an extension library directory. The ClassLoader finds and loads the Java class in this directory.
System class Loader: It loads Java classes according to the Classpath (CLASSPATH) of the Java application. In general, Java-applied classes are loaded by it. It can be obtained by Classloader.getsystemclassloader ().
In addition to the system-provided classloader, developers can implement their own classloader by inheriting the Java.lang.ClassLoader class to meet some special requirements.
All Class loaders have a parent class loader, in addition to the boot class loader. Class loading in the delegate mode, first layer to the parent class load, the parent load is not successful and then one layer to the child loading.
Point 1: Why this delegation is used for security purposes, such as the user has customized a java.lang.String, then if not to the boot loader to load, there will be more than one String of class instances in memory. and a limited access to the content of the package, hackers can also be obtained in this way (point 2 and then continue to explain). In this way, the boot class loader will only load a class, see the user-defined string comes, to see if they have loaded, the result is the system started to load the Java.lang.String class, it will not be loaded.
Point 2: Judging whether a class is equal not only depends on whether the class is named, but also whether the same class initializes the loader. So if the hacker wants to do a java.lang.Hack class to load, starting with the delegate mode, the boot class loader fails to load this class, it can only be given to the user-defined class loading. So the class and system of the Lang package is not in an initialization loader, even if the package name, still can not access the contents of the package is visible.
------------------------------------------------------
Further clarification
One, there are two terms, one is called "Define class loader", one is called "Initial class loader".
For example, there are the following class loader structures:
Bootstrap
Extclassloader
Appclassloader
-Custom CLSLOADR1
-Custom CLSLOADR2
If the Java.lang.String class is loaded with "Custom Clsloadr1", then the bootstrap class is called the "define class loader" for the class, based on the parent-delegated final bootstrap. All class loaders, including Bootstrap, that get the class instance are called "Initial class loaders."
Two, called "namespaces", refers to a "table" maintained by the JVM for each classloader, which records the list of classes loaded with such loaders as the "initial ClassLoader" (instead of defining a class loader, so that a class can exist in many namespaces), so the problem in the topic can be explained:
The cltest is Appclassloader loaded, and the string is loaded by loading the Cltest ClassLoader, which is appclassloader, but eventually delegated to bootstrap loaded (of course, the string class has already been loaded, Here is just an example). So, for the string class, Bootstrap is the "define class loader", and Appclassloader is the "initial ClassLoader." According to what has just been said, the string class is in the Appclassloader namespace (and also in the Bootstrap,extclassloader namespace, because Bootstrap, Extclassloader is also the initial class loader for string, so cltest can access the string class casually. This explains the phrase "classes in different namespaces are not directly accessible to each other".
Third, a class, loaded by a different class loader instance, produces two different classes in the method area that are not visible to each other and generate different class instances in the heap.
Four, the classes under CLASSPATH and ext loaded by different ClassLoader instances (such as custom clsloadr1,-custom CLSLOADR2), That is, the class that our custom class loader delegates to Appclassloader and extclassloader load, is it the same class in memory?
All class loaders that inherit ClassLoader and do not override the Getsystemclassloader method, The Appclassloader obtained by the Getsystemclassloader method are all the same Appclassloader instances, similar to the singleton pattern.
In the ClassLoader class, the Getsystemclassloader method calls the private Initsystemclassloader method to obtain the Appclassloader instance, in Initsystemclassloader:
Sun.misc.Launcher L = sun.misc.Launcher.getLauncher ();
。。。
SCL = L.getclassloader ();
Appclassloader is the inner class of the Sun.misc.Launcher class, and the Launcher class generates the Appclassloader instance in new itself and puts it in its own private variable loader:
Loader = Appclassloader.getappclassloader (Extclassloader);
It is worth mentioning that the Sun.misc.Launcher class uses a method similar to the singleton pattern, that is, the interface Getlauncher () which provides a singleton mode, and the constructor is set to public. However, in ClassLoader, launcher instances are obtained through a single-piece pattern, so the appclassloader we write for each classloader is the same Appclassloader class instance.
The conclusion is that all classes under CLASSPATH and ext loaded by the class loader with normal Parental delegation mode are the same class in the method area, and the class instances in the heap are the same.
----------------------------------------
Contextclassloader
Each thread holds a contextclassloader that can be obtained or defined using the Get,set method. If not specified, it is the class loader that initiates the thread. If it is not the main thread, the new thread is the class loader of the parent thread.
Why should have such a thing, looked up some information to say is, because in order to secure ClassLoader The entrust mechanism cannot satisfy some specific needs, this time must use this way the backdoor. such as Jdbc,jndi,tomcat, etc.:
Java provides a number of service provider interfaces (services Provider INTERFACE,SPI) that allow third parties to provide implementations for these interfaces. The common SPI is JDBC, JCE, JNDI, JAXP, and JBI. These SPI interfaces are provided by the Java Core library, such as the JAXP SPI interface definition contained in the Javax.xml.parsers package. The implementation code for these SPI is likely to be contained in a jar package that is dependent on the Java application and can be found through the classpath (CLASSPATH). The problem is that the SPI interface is part of the Java Core library and is loaded by the boot class loader, and the Java class implemented by the SPI is typically loaded by the System class loader. The boot ClassLoader is unable to find the SPI implementation class because it only loads the Java core library. It also cannot delegate to the system ClassLoader because it is the ancestor class loader of the system ClassLoader. In other words, the class loader's proxy mode does not solve this problem.
The thread context ClassLoader just solves this problem. Using the thread context class loader in the code of the SPI interface, you can successfully load the class to the SPI implementation. The thread context ClassLoader is used in many implementations of the SPI.
Http://www.cnblogs.com/onlywujun/p/3528160.html
A little understanding of the use of ClassLoader in Java (GO)