Spring Boot starts loading data CommandLineRunner

Keywords: Spring

In practice, we will have the need to load some data or do something when the project service starts. To solve this problem, Spring Boot provides us with a way to implement the interface CommandLineRunner. Simple, just one class, no other configuration.  

 

By watching https://my.oschina.net/sdlvzg/blog/1154281 Create a project and do the following

Write code

Write the first boot load data class

package org.lvgang.startrun;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * Service startup execution
 * @author Administrator
 *
 */
@Component
public class StartRun1 implements CommandLineRunner {
	public void run(String... arg0) throws Exception {
		System.out.println("=========Service startup execution, loading data, etc.===========");
	}
}

After the above class is written, the first start-up loading data class is completed. The project can be started through the Main method. The following information will be output in the console, indicating that the loading is successful.

========= Service startup execution, loading data, etc.===========

 

Multiple Start Loading Data Classes, Sort Running

When using a project, we sometimes write multiple loading data classes, and then there is a sequence, so we can use the @Order Annotations define the order of execution.

For example:

First Start Data Loading Class

package org.lvgang.startrun;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Service startup execution
 * @author Administrator
 *
 */
@Component
@Order(value=1)
public class StartRun1 implements CommandLineRunner {
	public void run(String... arg0) throws Exception {
		System.out.println("====1111=====Service startup execution, loading data, etc.===========");
	}
}

The second boot data loading class

package org.lvgang.startrun;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Service startup execution
 * @author Administrator
 *
 */
@Component
@Order(value=2)
public class StartRun2 implements CommandLineRunner {
	public void run(String... arg0) throws Exception {
		System.out.println("=====2222====Service startup execution, loading data, etc.===========");
	}
}

Complete the above two classes, you can start the project through the Main method, and the console will output the following information, which means that the loading is successful.

==== 1111======== Service Start Execution, Load Data, etc.===========
===== 2222===== Service Start Execution, Load Data, etc.===========

 

Start data loading can be implemented either by CommandLineRunner or by Application Runner, whose execution time is when the container is started.

The same point: first, the execution time is when the container starts and completes; second, there is a run() method in both interfaces;

Differences: The parameter of run method in Application Runner is Application Arguments, while the parameter of run method in CommandLine Runner interface is String array.

In fact, there is no big difference between the two. If you want to get command line parameters in more detail, you use the Application Runner interface.

Posted by scanreg on Sat, 15 Jun 2019 13:02:15 -0700