How to run some specific code when Spring Boot starts? ApplicationRunner & CommandLineRunner

How to run some specific code when Spring Boot starts?

You can implement the interface ApplicationRunner or CommandLineRunner. These two interfaces are implemented in the same way. They both provide only one run method.

1. ApplicationRunner

  • In the project, you may encounter such a problem: after the project is started, a piece of code is executed immediately.
    In SpringBoot, an interface is provided: ApplicationRunner. In this interface, there is only one run method, and the timing of its execution is: after the spring container is started, it will immediately execute the run method of the interface implementation class.
  • The class that implements the ApplicationRunner interface and rewrites the run method. After the springBoot project is started, the run method is called and executed once
@Component
@Order(1)
public class TestImplApplicationRunner implements ApplicationRunner {
    
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
    
    
    //do something
        System.out.println("测试ApplicationRunner接口1");

    }
}
  • @Component annotation
    This implementation class is to be injected into the spring container, and the @Component annotation is used here;
    in the same project, multiple ApplicationRunner implementation classes can be defined, and their execution order is annotated with @Order annotation or re-implemented the Ordered interface to fulfill.

  • Parameters of the run method: ApplicationArguments can get the command parameters executed by the current project. (For example, when the project is executed as a jar, the parameters can be obtained through ApplicationArguments); since this method is executed after the container is started, you can get other injected ones from the spring container here. beans.

  • @Order annotation
    If there are multiple implementation classes and you need them to be executed in a certain order, you can add the @Order annotation to the implementation class. @Order(value=integer value). SpringBoot will execute according to the value in @Order from small to large.
    @order, using annotations to control the loading order of beans, the @Order tag defines the loading order of components, the smaller the value, the higher the priority, and it can be negative. The smaller the value, the earlier it is loaded.

  • @Order(-1) takes precedence over @Order(0)
    @Order(1) takes precedence over @Order(2)

2. CommandLineRunner

background

Before the project starts, the data is preloaded. For example, permissions container, special user data, etc. Usually we can use listeners and events to operate. However, springboot provides a simple way to achieve such requirements, namely, CommandLineRunner.

Get to know this class first

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * Interface used to indicate that a bean should <em>run</em> when it is contained within
 * a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined
 * within the same application context and can be ordered using the {@link Ordered}
 * interface or {@link Order @Order} annotation.
 * <p>
 * If you need access to {@link ApplicationArguments} instead of the raw String array
 * consider using {@link ApplicationRunner}.
 *
 * @author Dave Syer
 * @see ApplicationRunner
 */
@FunctionalInterface
public interface CommandLineRunner {
    
    

	/**
	 * Callback used to run the bean.
	 * @param args incoming main method arguments
	 * @throws Exception on error
	 */
	void run(String... args) throws Exception;

}

In the document, we can know the following points.

  • This is an interface that users can customize to implement the interface, and specifically implement the run method
  • Any bean within the context container can implement the run method
  • If there are multiple implementation classes of this interface in the context, you can specify the loading order through the @order annotation

So we have basically understood the function and usage of this interface.

Case Description
Define a data loading class MyStartupRunner1, sorted as 2; another data loading class MyStartupRunner2, sorted as 1. Look at the order in which they record data.

	@Component
	@Order(value = 2)
	public class MyStartupRunner1 implements CommandLineRunner{
    
    
    @Override
    public void run(String... strings) throws Exception {
    
    
        System.out.println(">>>>>>>>>>服务启动执行,执行加载数据等操作 MyStartupRunner1 order 2 <<<<");
	    }
	}

	@Component
	@Order(value = 1)
	public class MyStartupRunner2 implements CommandLineRunner {
    
    
	@Override
		public void run(String... strings) throws Exception {
    
    
    		System.out.println(">>>>服务启动执行,执行加载数据等操作 MyStartupRunner2 order 1 <<<<<<<");
    	}
	}

Guess you like

Origin blog.csdn.net/QRLYLETITBE/article/details/129326566