美文网首页
spring-boot 学习笔记之Runner

spring-boot 学习笔记之Runner

作者: linking12 | 来源:发表于2016-01-20 13:30 被阅读327次

    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排序小的执行顺序在前

    相关文章

      网友评论

          本文标题:spring-boot 学习笔记之Runner

          本文链接:https://www.haomeiwen.com/subject/aeklkttx.html