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

application.propertiesとYAML

propertiesファイルとYAMLファイルでアプリを設定します

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

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

Where Configuration Lives

Spring Boot reads externalized configuration from application.properties or application.yml, placed under src/main/resources. This keeps tunable values out of your compiled code.

At startup Boot loads these files into the Environment, making every key available for injection, conditional beans, and auto-configuration.

  • One file for the whole app, or split per profile
  • Values can be overridden later by env vars or CLI args

The .properties Format

The classic format is a flat list of key=value lines. Nested concepts are expressed with dotted keys.

It is simple and diff-friendly, but verbose when you have deep structures because each key repeats the full path.

server.port=8080
server.servlet.context-path=/api
spring.datasource.url=jdbc:postgresql://localhost:5432/app
spring.datasource.username=app_user
spring.jpa.hibernate.ddl-auto=validate

The YAML Format

application.yml expresses the same data as a hierarchy, so shared prefixes are written once. It is easier to read for deeply nested config like spring.jpa.properties.hibernate.*.

Indentation is significant: use spaces, never tabs, and keep two spaces per level.

server:
  port: 8080
  servlet:
    context-path: /api
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/app
    username: app_user
  jpa:
    hibernate:
      ddl-auto: validate

Lists and Maps in YAML

YAML shines for collections. A sequence uses - items; a map is just nested keys. Spring binds these directly to List and Map properties.

app:
  cors:
    allowed-origins:
      - https://app.example.com
      - https://admin.example.com
  feature-flags:
    beta-search: true
    dark-mode: false

Referencing Other Properties

You can interpolate one property into another with ${...}. This avoids repeating literals and lets a single base value drive several derived keys.

app:
  name: orders-service
  base-url: https://api.example.com
  health-url: ${app.base-url}/actuator/health
logging:
  file:
    name: /var/log/${app.name}.log

Default Values with Placeholders

Inside a placeholder you can supply a fallback after a colon: ${KEY:default}. Spring uses the default when the referenced property or environment variable is absent.

app:
  timeout-ms: ${REQUEST_TIMEOUT:5000}
  region: ${AWS_REGION:eu-central-1}

Injecting Single Values with @Value

For one-off properties, inject directly with @Value. SpEL placeholders resolve against the Environment at bean creation time.

Use @Value for a handful of keys; prefer @ConfigurationProperties when a feature has many related settings.

@Service
public class OrderService {
    @Value("${app.timeout-ms:5000}")
    private long timeoutMs;

    @Value("${app.name}")
    private String appName;
}

Properties vs YAML: Trade-offs

Functionally they are interchangeable; the choice is about ergonomics.

  • .properties — trivial parsing, great for small/flat config, no indentation traps
  • .yml — concise for nested trees, native lists/maps, but indentation-sensitive

If both files exist, .properties takes precedence over .yml for the same key, which can surprise teams. Pick one convention.

Multiple Documents in One YAML

A single YAML file can hold several documents separated by ---. Combined with spring.config.activate.on-profile, this lets you keep common and profile-specific values together.

spring:
  application:
    name: orders
---
spring:
  config:
    activate:
      on-profile: dev
  datasource:
    url: jdbc:h2:mem:devdb

Type Conversion and Durations

Spring converts string values into rich types. Duration accepts suffixes like 30s or 5m, and DataSize accepts 10MB. Enums and List are also converted automatically.

app:
  cache-ttl: 30m
  max-upload: 10MB
  log-level: WARN

Where Not to Put Secrets

Checked-in application.yml is the wrong home for passwords and API keys. Keep secrets in environment variables, a vault, or a secrets manager, and reference them via placeholders.

  • Commit non-sensitive defaults only
  • Inject secrets at deploy time

Quick Check

Test your understanding of property precedence.

Recap

You learned how Spring Boot externalizes configuration.

  • application.properties and application.yml both feed the Environment
  • YAML is concise for nested trees and native lists/maps
  • Use ${...} for references and ${KEY:default} for fallbacks
  • Spring converts to types like Duration and DataSize
  • Keep secrets out of committed files

よくある質問

「application.propertiesとYAML」レッスンは無料ですか?

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

「application.propertiesとYAML」で何を学びますか?

propertiesファイルとYAMLファイルでアプリを設定します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「application.propertiesとYAML」レッスンにはどのくらい時間がかかりますか?

ほとんどの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に戻る