application.properties vs application.yml
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)ShopLite's page size, support email and database address are different on your laptop and on the live server. If they sit inside Java code, every change needs a rebuild and a new release.
The same settings in properties and YAML
Configuration is every value an app needs that is not code, such as a store name, a page size or a database address. Spring Boot reads it from application.properties or application.yml.
application.properties holds one key=value setting per line. application.yml holds the same keys as a tree: each dot becomes a new, more indented line. Example 1 reads them through the Environment bean.
Example 1 · SettingsReport.java and application.properties
Runs in: the config-lab practice project (Spring Boot 4.1)
// src/main/java/com/shoplite/lab/SettingsReport.javapackage com.shoplite.lab;
import org.springframework.boot.CommandLineRunner;import org.springframework.core.env.Environment;import org.springframework.stereotype.Component;
@Componentpublic class SettingsReport implements CommandLineRunner {
private final Environment env;
public SettingsReport(Environment env) { // Spring passes in the Environment: every setting it loaded this.env = env; }
@Override public void run(String... args) { System.out.println("App name: " + env.getProperty("spring.application.name")); System.out.println("Store name: " + env.getProperty("shoplite.store-name")); System.out.println("Support email: " + env.getProperty("shoplite.support.email")); System.out.println("Support phone: " + env.getProperty("shoplite.support.phone"));
Integer maxPageSize = env.getProperty("shoplite.max-page-size", Integer.class); System.out.println("Max page size: " + maxPageSize + " (an Integer, so maths works: " + (maxPageSize * 2) + ")");
System.out.println("First city: " + env.getProperty("shoplite.delivery-cities[0]")); System.out.println("Whole list: " + env.getProperty("shoplite.delivery-cities")); }}
# src/main/resources/application.propertiesspring.application.name=config-labspring.main.banner-mode=offlogging.level.root=warn
shoplite.store-name=ShopLiteshoplite.support.email=help@shoplite.exampleshoplite.support.phone=1800-123-4567shoplite.max-page-size=100shoplite.delivery-cities[0]=Puneshoplite.delivery-cities[1]=Chennaishoplite.delivery-cities[2]=KochiApp name: config-lab Store name: ShopLite Support email: help@shoplite.example Support phone: 1800-123-4567 Max page size: 100 (an Integer, so maths works: 200) First city: Pune Whole list: null
What this code does
SettingsReportis aCommandLineRunner: Spring calls itsrunmethod once after start-up. It receives theEnvironmentin its constructor and asks it for each key.getProperty("shoplite.max-page-size", Integer.class)converts the text100to anInteger, somaxPageSize * 2works. Without the type, you get aString.- A list is stored as separate keys
[0],[1],[2], sodelivery-cities[0]isPunebut the whole-list key isnull. The next lesson binds a whole list in one step with@ConfigurationProperties.
Example 2 · src/main/resources/application.yml: the same settings
Runs in: the config-lab practice project (Spring Boot 4.1)
spring: application: name: config-lab main: banner-mode: "off"logging: level: root: warnshoplite: store-name: ShopLite support: email: help@shoplite.example phone: 1800-123-4567 max-page-size: 100 delivery-cities: - Pune - Chennai - KochiApp name: config-lab Store name: ShopLite Support email: help@shoplite.example Support phone: 1800-123-4567 Max page size: 100 (an Integer, so maths works: 200) First city: Pune Whole list: null
What this code does
- Delete
application.properties, add this file and run the sameSettingsReportfrom Example 1: the output is identical, line for line. spring:/application:/name:is the YAML tree forspring.application.name. Each level is two spaces deeper; the list items- Punebecomedelivery-cities[0]and so on."off"is in quotes on purpose: unquotedoffis a yes/no value in YAML (see the next section).
Why not hard-code these values in Java? A value in code needs a rebuild and a new release to change, and it is the same on your laptop and on the live server. A value in a file can differ per computer and can be replaced at start-up. The dots in a key such as shoplite.support.email are only part of its name.
The Environment is the Spring bean that holds every setting Boot has loaded: env.getProperty("key") returns the value as a String, or null when the key is missing. Three small rules:
- A list in YAML is written with
-items; in a properties file it iskey[0]=,key[1]=and so on. - Text never needs quotes in a properties file. Everything after the first
=is the value, spaces included. - Both formats allow comments: a line starting with
#.
config-lab on start.spring.io like shoplite-api (Maven, Java 17, Boot 4.1.x), package com.shoplite.lab, no dependencies yet. The examples set spring.main.banner-mode=off and logging.level.root=warn, which hide the Spring logo and the start-up lines, so the console shows only what the examples print.YAML mistakes: tabs, indentation and surprise values
YAML is a text format for settings written as an indented tree. Spaces show which key belongs inside which, and tab characters are not allowed.
YAML reads the indentation with spaces to know which key belongs inside which. Some mistakes stop the app at start-up; the worse ones change a value and print nothing.
Example 3 · A tab in application.yml stops the app at start-up
Runs in: the config-lab practice project (Spring Boot 4.1)
spring: application: name: config-lab main: banner-mode: "off"logging: level: root: warnshoplite: store-name: ShopLite support: email: help@shoplite.example phone: 1800-123-4567 max-page-size: 100 delivery-cities: - Pune - Chennai - Kochi11:16:03.323 [main] ERROR org.springframework.boot.SpringApplication -- Application run failed
while scanning for the next token
found character '\t(TAB)' that cannot start any token. (Do not use \t(TAB) for indentation)
in 'reader', line 13, column 1:
email: help@shoplite.example
^
at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:460)
... (more "at" lines)What this code does
- Line 13 starts with a tab instead of four spaces. Boot reads the YAML before any bean exists, so the app stops at once with
Application run failed. line 13, column 1and the^mark point at the exact character. Replace the tab with spaces and the app starts again.- The log line looks plain (no PID, no app name) because the logging system is not set up yet when the file is read.
Example 4 · Wrong indentation: no error, just a missing value
Runs in: the config-lab practice project (Spring Boot 4.1)
spring: application: name: config-lab main: banner-mode: "off"logging: level: root: warnshoplite: store-name: ShopLite support: email: help@shoplite.example phone: 1800-123-4567 max-page-size: 100 delivery-cities: - Pune - Chennai - KochiApp name: config-lab Store name: ShopLite Support email: help@shoplite.example Support phone: null Max page size: 100 (an Integer, so maths works: 200) First city: Pune Whole list: null
What this code does
phonesits two spaces deep instead of four, so YAML files it undershoplite, not undershoplite.support. The keyshoplite.support.phonedoes not exist, andSettingsReportprintsnull.- The app starts normally. This kind of mistake shows up later, for example as a
NullPointerExceptionwhen code uses the value. - The list items here start at the same column as
delivery-cities:. That is valid YAML, so the cities still load.
A tab stops the app with found character '\t(TAB)' that cannot start any token, and the message gives the line and column. A missing space after the colon (max-page-size:100) stops it with could not find expected ':'. A line two spaces too far left belongs to a different parent, so the value is saved under a different key: your code asks for the right key, gets null and carries on.
YAML also guesses types. Unquoted 1.10 is the number 1.1, 0123 is an octal number (83), and off, on, yes, no and NO become true or false. Quote such values (app-version: "1.10") whenever you mean the exact text.
Fix the error
Fix the YAML line
Each file breaks at start-up. Pick the line that replaces the marked one.
Round 1 of 3
A tab sits in front of the email key.
Error
found character '\t(TAB)' that cannot start any token in 'reader', line 3, column 1
Relaxed binding: one setting, many spellings
Relaxed binding means Spring Boot treats different spellings of a key as the same setting, for example max-page-size, maxPageSize and the environment variable SHOPLITE_MAXPAGESIZE.
One setting can be written in several ways. In a file, shoplite.max-page-size and shoplite.maxPageSize set the same value. As an environment variable, the key becomes SHOPLITE_MAXPAGESIZE.
Example 5 · PageSizeReport.java: one key from different spellings
Runs in: the config-lab practice project (Spring Boot 4.1)
// src/main/java/com/shoplite/lab/PageSizeReport.javapackage com.shoplite.lab;
import org.springframework.boot.CommandLineRunner;import org.springframework.core.env.Environment;import org.springframework.stereotype.Component;
@Componentpublic class PageSizeReport implements CommandLineRunner {
private final Environment env;
public PageSizeReport(Environment env) { this.env = env; }
@Override public void run(String... args) { System.out.println("shoplite.max-page-size = " + env.getProperty("shoplite.max-page-size")); }}
# src/main/resources/application.propertiesspring.application.name=config-labspring.main.banner-mode=offlogging.level.root=warn
shoplite.max-page-size=100$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar shoplite.max-page-size = 100 $ SHOPLITE_MAXPAGESIZE=60 java -jar target/config-lab-0.0.1-SNAPSHOT.jar shoplite.max-page-size = 60 $ SHOPLITE_MAX_PAGE_SIZE=70 java -jar target/config-lab-0.0.1-SNAPSHOT.jar shoplite.max-page-size = 70 $ MAX_PAGE_SIZE=70 java -jar target/config-lab-0.0.1-SNAPSHOT.jar shoplite.max-page-size = 100
What this code does
- Package the project with
./mvnw package(.\mvnw packageon Windows), then run the jar with an environment variable in front, as the output shows (macOS and Git Bash syntax; PowerShell is in the next section). SHOPLITE_MAXPAGESIZEandSHOPLITE_MAX_PAGE_SIZEboth reachshoplite.max-page-size.MAX_PAGE_SIZEhas noSHOPLITE_prefix, so it is a different key and the file value 100 stays.- The code always asks for the kebab-case name
shoplite.max-page-size; relaxed binding does the matching.
The recommended spelling is the kebab-case form, max-page-size: lower case with dashes. A file also accepts maxPageSize, max_page_size and MAX-PAGE-SIZE. Environment variables cannot contain dots or dashes on most systems, so dots become _, dashes are dropped and everything is upper case. Boot 4.1 also accepts SHOPLITE_MAX_PAGE_SIZE, but the form without extra underscores is the documented one.
The prefix is part of the name. MAX_PAGE_SIZE alone is a different key, ignored without any message. When you read a value in code, always ask for the kebab-case name, as Example 5 does.
Where values come from and who wins
A property source is one place Spring Boot reads settings from, such as a file, the environment variables or the command line. When two sources set the same key, the stronger one wins.
Boot collects settings from many places at once. The picture shows the five you use daily, strongest first, for one key: shoplite.max-page-size.
Example 6 · Terminal: the same key from four sources
Runs in: the config-lab practice project (Spring Boot 4.1)
# 1. Only the file inside the jar: application.properties says 100java -jar target/config-lab-0.0.1-SNAPSHOT.jar# 2. An environment variable beats the fileSHOPLITE_MAXPAGESIZE=60 java -jar target/config-lab-0.0.1-SNAPSHOT.jar# 3. A Java system property (-D, before -jar) beats the environment variableSHOPLITE_MAXPAGESIZE=60 java -Dshoplite.max-page-size=90 -jar target/config-lab-0.0.1-SNAPSHOT.jar# 4. A command-line argument (after the jar name) beats everythingSHOPLITE_MAXPAGESIZE=60 java -Dshoplite.max-page-size=90 -jar target/config-lab-0.0.1-SNAPSHOT.jar --shoplite.max-page-size=80$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar shoplite.max-page-size = 100 $ SHOPLITE_MAXPAGESIZE=60 java -jar target/config-lab-0.0.1-SNAPSHOT.jar shoplite.max-page-size = 60 $ SHOPLITE_MAXPAGESIZE=60 java -Dshoplite.max-page-size=90 -jar target/config-lab-0.0.1-SNAPSHOT.jar shoplite.max-page-size = 90 $ SHOPLITE_MAXPAGESIZE=60 java -Dshoplite.max-page-size=90 -jar target/config-lab-0.0.1-SNAPSHOT.jar --shoplite.max-page-size=80 shoplite.max-page-size = 80
What this code does
- Each run adds one stronger source. The printed value always comes from the strongest source that sets the key.
- In PowerShell, set the variable first with
$env:SHOPLITE_MAXPAGESIZE = "60", then runjava -jar ...; remove it again withRemove-Item Env:SHOPLITE_MAXPAGESIZE. - Nothing was rebuilt between runs: the same jar printed four different values.
The ranking is why one jar can run anywhere. The jar carries safe defaults, the server sets environment variables, and you add a -- argument for a quick one-off test. Between the files, ./config/application.properties is read before ./application.properties (the folder you start the app from).
When both .properties and .yml sit in the same folder, both are read and the .properties value wins for a key set in both.
Predict the output
Which value wins?
Read the sources, then pick the value the app uses.
Round 1 of 3
What is shoplite.max-page-size?
Picking one format per project
Both formats work equally well; the important rule is one format per project. With both files in one folder, a reader cannot tell which value is used without knowing the precedence rule above.
Pick properties when settings are few and flat, when you want copy-paste lines for documentation and command-line arguments, or when your team is new to YAML. Pick YAML when the settings are deeply nested or hold lists, because the tree is easier to scan and keys are not repeated.
ShopLite uses application.properties: each line is complete on its own, so it searches well, diffs cleanly in Git, and the same line works as a -- argument. Most Spring Boot documentation shows both forms side by side, so you can always translate.
Next step
Check your understanding
11 quick questions