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

How to parse ClassLoader

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to parse ClassLoader. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Overview of the java.lang.ClassLoader class:

The function of ClassLoader class is to find the corresponding Class bytecode file according to the fully qualified name of a specified class, and then load it into an instance of a java.lang.Class class.

Division of class loaders:

Most java programs use the class loaders provided by the system in the following 3:

Start the classloader (Bootstrap ClassLoader):

This class loader is responsible for loading the class libraries under the\ lib directory into the virtual machine memory and is used to load the core library of java. This kind of loader does not inherit from java.lang.ClassLoader and cannot be directly called by java programs. The code is written in C++. It is part of the virtual machine itself.

Extended class loader (Extendsion ClassLoader):

This class loader is responsible for loading the class library in the\ lib\ ext directory, which is used to load the java extension library. Developers can use this class loader directly.

Application class loader (Application ClassLoader):

This class loader is responsible for loading the class library under the user classpath (CLASSPATH). Generally speaking, the java classes we write are loaded by this class loader. This class loader is the return value of the getSystemClassLoader () method in CLassLoader, so it is also called system class loader. In general, this is the default class loader for the system.

In addition, we can also add our own defined class loader to meet special requirements, which need to inherit the java.lang.ClassLoader class.

Use the code to look at the classloader:

Package com.wang.test;publicclass TestClassLoader {

Publicstaticvoid main (String [] args) {

ClassLoader loader = TestClassLoader.class.getClassLoader ()

System.out.println (loader.toString ())

System.out.println (loader.getParent () .toString ())

System.out.println (loader.getParent () .getParent ())

}

}

Observe the print results:

Sun.misc.Launcher$AppClassLoader@500c05c2

Sun.misc.Launcher$ExtClassLoader@454e2c9c

Null

The first line prints is the application class loader (the default loader), the second line prints its parent class loader, extension class loader, according to our idea the third line should print the start class loader, here the return null, the reason is getParent (), when returning null, it defaults to use the startup class loader as the parent loader.

Parent delegation model for class loaders:

The parent delegation model is a specification for organizing the relationship between class loaders. Its working principle is that if a class loader receives a request for class loading, it will not attempt to load the class itself. Instead, it delegates the request to the parent class loader to complete, so that all load requests are eventually passed to the top-level startup class loader. Only when the parent class loader cannot complete the load request (the desired class is not found in its search scope) will it be handed over to the subclass loader to attempt to load.

The advantage is that the java class has a hierarchical relationship with priority along with its class loader. This is very necessary, such as java.langObject, which is stored in\ jre\ lib\ rt.jar, which is the parent class of all java classes, so no matter which class is loaded, all load requests are summarized into the top-level startup class loader, so the Object class is loaded by the startup class loader, so the same class is loaded, if the parent delegation model is not used If each class loader loads itself, there will be more than one Object class in the system and the application will be messed up.

Class.forname () and ClassLoader.loadClass ():

Class.forname (): is a static method, the most commonly used is Class.forname (String className); returns a Class object based on the fully qualified name of the class passed in. This method performs class initialization while loading the Class file into memory.

For example, Class.forName ("com.wang.HelloWorld")

ClassLoader.loadClass (): this is an instance method that requires a ClassLoader object to call it. When the method loads the Class file into memory, it does not initialize the class until it is used for the first time. This method needs to get a ClassLoader object, so you can specify which classloader to use as needed.

For example, ClassLoader cl=.;cl.loadClass ("com.wang.HelloWorld")

The above is how to interpret ClassLoader. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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: 0

*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

Internet Technology

Wechat

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

12
Report