SpringBootの起動順序はどのように設定しますか?
Spring Bootでは、起動順序を設定するために@OrderアノテーションやOrderedインタフェースを使用することができます。
- 注文
- ご注文
@SpringBootApplication
@Order(1)
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
- 注文されました。
- コマンドラインランナー
- アプリケーションランナー
- 注文した
- オーダーを取得する
@Component
public class MyCommandLineRunner implements CommandLineRunner, Ordered {
@Override
public void run(String... args) throws Exception {
// 执行启动逻辑
}
@Override
public int getOrder() {
return 1;
}
}
注意:起動順序が指定されていない場合、デフォルトの起動順序はSpring Beanの依存関係に従って決定されます。複数のBeanがOrderedインターフェースを実装したり、@Orderアノテーションを使用した場合、それらの順序に従ってソートされて起動されます。