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 are the load parameters that implement the initialization method when the project starts in the

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Many beginners are not very clear about what are the loading parameters of the initialization method at the start of the project. in order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Springboot project startup, initialization method load parameters

Today, I saw the @ PostConstruct annotation used in the project. I didn't see it before, so I checked it out.

1.@PostConstruct description

The method modified by @ PostConstruct runs when the server loads Servlet and is called only once by the server, similar to the inti () method of Serclet. Methods modified by @ PostConstruct run after the constructor and before the init () method.

2.@PreDestroy description

The method modified by @ PreDestroy runs when the server uninstalls Servlet and is called only once by the server, similar to the destroy () method of Servlet. The method modified by @ PreDestroy runs after the destroy () method, before the Servlet is completely uninstalled.

You can see that this annotation is used to load the initialization parameters when the project starts.

Then I summarized several ways to initialize load parameters when a project starts in springboot.

First: note @ PostConstruct

After the project starts, you can see that this line of code has already been executed when the project starts.

Second: implement the CommandLineRunner interface

The parameters in the @ Order annotation are in the order in which the class executes, from small to large.

All the output statements in the class are printed out

The third kind: startup class of springboot

The easiest way is to add the class that needs to be initialized directly to the startup class of springboot.

Springboot initialization parameters order spring initialization parameters order is

Constructor-- > automatic injection-- > PostConstrut-- > InitializingBean-- > init method

-- > CommandLineRunner interface method

During the container initialization process, the initialization object is performed in the following order.

Initialization-Construction method!

Initialize-- @ PostConstruct method

Initialization-InitializingBean interface

Initialize-- init method!

-after the container is started.

Container startup-CommandLineRunner interface method!

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report