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.
- Order requested
- @Purchase
@SpringBootApplication
@Order(1)
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
- Requested
- Runner for Command Line
- The runner of the application
- Requested
- 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.