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

Why don't Spring IOC containers use Class.forName to load classes

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Why don't Spring IOC containers use Class.forName add-on classes? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Why the Spring IOC container we're going to learn today uses ClassLoader instead of Class.forName to load classes!

Although both Class.forName () and ClassLoader can be used to load classes, there are some differences. To illustrate them, let's take a look at the steps that JVM takes to load classes!

After knowing the above, to figure out why Classloader is used in Spring Ioc instead of Class.forName, you have to start with the difference between them. Let's first see what happens if you use Class.forName!

Create a test class first.

In the above code, the call to Class.forName ("com.xttblog.Xttblog") triggers the execution of the static code block of Xttblog, while ClassLoader.getSystemClassLoader (). LoadClass ("com.xttblog.Xttblog") does not.

Calling Class.forName is actually equivalent to calling Class.forName (className, true, currentLoader), and the second parameter of this method indicates whether the class needs to be initialized. The source code is set to true, so when Class.forName gets the Class object, it initializes the class automatically. And the ClassLoader of the Class.forName loading class is the same as the ClassLoader of the class in which the Class.forName is called. In the above code, the call to Class.forName ("com.xttblog.Xttblog") triggers the execution of the static code block of Xttblog, while ClassLoader.getSystemClassLoader (). LoadClass ("com.xttblog.Xttblog") does not.

We know that there is a lazy load (lazy loading) in Spring's IOC, but if you use Class.forName, lazy loading is not possible. In order to speed up the initialization, Spring IoC uses a lot of delayed loading technology. Using classloader does not need to execute the initialization code in the class, so you can speed up the loading speed and leave the initialization of the class to the actual use of the class.

Therefore, the Spring IOC container does not use Class.forName to load classes.

After reading the above, do you understand why the Spring IOC container does not use the method of loading classes with Class.forName? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Servers

Wechat

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

12
Report