Sub topic 2
Writing application.properties and application.yaml
This chapter assumes you have a Spring Boot project built with Maven or Gradle and can read basic YAML and .properties syntax. By the end you will be able to write both file formats and split one YAML file into per-profile sections.
Where Boot looks for the files
Spring Boot loads application.properties and application.yaml (.yml also works) automatically at startup. The usual place is src/main/resources, which ends up at the root of the classpath. Boot also checks a config/ folder next to the jar, and files there can override the ones packaged inside it.
The same settings in two formats
Both files describe a tree of keys. A .properties file writes the full dotted path on every line. YAML shows the same path through indentation.
app.mail.host=localhost
app.mail.port=1025
[email protected]
app.mail.retry-attempts=3app:
mail:
host: localhost
port: 1025
sender: [email protected]
retry-attempts: 3YAML avoids repeating prefixes, which helps when many keys share one. A .properties file is easier to scan with grep and has no indentation rules. Pick one format per project and stay with it.
Multiple documents in one file
A YAML file can hold several documents separated by ---. Boot reads each one as its own block of configuration. A block can be limited to a profile with spring.config.activate.on-profile.
app:
mail:
host: localhost
port: 1025
sender: [email protected]
retry-attempts: 3
---
spring:
config:
activate:
on-profile: prod
app:
mail:
host: smtp.example.com
port: 587The first document is the default. The second applies only when the prod profile is active. In that case it replaces host and port, while sender and retry-attempts keep their default values. Profiles are covered in detail in a later chapter.
A .properties file can do the same with a #--- line on its own:
app.mail.host=localhost
app.mail.port=1025
#---
spring.config.activate.on-profile=prod
app.mail.host=smtp.example.com
app.mail.port=587Common mistakes
- Tabs in YAML. YAML allows only spaces for indentation. A tab causes a parse error at startup.
- Unquoted special values. YAML 1.1 can read
no,offandonas booleans. Quote them if you mean text, as in"no". - Mixed formats. Boot loads both files if both exist. When both define the same key, the
.propertiesvalue wins. Keeping one format avoids that surprise. - Wrong indentation. A key indented one level too deep or too shallow becomes a different property, and Boot will not warn you.
Seeing it work
Start the application with the defaults and the values come from the first document. Start it with --spring.profiles.active=prod and the prod document overrides the host and port. The file itself never changes.
Exercise
A YAML file has two documents. The first sets app.mail.port to 1025 and app.mail.sender to a default address. The second has on-profile: prod and sets only app.mail.port to 587. With prod active, what are the port and sender?
Show answer
The port is 587 and the sender is the default address from the first document. The prod document overrides only the keys it defines, and all other keys keep their default values.