Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is parental appointment?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "what is parental appointment". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Parental appointment mechanism

The    parent delegation mechanism means that when a class loader receives a class load request, the class loader first delegates the request to the parent class loader. This is true of every class loader (recursively looking for it), and the subclass loader will try to load it itself only if the parent class loader cannot find the specified class within its own search scope.

Obviously, when introducing the parent appointment mechanism, we have to mention ClassLoader. Before we talk about ClassLoader, we need to know the basics of Java.

Java runs in Java's virtual machine (JVM), but how does it run in JVM? The Java source code we wrote in IDE is compiled into a .class bytecode file by the compiler. Then our ClassLoader is responsible for loading these class files into JVM for execution.

Three layers of ClassLoader are provided in JVM:

Bootstrap ClassLoader (boot class loader): mainly responsible for loading core class libraries (java.lang.*, etc.), constructing Extension ClassLoader and Application ClassLoader.

Extension ClassLoader (extension class loader): jar that is mainly responsible for loading some extensions in the jre/lib/ext directory.

Application ClassLoader (application class loader): the main function class that is mainly responsible for loading the application.

The last CustomClassLoader (user-defined classloader) is written by java, a user-defined classloader that loads class files with specified paths.

So how does a Hello.class file get loaded into JVM?

Let's take a brief look at the source code.

Protected Class loadClass (String name, boolean resolve) throws ClassNotFoundException {synchronized (getClassLoadingLock (name)) {/ / first check whether the classsh has been loaded with Class c = findLoadedClass (name); if (c==null) {long t0 = System.nanoTime (); try {/ / c==null means it is not loaded. If there is a parent loader, let the parent loader load if (parent! = null) {c = parent.loadClass (name, false) } else {/ / if the loader of the parent class is empty, it means recursive to bootStrapClassloader / / bootStrapClassloader is special and cannot get c = findBootstrapClassOrNull (name) via get;}} catch (ClassNotFoundException e) {} if (c = = null) {/ / if bootstrapClassLoader is still not loaded, recursively try to load classlong T1 = System.nanoTime (); c = findClass (name); sun.misc.PerfCounter.getParentDelegationTime (). AddTime (T1-T0) Sun.misc.PerfCounter.getFindClassTime (). AddElapsedTimeFrom (T1); sun.misc.PerfCounter.getFindClasses (). Increment ();}} if (resolve) {resolveClass (c);} return c;}}

Regardless of our custom classloader, we will first check in AppClassLoader to see if it has been loaded, and if so, there is no need to load it again. If not, the parent loader is taken and the loadClass method of the parent loader is called. The same person in the parent class first checks to see if it has been loaded, if not further up. Note that no loader chooses to load until it reaches Bootstrap ClassLoader. If the parent loader fails to load, it sinks to the child loader to load, down to the lowest level (that is, the recursive lookup process), and if no loader can load, a ClassNotFoundException is thrown.

As a result, we can well summarize the workflow of the parent appointment mechanism:

1. When Application ClassLoader receives a class load request, he first does not try to load the class himself, but delegates the request to the parent class loader Extension ClassLoader to complete it.

2. When Extension ClassLoader receives a class load request, he will not try to load the class himself at first, but delegate the request to the parent class loader Bootstrap ClassLoader to complete it.

3. If the Bootstrap ClassLoader load fails (the required class is not found in\ lib), Extension ClassLoader will try to load.

4. If Extension ClassLoader also fails to load, it will be loaded using Application ClassLoader.

5. If Application ClassLoader also fails to load, Custom ClassLoader (user-defined loader) will be used to attempt to load.

6. If both failed to load, a ClassNotFoundException exception will be thrown.

The role of parental appointment mechanism

1. Prevent repeated loading of the same .class. Ask the above through the delegate, once loaded, you don't have to load it again. Keep the data secure.

2. Make sure that the core .class cannot be tampered with. Through delegation, the core .class will not be tampered with, even if it is tampered with, it will not be loaded, and even if it is loaded, it will not be the same .class object. Different loaders load the same .class and are not the same Class object. This ensures the security of Class execution.

Take Chestnut: if someone wants to replace the system-level class: String.java. Tamper with its implementation, but under this mechanism, the classes of these systems have already been loaded by Bootstrap ClassLoader, so they will not be loaded again, preventing the implantation of dangerous code to a certain extent.

This is the end of what is the assignment of parents. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 225

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report