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 the initialization method of Bean in SpringBoot

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

Share

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

This article introduces the relevant knowledge of "what is the initialization method of Bean in SpringBoot". In the operation of actual cases, many people will encounter such a dilemma, so 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!

Notes and instructions

Use annotations: @ PostConstruct

Effect: the specified operation is performed after Bean initialization (after constructor and @ Autowired). It is often used to delay actions in constructors.

Note: Bean initialization execution order: constructor-> @ Autowired-> @ PostConstruct

Code sample Annotation sample @ Componentpublic class PostConstructTest1 {@ Autowired PostConstructTest2 postConstructTest2; public PostConstructTest1 () {/ / postConstructTest2.hello ();} @ PostConstruct public void init () {/ / some init function}}

In the initialization operation of Bean, you will sometimes encounter a null pointer error when calling other Bean. At this point, you can put the operation that calls the method of another Bean into the method annotated by @ PostConstruct, delaying its execution.

Error example @ Component public class PostConstructTest1 {@ Autowired PostConstructTest2 postConstructTest2; public PostConstructTest1 () {postConstructTest2.hello ()}} @ Component public class PostConstructTest2 {public void hello () {System.out.println ("hello, I am PostConstructTest2");}}

Correct example @ Componentpublic class PostConstructTest1 {@ Autowired PostConstructTest2 postConstructTest2; public PostConstructTest1 () {postConstructTest2.hello ();}} @ Componentpublic class PostConstructTest1 {@ Autowired PostConstructTest2 postConstructTest2; public PostConstructTest1 () {/ / postConstructTest2.hello ();} @ PostConstruct public void init () {postConstructTest2.hello ();}}

SpringBoot @ PostConstruct is good, but it should be used with caution.

If you have done SpringBoot development, you must be familiar with @ PostConstruct. In a Bean component, the method marked @ PostConstruct automatically executes the logic of the method after the Bean construction is complete.

1 the emergence of the problem

Let's first talk about the loading process of Bean in SpringBoot. To put it simply, SpringBoot will automatically initialize a global single instance of a class or interface marked with Bean-related annotations (such as @ Component, @ Service, @ Repository, etc.). If marked, the initialization order will be in the order of user tags, otherwise it will be initialized in the default order. During initialization, the @ PostConstruct method of the Bean, if any, is executed after the constructor of a Bean is executed, and then the next Bean is initialized.

So: if the logical processing time in the @ PostConstruct method is long, it will increase the time it takes for the SpringBoot application to initialize the Bean, which in turn increases the time it takes for the application to start. Because the SpringBoot application opens the port to provide services only after Bean initialization is completed, the application is not accessible until then.

2 case simulation

To simulate the situation mentioned above, build two component classes, ComponentOne and ComponentTwo, in the SpringBoot project. The time-consuming initialization logic is placed in ComponentOne, and the initialization order of ComponentOne is set before ComponentTwo. The complete code is as follows:

@ Component@Order (Ordered.HIGHEST_PRECEDENCE) public class ComponentOne {private Logger logger = LoggerFactory.getLogger (this.getClass ()); public ComponentOne () {this.logger.info ("ComponentOne initialization complete");} @ PostConstruct public void init () {this.logger.info ("ComponentOne simulation time-consuming logic start") Try {/ / here hibernate for 5 seconds to simulate time-consuming logic Thread.sleep (1000 * 5);} catch (InterruptedException e) {logger.info ("analog logic time-consuming failure", e);} this.logger.info ("ComponentOne simulation time-consuming logic completion") } @ Component@Order (Ordered.HIGHEST_PRECEDENCE + 1) public class ComponentTwo {private Logger logger = LoggerFactory.getLogger (this.getClass ()); public ComponentTwo () {this.logger.info ("ComponentTwo initialization completed");} @ PostConstruct public void init () {this.logger.info ("post-ComponentTwo initialization processing");}}

Start the application, and initialize part of the log as follows:

This is the end of the content of "what is the initialization method of Bean in SpringBoot". Thank you for 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: 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