关于 Interface CommandLineRunner 执行顺序

多个 CommandLineRunner 写法

 1 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.boot.CommandLineRunner;
 6 import org.springframework.core.annotation.Order;
 7 import org.springframework.stereotype.Component;
 8 
 9 @Component11 public class Runner01 implements CommandLineRunner {
12 
13     private static final Logger log = LoggerFactory.getLogger("log");
14 
15     @Override
16     public void run(String... args) throws Exception {
17         log.info("run01");
18     }
19 }
 1 @Component 3 public class Runner02 implements CommandLineRunner {
 4     private static final Logger log = LoggerFactory.getLogger("log");
 5 
 6     @Override
 7     public void run(String... args) throws Exception {
 8         log.info("run02");
 9     }
10 }

执行顺序为 Application > Runner

 1 import org.slf4j.Logger;
 2 import org.slf4j.LoggerFactory;
 3 import org.springframework.boot.CommandLineRunner;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 @SpringBootApplication
 8 public class Application implements CommandLineRunner {
 9     private static final Logger log = LoggerFactory.getLogger("log");
10 
11     public static void main(String[] args) {
12         SpringApplication.run(Application.class, args);
13     }
14 
15     @Override
16     public void run(String... args) throws Exception {
17         log.info("Application");
18     }
19 }

启动类实现 CommandLineRunner 接口,执行顺序为 Application > Runner

 1 @Component
 2 @Order(value = 300)
 3 public class Runner01 implements CommandLineRunner {
 4 
 5     private static final Logger log = LoggerFactory.getLogger("log");
 6 
 7     @Override
 8     public void run(String... args) throws Exception {
 9         log.info("run01");
10     }
11 }
12 
13 @Component
14 @Order(value = 2)
15 public class Runner02 implements CommandLineRunner {
16     private static final Logger log = LoggerFactory.getLogger("log");
17 
18     @Override
19     public void run(String... args) throws Exception {
20         log.info("run02");
21     }
22 }
23 
24 @Component
25 @Order(value = 1)
26 public class Runner03 implements CommandLineRunner {
27     private static final Logger log = LoggerFactory.getLogger("log");
28 
29     public void run(String... args) throws Exception {
30         log.info("run03");
31     }
32 }
33 
34 @Component
35 public class Runner04 implements CommandLineRunner {
36     private static final Logger log = LoggerFactory.getLogger("log");
37 
38     @Override
39     public void run(String... args) throws Exception {
40         log.info("run04");
41     }
42 }
43 
44 @SpringBootApplication
45 public class Application implements CommandLineRunner {
46     private static final Logger log = LoggerFactory.getLogger("log");
47 
48     public static void main(String[] args) {
49         SpringApplication.run(Application.class, args);
50     }
51 
52     @Override
53     public void run(String... args) throws Exception {
54         log.info("Application");
55     }
56 }

使用Order注解,Order 数值小的先执行 > Application > Runner

如果 Application 也添加 Order 注解,Application 先于同级 Order 执行

posted @ 2020-06-01 10:07  河图书卦  阅读(1570)  评论(0编辑  收藏  举报