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 Springboot starts the execution of specific code

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

Share

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

This article is about how Springboot initiates the execution of specific code. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Implement the InitializingBean interface or use @ PostConstruct annotations

Implement InitializingBean as follows

Public class AnotherExampleBean implements InitializingBean {@ Override public void afterPropertiesSet () {/ / do some initialization}}

The official explanation is this: implementing this interface allows all necessary properties of the bean to be injected by the container (dependency injection) before executing the methods in afterPropertiesSet ().

The author uses a simple example to demonstrate (note: using @ PostConstruct and implementing the interface are equivalent, you can choose one of the two)

We use the @ PostConstruct annotation on the init method, and the Chicken class is used in the method, and this Chicken class is set through dependency injection, so it confirms the official statement that the @ PostConstruct annotated method will not be called until the dependency injection is complete. So why not put the Chicken class inside the List in the constructor, because the Chicken class has not been injected when the container calls the constructor method, so write it in the @ PostConstruct annotated method.

/ / first declare an entity class @ Data public class Chicken {private String name;} / / inject it into the container @ Configurationpublic class UserConfig {@ Bean public Chicken putUser () {Chicken chinken = new Chicken (); chinken.setName ("ordinary chicken nuggets"); return chinken;}} / / call injection chinken@Componentpublic class Family {@ Resource Chicken chicken; public static List names in the family class PostConstruct public void init () {names.add (chicken.getName ());} public Family () {names = new LinkedList ();}}

Implement the ApplicationListener interface

If the bean in a container implements the ApplicationListener interface, then at any time if an ApplicationEvent (event) is published in the ApplicationContext (container), the bean will be notified so that the appropriate policy can be enforced.

Here are several common ApplicationEvent events provided by Spring

The event name explains that ContextRefreshedEvent publishes this event when the container ApplicationContext container is initializing or refreshed. The initialization here means that all bean is loaded and bean with post processing is detected and activated. ContextStartedEvent publishes this event when the container starts calling the start () method. The start here is that all lifecycle bean receives a start signal ContextStoppedEvent will issue this event when the container calls the stop method

To take a simple example, in the following code I implement the ApplicationListener interface and listen for ContextRefreshedEvent events, so when springboot is started and initialized, I can execute the following method.

@ Component@Slf4jpublic class MenuManage implements ApplicationListener {@ Override public void onApplicationEvent (ContextRefreshedEvent event) {/ / do something}} implement the CommandLineRunner or ApplicationRunner interface

The bean that implements CommandLineRunner is detected by springboot and the run method is executed after the project is started. If there are multiple bean that implement the CommandLineRunner interface, you can use order annotations to specify the execution order.

@ Order (2) @ Componentpublic class ServerStartedReport implements CommandLineRunner {@ Override public void run (String...) Args) throws Exception {/ / do something}}

The only difference between the implementation of the ApplicationRunner interface and the implementation of CommandLineRunner is that the parameters received by the latter are the original parameters passed into the main method, while the parameters received by ApplicationRunner are encapsulated with the original parameters, and the specified parameters can be obtained through the parameter name name.

@ Componentpublic class MyApplicationRunner implements ApplicationRunner {@ Override public void run (ApplicationArguments args) throws Exception {System.out.println ("ApplicationRunner:" + Arrays.asList (args.getSourceArgs (); System.out.println ("getOptionNames:" + args.getOptionNames ()); System.out.println ("getOptionValues:" + args.getOptionValues ("foo")); System.out.println ("getOptionValues:" + args.getOptionValues ("log");}} Thank you for reading! This is the end of this article on "how Springboot starts to execute specific code". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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