Spring Boot Project Setup & Auto-Configuration
application.properties vs application.yml — Full Configuration Guide
Compare syntax styles, lists representation, maps representation, and environment variable bindings.
Introduction
Spring Boot provides robust configuration support, allowing external settings to adapt application behaviors without rebuilding the code. The two main formats used for property configurations are application.properties and application.yml.
While properties uses flat key-value pairs, YAML (YAML Ain't Markup Language) provides a hierarchical, indented structure that reduces repeated prefixes, making it highly readable for complex, nested configurations.
Why It Matters
Misunderstanding configuration precedence, incorrect list syntax, or missing whitespace indentations in YAML files can cause properties to bind as null or throw runtime exceptions. Correct configuration design ensures clean environment separation and permits type-safe property mapping inside Java code.
Real-World Analogy
Think of properties vs YAML like **a flat grocery receipt vs an categorized cabinet system**:
- Properties: A single continuous receipt line:
kitchen.shelf1.boxA.contents=spicesandkitchen.shelf1.boxB.contents=rice. You must repeat the full path every time. - YAML: Indented shelves where drawers are logically nested:
Under the Hood Concepts
Spring Boot evaluates external properties using two core mechanisms:
- Precedence Rules: If a property is defined in multiple places, Spring Boot loads them in a specific order. Command-line parameters (e.g.
--server.port=9000) override system environment variables, which in turn override properties inapplication.ymlorapplication.propertiesfiles. - Relaxed Binding: Spring relaxes formatting naming rules. If a Java property is named
databaseUrl, Spring will match any of these environment formats:database-url(Kebab-case, recommended for properties/YAML)DATABASE_URL(Snake-case, recommended for system environment variables)databaseUrl(Camel-case)
Practical Example
Here is a side-by-side comparison of database configuration mapping in both formats, followed by a type-safe properties configuration class:
1. flat application.properties
2. hierarchical application.yml
3. Type-Safe Configuration Mapping Class
Quick Quiz
Q1: In YAML, which symbol is used to represent the start of a list item element?
A) colon (:)
B) hyphen/dash (-)
C) square bracket ([])
D) double slash (//)
Answer: B — Indented lists in YAML use a hyphen followed by a space to denote list collection items.
Q2: Which location has the highest precedence if a property named server.port is declared in multiple locations?
A) inside application.properties on the classpath
B) inside application.yml on the classpath
C) passed as a command-line parameter when launching the jar
D) set inside a developer-specific Profile properties file
Answer: C — Command line arguments (like server.port) take top precedence, overriding classpath config files and system environment variables.
Scenario-Based Challenge
Production Scenario:
You are deploying a Spring Boot docker container to production. The database password is dynamically generated by your cloud provider and set inside the container environment as APP_DATASOURCE_PASSWORD. How does Spring map this to a properties class field named app.datasource.password without altering the file?
Spring Boot's **Relaxed Binding** and Environment properties system handles this translation automatically. In environment variables, dots . are replaced with underscores _, and case sensitivity is mapped to uppercase.
Thus, APP_DATASOURCE_PASSWORD is automatically resolved as the property key app.datasource.password and injected into any corresponding properties class or @Value reference at runtime.
Interview Questions
1. Conceptual: What is the difference between @Value annotation and @ConfigurationProperties?
@Value is useful for injecting simple individual values (e.g. @Value("${app.title}")). However, it does not support Relaxed Binding for maps, cannot validate properties using JSR-380 annotations, and can clutter classes with multiple declarations. @ConfigurationProperties binds hierarchical, typed namespaces group-by-group, supports metadata indexing, validation, and Relaxed Binding.
2. Technical: How does YAML map lists differently from standard properties in Spring Boot?
In properties, lists are mapped flatly using array index annotations: app.list[0]=val1. In YAML, list items are declared hierarchically using dashes: - val1. The underlying Spring parser converts both into standard Java java.util.List instances.
Production Considerations
Never commit database passwords, tokens, or security keys to your version control repository (Git). Instead, define placeholders in your YAML/properties file: password: ${DATABASE_PASSWORD}, forcing the deployment script to supply this key as a secure environment variable at runtime.