Spring Bootの起動パラメータをどのように設定しますか?

Spring Bootアプリケーションで起動パラメータを設定する方法は次のとおりです:

  1. application.propertiesまたはapplication.ymlファイルでパラメータを設定します。
    application.propertiesファイルでは、次の形式で起動パラメータを設定できます:
  2. server.port=8080
    logging.level.root=DEBUGサーバーポート=8080
    ログのレベル.ルート=デバッグ
  3. application.ymlファイルでは、以下の形式で起動パラメータを設定することができます:
  4. サーバー:
    ポート:8080
    ロギング:
    レベル:
    ルート:デバッグ
  5. アプリケーションを起動する際には、コマンドラインでパラメーターを使用してください。
    コマンドラインで以下の形式で起動パラメーターを設定することができます:
  6. java -jar あなたのアプリケーション.jar –server.port=8080 –logging.level.root=DEBUG
  7. コード内で注釈を使用して、パラメータを設定します。
    @SpringBootApplicationアノテーションを起動クラスに使用することで、ポート番号などの一般的な起動パラメータを設定できます。
  8. @SpringBootApplication
    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
    public class YourApplication {
    public static void main(String[] args) {
    SpringApplication.run(YourApplication.class, args);
    }
    }==>

    @SpringBootApplication
    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
    public class YourApplication {
    public static void main(String[] args) {
    SpringApplication.run(YourApplication.class, args);
    }
    }

  9. パラメータをより詳細に制御する必要がある場合は、@ConfigurationPropertiesアノテーションを使用してパラメータをカスタムの設定クラスに注入することができます。
  10. @ConfigurationProperties(prefix = “your.config”)
    @Component
    public class YourConfigProperties {
    private String property1;
    private int property2;
    // …
    // getter and setter methods
    }

    @ConfigurationProperties(prefix = “your.config”)
    @Component
    public class YourConfigProperties {
    private String property1;
    private int property2;
    // …
    // getter and setter methods
    }
  11. その後、設定クラスを有効にするために@EnableConfigurationPropertiesアノテーションを起動クラスに使用します。
  12. @SpringBootApplication
    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
    @EnableConfigurationProperties(YourConfigProperties.class)
    public class YourApplication {
    public static void main(String[] args) {
    SpringApplication.run(YourApplication.class, args);
    }
    }@SpringBootApplication
    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
    @EnableConfigurationProperties(YourConfigProperties.class)
    public class YourApplication {
    public static void main(String[] args) {
    SpringApplication.run(YourApplication.class, args);
    }
    }
  13. パラメータを設定する際に、以下の形式で属性値を指定することができます。
  14. あなたの設定のプロパティ1は値1です
    あなたの設定のプロパティ2は42です

これは、スタートアップパラメータを設定するための一般的な方法です。具体的な使用方法は必要に応じて調整できます。

bannerAds