【SpringBoot】零碎

 

启动后自动执行任务

实现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..........");
    }
}
View Code

执行效果:

原文地址:https://www.cnblogs.com/clarino/p/15496160.html