application.properties und YAML
Konfigurieren Sie Apps mit properties- und YAML-Dateien.
application.properties und YAML ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Microservices & REST APIs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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=validateThe 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: validateLists 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: falseReferencing 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}.logDefault 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:devdbType 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: WARNWhere 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.propertiesandapplication.ymlboth 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
DurationandDataSize - Keep secrets out of committed files
Häufig gestellte Fragen
Ist die Lektion „application.properties und YAML“ kostenlos?
Ja — der vollständige Text von „application.properties und YAML“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Microservices & REST APIs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „application.properties und YAML“?
Konfigurieren Sie Apps mit properties- und YAML-Dateien. Du übst Spring Boot 4 Microservices & REST APIs mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Spring Boot 4 Microservices & REST APIs zu starten?
Keine Vorkenntnisse erforderlich. Spring Boot 4 Microservices & REST APIs auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „application.properties und YAML“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Spring Boot 4 Microservices & REST APIs-Lektion Code schreiben und ausführen?
Ja. Jede Spring Boot 4 Microservices & REST APIs-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- application.properties und YAML
- Profile für Umgebungen
- Typsichere Konfiguration mit @ConfigurationProperties
- Externe und überschreibbare Konfiguration