Java class loader has several? What is a parent to delegate mechanism?

A, JAVA class loader including several?

Start class loader bootstrap class loader

  Start class loader to load the JVM major class of its own needs, the class loader is implemented in C ++ language, is part of the virtual machine itself, which is responsible under the core libraries or -Xbootclasspath parameters under the / lib path specified path jar package is loaded into memory, attention: Because virtual machines are loaded by file name recognition jar package, such as rt.jar, if the file name is not recognized virtual machine, even if the jar package lib directory is not thrown into action (for security reasons, Bootstrap boot class loader loads only the package name for the class at the beginning of java, javax, sun, etc.).

Extension classloader extensions class loader

  It is responsible for loading the JAVA_HOME / lib / ext directory or system variable by the specified bit path -Djava.ext.dir libraries, developers can use the standard direct extension class loader.

Application class loader application class loader

  The application loader means sun.misc.Launcher $ AppClassLoader Sun company to achieve. It is responsible for loading the class library in the system, specify the path java -classpath or -D java.class.path, that is, the classpath, we often use developers can directly use the system class loader loads the class in general is a program default class loader to be acquired by the class loader ClassLoader # getSystemClassLoader () method.

Custom class loader java.lang.classloder

  Custom class loader needs to inherit java.lang.ClassLoader.

The relationship between the class loader

  Start class loader, implemented by C ++, no parent.    

  Expand the class loader (ExtClassLoader), implemented by the Java language, the parent class loader is null    

  System class loader (AppClassLoader), implemented by the Java language, the parent class loader is ExtClassLoader    

  Custom class loader, the parent class loader is certainly AppClassLoader.

Second, parents delegate mechanism

  Please note that parents appoint parent-child relationship model is not known as class inheritance.    

  Its working principle is: if a class loader loads the class received a request, it will not go to their own load, but to entrust the request to the parent class loader to perform, if the parent class loader there parent class loader, then further delegate up, followed by recursive requests will eventually reach the top of the boot class loader, if the parent class loader can complete class loading tasks on successful return, if the parent class loader can not complete this add tasks, sub loader will try to load their own, this is the parents delegate model in which each son are lazy, every time you have left to live his father would do, until my father says I can not do it, my son his own way to complete.

Parents delegate mechanism of action / benefits

  By repeating this level off to avoid loading the class, when his father had loaded the class, there is no need child ClassLoader loaded once again. Followed by safety reasons, java api defined core types are not replaced arbitrarily assumed java.lang.Integer class named pass through a network, it is transmitted to the boot class loader parents by entrusting mode, the boot class loader the name of the class found in the core Java API, found that the class has been loaded, and will not reload the network passed over java.lang.Integer, return the loaded directly off Integer.class, so that we can prevent the core API library is tampering.

  to sum up:

  Benefits 1, can prevent repeated load class

  The health benefits can prevent Java core class libraries api been tampered with

Scenarios

  The Tomcat container, each with its own ClassLoader WebApp, based on the load path of each WebApp ClassPath the event of carrying Tomcat Jar package entrusted to CommonClassLoader loading. Package with isolation. In addition mature open source frameworks, has its own ClassLoader.

Guess you like

Origin www.cnblogs.com/baichunyu/p/11977874.html