spring-boot 学习笔记之Runner

SpringBoot之Runner

如果想在SpringBoot容器启动后做一些事情,SpringBoot提供了两个回调类

  • CommandLineRunner : 执行参数为ApplicationArguments
    public interface ApplicationRunner {
       void run(ApplicationArguments args) throws Exception;
     }```
    
  • ApplicationRunner:执行参数为数组
    public interface CommandLineRunner {
       void run(String... args) throws Exception;
    }```
    

Demo如下:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
public class DefaultCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println(getClass().getSimpleName());
    }
}
@Component
@Order(3)
public class Runner1 extends DefaultCommandLineRunner {

}
@Component
@Order(2)
public class Runner2 extends DefaultCommandLineRunner {

}
@Component
@Order(1)
public class Runner3 extends DefaultCommandLineRunner {

}
Paste_Image.png

Order排序小的执行顺序在前

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容