Spring automatically loads data in several ways and executes the sequence

  • During the startup process of Spring application, it is necessary to automatically scan the classes annotated with @Component, load the classes and initialize the objects for automatic injection.
  • When loading a class, the code in the static code block must first be executed.
  • The constructor method will be executed later when the object is initialized.
  • After the object injection is completed, call the method annotated with @PostConstruct.
  • When the container is started successfully, the run methods in the CommandLineRunner and ApplicationRunner interface classes are called according to the order of the @Order annotations.
  • Therefore, the loading order is static>constructer>@PostConstruct>CommandLineRunner and ApplicationRunner.

 

Method 1: @PostConstruct

This method is probably the most commonly used

You can use Spring Boot's @PostConstruct annotation to implement a function that is executed once at startup. The method marked with the @PostConstruct annotation will be automatically called after the Bean initialization is completed. You can perform operations in this method that only need to be performed once at startup.

If you want to complete some initialization operations when generating an object, but these initialization operations rely on dependency injection, then you cannot implement it in the constructor. To this end, you can use @PostConstruct to annotate a method to complete initialization. The @PostConstruct annotated method will be automatically called after dependency injection is completed. Website address: yii666.com Article source address: https://www.yii666.com/blog/460754.html

Constructor >> @Autowired >> @PostConstruct Article source address https://www.yii666.com/blog/460754.html

For example, add a @PostConstruct annotation-marked method to the startup class of a Spring Boot application:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;

@SpringBootApplication
public class MyApplication {

    @PostConstruct
    public void init() {
        // 在这里执行仅需在启动时执行一次的操作
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

In the above example code, the init() method is marked with the @PostConstruct annotation, indicating that it will be automatically called after the MyApplication Bean is initialized. In the init() method, you can perform operations that only need to be performed once at startup, such as initializing some data, establishing a database connection, etc.

Method 2: Use the CommandLineRunner interface or ApplicationRunner interface provided by Spring Boot

This method has been used successfully in projects.

In addition to the @PostConstruct annotation, you can also use the CommandLineRunner interface or ApplicationRunner interface provided by Spring Boot to implement the function of executing once at startup.

Both interfaces have a run() method, which is automatically called after the application starts. This method needs to implement operations that need to be performed at startup, such as initializing data, starting scheduled tasks, etc.

If you need multiple operations to be executed at startup, you can define multiple beans that implement the CommandLineRunner or ApplicationRunner interface, and specify their execution order through the @Order annotation.

The sample code is as follows:

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(1) // 可以通过@Order注解指定执行顺序,数字越小越先执行
public class MyCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // 在这里执行启动时需要执行的操作
    }
}

@Component
@Order(2)
public class MyApplicationRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 在这里执行启动时需要执行的操作
    }
}

The above example code defines two beans, namely MyCommandLineRunner that implements the CommandLineRunner interface and MyApplicationRunner that implements the ApplicationRunner interface. Their run() method will be automatically called after the application starts, and operations that need to be performed at startup can be implemented here. Among them, the @Order annotation is used to specify their execution order. The smaller the number, the earlier they are executed.

Method 3: Use the ApplicationListener interface provided by Spring Boot

This method has not yet been implemented

You can also use the ApplicationListener interface provided by Spring Boot to implement the function that is executed once when the application starts. This interface defines methods for listening to Spring Boot application events. When the application triggers the corresponding event, the listener will automatically call the corresponding method for processing.

The specific implementation steps are as follows: Article address https://www.yii666.com/blog/460754.html

Create a class that implements the ApplicationListener interface, such as MyApplicationListener.

Implement the onApplicationEvent() method, and write in this method the operations that need to be performed at startup, such as initializing data, establishing a database connection, etc. Website:yii666.com<

Register MyApplicationListener as a Spring Bean through the @Component annotation or @Bean annotation.

The sample code is as follows:

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 在这里执行启动时需要执行的操作
    }
}

The above sample code creates a Bean named MyApplicationListener and implements the ApplicationListener interface for listening to the ApplicationReadyEvent event. Write the operations that need to be performed at startup in the onApplicationEvent() method. Finally, register MyApplicationListener as a Spring Bean through the @Component annotation.

When the application is started, MyApplicationListener will automatically listen to the ApplicationReadyEvent event and execute the code in it. You can implement operations here that need to be executed once at startup, ensuring that they are only executed once when the application starts.

Guess you like

Origin blog.csdn.net/zlfjavahome/article/details/132889707