In java reflection, class Difference between forname and classloader

preface

Recently, during the interview, I was asked about class in java reflection The difference between loading classes with forname() and using classloader. I didn't think of it at that time. Later, I studied it myself and wrote it down.

explain

In Java, class Both forname () and classloader can load classes. Classloader is a class loader that follows the parental delegation model and finally calls the class loader to start. Its function is to "obtain the binary byte stream describing this class through the fully qualified name of a class". After obtaining the binary stream, it is put into the JVM. Class. The forname () method is actually implemented by calling the classloader.

Class. forName(String className); The source code of this method is

The last method is called forName0. The second parameters in this forName0 method are set to true by default. This parameter represents whether to initialize the loaded class, initialize the class when it is set to true, represent the static block of code in the execution class, and assign the static variable.

You can also call class Forname (string name, Boolean initialize, classloader) method to manually select whether to initialize the class when loading the class. Class. The source code of forname (string name, classloader) is as follows:

Only part of the comments in the source code are extracted. The description of the parameter initialize is: if {@ code true} the class will be initialized This means that if the parameter is true, the loaded class will be initialized.

give an example

Here are some examples to illustrate the results:

A class containing static code blocks, static variables, and static methods assigned to static variables

Use class Test method for forname():

Operation results:

Test method using classloader:

Operation results:

According to the operation results, class Forname initializes the class when loading the class, while the loadclass of classloader does not initialize the class, but loads the class into the virtual machine.

Application scenario

The implementation of IOC in the familiar spring framework is the use of classloader.

When we use JDBC, we usually use class The forname () method to load the database connection driver. This is because the JDBC specification explicitly requires that the driver (database driver) class must register itself with the drivermanager.

Take MySQL driver as an example to explain:

We can see that the operation registered by the driver to the drivermanager is written in the static code block, which is why class is used when writing JDBC The reason for forname().

Well, I'll write about it today. Recently, I met a lot of problems and learned a lot in the interview. Although I'm very tired, it also makes people grow up a lot. After all, the interview is a peeling process, and I will encounter various problems and scenes of various enterprises and interviewers. Cheer yourself on and find a company that can let you work for at least a few years. Don't always let me fall down after a job. Or I'm helpless.

After finding a job, I will summarize my experience.

After two years, when I was asked how SPI destroyed the parental delegation model in the second interview, I suddenly realized that when I wrote this blog before, it was because the interviewer asked a question on the title of this blog. Now I can vaguely feel that the former interviewer should want to ask class The way forname() loads the database driver actually destroys the use of the parent delegation model. A third-party class is loaded through the application classloader instead of the parent bootstrap classloader.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>