How can the startup order in Spring Boot be configured?

In Spring Boot, you can use the @Order annotation or implement the Ordered interface to set the startup order.

  1. Order requested
  2. @Purchase
@SpringBootApplication
@Order(1)
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
  1. Requested
  2. Runner for Command Line
  3. The runner of the application
  4. Requested
  5. obtain the order
@Component
public class MyCommandLineRunner implements CommandLineRunner, Ordered {
    @Override
    public void run(String... args) throws Exception {
        // 执行启动逻辑
    }

    @Override
    public int getOrder() {
        return 1;
    }
}

If no startup order is specified, the default startup order is determined based on the dependency relationships of Spring Beans. If multiple Beans implement the Ordered interface or use the @Order annotation, they will be sorted and started according to their order.

bannerAds