Spring Bootの起動パラメータをどのように設定しますか?
Spring Bootアプリケーションで起動パラメータを設定する方法は次のとおりです:
- application.propertiesまたはapplication.ymlファイルでパラメータを設定します。
application.propertiesファイルでは、次の形式で起動パラメータを設定できます: - server.port=8080
logging.level.root=DEBUGサーバーポート=8080
ログのレベル.ルート=デバッグ - application.ymlファイルでは、以下の形式で起動パラメータを設定することができます:
- サーバー:
ポート:8080
ロギング:
レベル:
ルート:デバッグ - アプリケーションを起動する際には、コマンドラインでパラメーターを使用してください。
コマンドラインで以下の形式で起動パラメーターを設定することができます: - java -jar あなたのアプリケーション.jar –server.port=8080 –logging.level.root=DEBUG
- コード内で注釈を使用して、パラメータを設定します。
@SpringBootApplicationアノテーションを起動クラスに使用することで、ポート番号などの一般的な起動パラメータを設定できます。 - @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);
}
} - パラメータをより詳細に制御する必要がある場合は、@ConfigurationPropertiesアノテーションを使用してパラメータをカスタムの設定クラスに注入することができます。
- @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
} - その後、設定クラスを有効にするために@EnableConfigurationPropertiesアノテーションを起動クラスに使用します。
- @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);
}
} - パラメータを設定する際に、以下の形式で属性値を指定することができます。
- あなたの設定のプロパティ1は値1です
あなたの設定のプロパティ2は42です
これは、スタートアップパラメータを設定するための一般的な方法です。具体的な使用方法は必要に応じて調整できます。