關於 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 執行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM