Spring Boot:项目启动执行CommandLineRunner

Spring Boot提供了一个借口CommandLineRunner,实现这个接口可以使得在项目启动时去执行一些操作。@Order注解来规定调用所有CommandLineRunner实例的顺序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.roger.commandline;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(value = 1)
public class FirstCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
System.out.println(">>>>>>>>first command line runner: " + strings);
}
}