0Pricing
Spring Boot 4 Microservices & REST APIs · レッスン

@ConfigurationPropertiesによる型安全な設定

プロパティを型付き設定クラスにバインドします

「@ConfigurationPropertiesによる型安全な設定」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Beyond @Value

Scattering dozens of @Value annotations across a feature is brittle and hard to validate. @ConfigurationProperties binds a whole group of related keys to a single strongly typed object.

You get IDE auto-completion, type safety, validation, and a clear contract for what the feature reads.

Binding a Prefix to a Class

Declare a class with a prefix; Spring maps each key under that prefix to a matching field. Relaxed binding lets max-retries in YAML map to maxRetries in Java.

@ConfigurationProperties(prefix = "app.mail")
public class MailProperties {
    private String host;
    private int port;
    private int maxRetries;
    // getters and setters
}

Registering the Properties Bean

A @ConfigurationProperties class must become a bean. The cleanest way is @EnableConfigurationProperties on a configuration class, or @ConfigurationPropertiesScan on the main app.

@SpringBootApplication
@ConfigurationPropertiesScan
public class Application { }

// or explicitly:
@Configuration
@EnableConfigurationProperties(MailProperties.class)
class MailConfig { }

The Backing YAML

With the prefix app.mail, these YAML keys bind to the fields. Relaxed binding accepts kebab-case keys for camelCase fields.

app:
  mail:
    host: smtp.example.com
    port: 587
    max-retries: 3

Injecting the Properties

Inject the typed object anywhere like any other bean. The feature code reads structured config instead of loose strings.

@Service
public class MailService {
    private final MailProperties props;
    public MailService(MailProperties props) {
        this.props = props;
    }
    void send() {
        connect(props.getHost(), props.getPort());
    }
}

Immutable Binding with Records

Spring Boot supports constructor binding, perfect for immutable config. A Java record works directly with @ConfigurationProperties, no setters needed.

@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(
        String host,
        int port,
        int maxRetries) {
}

Default Values

With records you can give defaults in a compact constructor; with classic classes you initialize the field. Defaults apply when the key is absent from the Environment.

@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(String host, Integer port, Integer maxRetries) {
    public MailProperties {
        if (port == null) port = 587;
        if (maxRetries == null) maxRetries = 3;
    }
}

Nested Objects

Configuration trees map to nested types. A field of a custom type binds keys one level deeper, keeping complex config organized.

@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private final Security security = new Security();
    public Security getSecurity() { return security; }
    public static class Security {
        private boolean enabled;
        private String issuer;
        // getters/setters
    }
}

Lists and Maps

Collection-typed fields bind to YAML sequences and maps automatically, which is far cleaner than parsing comma-separated strings by hand.

@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private List<String> allowedOrigins;
    private Map<String, Integer> rateLimits;
    // getters/setters
}
// app.allowed-origins: [a, b]
// app.rate-limits: { read: 100, write: 20 }

Validating Bound Values

Add @Validated to the properties class and JSR-380 constraints to fields. Spring validates at startup and fails fast if config is invalid, instead of erroring deep in production.

@Validated
@ConfigurationProperties(prefix = "app.mail")
public class MailProperties {
    @NotBlank private String host;
    @Min(1) @Max(65535) private int port;
    // getters/setters
}

Metadata and the Processor

Add spring-boot-configuration-processor as an annotation processor to generate metadata. Your IDE then offers auto-complete and docs for your custom keys in application.yml.

<!-- pom.xml -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

Quick Check

Test your understanding of relaxed binding.

Recap

@ConfigurationProperties gives type-safe, grouped configuration.

  • Bind a prefix to a class or record
  • Register via @ConfigurationPropertiesScan or @EnableConfigurationProperties
  • Records enable immutable constructor binding
  • Nested types, lists, and maps bind naturally
  • @Validated makes invalid config fail fast at startup

よくある質問

「@ConfigurationPropertiesによる型安全な設定」レッスンは無料ですか?

はい。「@ConfigurationPropertiesによる型安全な設定」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

「@ConfigurationPropertiesによる型安全な設定」で何を学びますか?

プロパティを型付き設定クラスにバインドします ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「@ConfigurationPropertiesによる型安全な設定」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?

はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. application.propertiesとYAML
  2. 環境ごとのプロファイル
  3. @ConfigurationPropertiesによる型安全な設定
  4. 外部化と上書きによる設定
← Spring Boot 4 Microservices & REST APIsに戻る