启动后自动执行任务
实现CommandLineRunner接口,实现其中的run(String... args) 方法。
如果存在多个实现类,则指定 @Order注解决定顺序
@SpringBootApplication @Order(2) public class Application implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { System.out.println("Application run.........."); } } /////////////////////////////////////////////////// @Component @Order(1) public class Service1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Service1 run..............."); } } /////////////////////////////////////////////////// @Component @Order(3) public class Service3 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Service3 run.........."); } }
执行效果: