application.properties & YAML
application.properties & YAML in Fundamentals: core concepts, worked examples, and practical guidance in one focused lesson.
Introduction
application.properties and application.yml (or .yaml) fill the same Boot Environment. This course uses YAML because Orders settings are a tree and lists are obvious. Pick one format per repo.
Boot looks in ./config/, the working directory, classpath:/config/, and the classpath root. A file outside the JAR overrides the same key inside it. application-prod.yml overlays the shared file when SPRING_PROFILES_ACTIVE=prod. Inside a multi-document file, separate documents with --- and gate them with spring.config.activate.on-profile. The document key spring.profiles is retired.
If properties and YAML share a location, properties win. That surprise is why a second format looks 'ignored'. Placeholders pull from the environment; a default after a colon is for harmless values such as USD or 2s, never a password.
Indent with two spaces. Tabs fail the scan. Quote scalars YAML 1.1 may read as booleans ("NO", "on", "off"). Boot rejects duplicate keys in one document, so a second server.port should fail startup.
Purpose of this lesson
Describe Orders in YAML, overlay a prod profile without a second codebase, and keep every secret in an environment placeholder.
Understanding the topic
Nested YAML flattens to the same names as dotted properties.
- Lists: a dash sequence under
allowed-currenciesbinds toList<String>. - Profile files hold overrides only: log level, a tighter timeout. They do not copy the whole tree.
- Placeholders resolve from higher-precedence sources. A colon default applies only when the variable is absent.
- One extension. Mixing
.yml,.yaml, and.propertiescreates precedence puzzles.
Step-by-step explanation
- Commit
application.ymlwith the port andorders.pricing. No secrets. - Add
application-prod.ymlwith only the keys that change. - Start with
SPRING_PROFILES_ACTIVE=prodand find the active-profile line in the log. - Insert a tab, read the scanner error, restore two-space indentation, and quote any country code.
Informative example
Shared file plus a prod document. The second document overrides the timeout and the log level. It does not repeat currency.
server:port: 8080orders:pricing:currency: ${ORDERS_CURRENCY:USD}tax-basis-points: 800allowed-currencies:- USD- EUR---spring:config:activate:on-profile: prodlogging:level:com.techlearningpro.orders: INFOorders:pricing:quote-timeout: 750ms
Real-world use
TechLearningPro Orders ships one application.yml: port 8080, a non-secret USD default, and a currency allow-list. Production sets SPRING_PROFILES_ACTIVE=prod so the overlay tightens the quote timeout and logs at INFO. The database password is the placeholder ORDERS_DB_PASSWORD with no default, injected by the platform. Reviewers can read the Git file and still cannot connect to production.
Security implications
Packaged YAML is world-readable to anyone who can unzip the image. Treat it as public.
- Secrets are placeholders with no default. A
changemefallback hides a missing variable and ships a known password. - A wrong profile should fail closed. That happens only when prod credentials are absent from the dev document.
- Do not parse request bodies with SnakeYAML. Boot's loader uses a safe constructor;
new Yaml()on user input is a historic remote-code-execution class of bugs. Orders uses Jackson. - A world-writable
./config/application.ymloutranks the JAR. Restrict it to the service account.
Best practices
- Standardize on
application.yml. Profile files contain deltas only. - Set
SPRING_PROFILES_ACTIVEon the deployment, not inside the shared file. - Write booleans as
trueandfalse. Comment that800basis points means 8.00%. - Log the active profiles at startup. Do not log the resolved
Environment.
Common mistakes
- Tabs, or duplicate keys in one document. The parser fails; an override belongs in another document or file.
- Both formats in one folder. Properties win, and the YAML value looks broken.
spring.profiles: prodinside a Boot 3 document. Usespring.config.activate.on-profile.- Unquoted
NOoryes. YAML 1.1 can bind those tokens as booleans. Quote"NO". - A production password in
application-prod.ymlbecause the file 'only runs in prod'. It is still in Git and in the image.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionHow does application-prod.yml interact with application.yml?+
Answer
prod is active. Only the keys you repeat change. Turn the profile on with SPRING_PROFILES_ACTIVE. In one file, gate a document with spring.config.activate.on-profile.2IntermediateQuestionWhy can an unquoted NO become false, and which file wins if both formats set the port?+
Answer
yes, no, on, and off as booleans, so quote country codes. In the same location, application.properties overrides application.yml. External files override packaged ones. A tab fails the scan outright.3AdvancedQuestionHow should the Orders database password appear in YAML?+
Answer
application-prod.yml is in Git and in image layers. A default such as changeme is worse than a failed boot.Hands-on exercise
Lab. Write the shared YAML with a USD currency default and a list of USD and EUR. Add a prod file that sets the Orders logger to INFO and the quote timeout to 750ms. Start with SPRING_PROFILES_ACTIVE=prod, confirm the log line, then quote an unquoted NO after seeing how the binder treats it.
Summary
Orders ships a readable application.yml and a thin prod overlay. Profiles select the overlay, placeholders import secrets, and a missing password fails startup — the configuration doing its job.