Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 11

    application.properties & YAML

    application.properties & YAML in Fundamentals: core concepts, worked examples, and practical guidance in one focused lesson.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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-currencies binds to List<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 .properties creates precedence puzzles.

    Step-by-step explanation

    1. Commit application.yml with the port and orders.pricing. No secrets.
    2. Add application-prod.yml with only the keys that change.
    3. Start with SPRING_PROFILES_ACTIVE=prod and find the active-profile line in the log.
    4. 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.

    bash
    server:
    port: 8080
    orders:
    pricing:
    currency: ${ORDERS_CURRENCY:USD}
    tax-basis-points: 800
    allowed-currencies:
    - USD
    - EUR
    ---
    spring:
    config:
    activate:
    on-profile: prod
    logging:
    level:
    com.techlearningpro.orders: INFO
    orders:
    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 changeme fallback 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.yml outranks the JAR. Restrict it to the service account.

    Best practices

    • Standardize on application.yml. Profile files contain deltas only.
    • Set SPRING_PROFILES_ACTIVE on the deployment, not inside the shared file.
    • Write booleans as true and false. Comment that 800 basis 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: prod inside a Boot 3 document. Use spring.config.activate.on-profile.
    • Unquoted NO or yes. YAML 1.1 can bind those tokens as booleans. Quote "NO".
    • A production password in application-prod.yml because 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.

    3 questions
    1BeginnerQuestionHow does application-prod.yml interact with application.yml?+

    Answer

    Boot loads the shared file, then overlays the profile file when 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

    YAML 1.1 treats 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

    As a placeholder with no default, resolved from the environment or a secret mount. A literal in 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.

    Ready to mark this lesson complete?Track your journey across the entire course.