0Pricing
Spring Boot 4 Microservices & REST APIs · Pelajaran

application.properties dan YAML

Konfigurasikan aplikasi dengan file properti dan YAML.

application.properties dan YAML adalah pelajaran Spring Boot 4 Microservices & REST APIs gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Boot 4 Microservices & REST APIs, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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

Pertanyaan yang Sering Diajukan

Apakah pelajaran “application.properties dan YAML” gratis?

Ya — teks lengkap “application.properties dan YAML” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Boot 4 Microservices & REST APIs, upgrade ke CoddyKit PRO. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “application.properties dan YAML”?

Konfigurasikan aplikasi dengan file properti dan YAML. Kamu berlatih Spring Boot 4 Microservices & REST APIs dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Spring Boot 4 Microservices & REST APIs?

Tidak diperlukan pengalaman sebelumnya. Spring Boot 4 Microservices & REST APIs di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.

Berapa lama pelajaran “application.properties dan YAML” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Boot 4 Microservices & REST APIs ini?

Ya. Setiap pelajaran Spring Boot 4 Microservices & REST APIs menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. application.properties dan YAML
  2. Profil untuk Lingkungan
  3. Konfigurasi Aman Tipe dengan @ConfigurationProperties
  4. Konfigurasi Eksternal dan Penimpaan
← Kembali ke Spring Boot 4 Microservices & REST APIs