AI Hints
Home›Spring Boot Tutorial›YAML, Profiles and Logging
Spring Boot · Lesson 40 of 120

YAML

Settings with indentation.

⏱ 2 min read▶ Video at 52:58🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Instead of application dot properties, many projects use application dot yml. It holds the same settings, in a format called YAML.

  • Same settings
  • Different format
  • application.yml

Properties and YAML

In a properties file, every line repeats the full key. In YAML, the key is split into levels, and each level is indented. Lists get their own lines, starting with a dash. Both work exactly the same way. Pick one per project.

application.properties

  • shop.name=Corner Booksshop.discount=15shop.categories=fiction,science

application.yml

  • shop: name: Corner Books categories: - fiction

application.yml

Spring application name becomes three nested levels. Shop has three keys, indented under it. A space after each colon is required. Categories is a list, one item per line. Run it. The same shop properties record is filled, from YAML this time.

application.ymlyaml
spring:
  application:
    name: bookshop

shop:
  name: Corner Books
  discount: 15
  categories:
    - fiction
    - science
    - history
Output
{"name":"Corner Books","discount":15,"categories":["fiction","science","history"]}
Also uses ShopProperties.java
@ConfigurationProperties("shop")
public record ShopProperties(String name, int discount, List<String> categories) {}
Also uses ShopConfig.java
@Configuration
@EnableConfigurationProperties(ShopProperties.class)
public class ShopConfig {}
Also uses ShopController.java
@RestController
public class ShopController {

    private final ShopProperties shop;

    public ShopController(ShopProperties shop) {
        this.shop = shop;
    }

    @GetMapping("/info")
    public ShopProperties info() {
        return shop;
    }
}
Also uses _curl.sh
curl localhost:8080/info

YAML tips

Use spaces, never tabs, for indentation. Two spaces per level is the usual style. Indentation matters, so one wrong space changes the meaning. Your editor can check the file for you.

keyvalue
Indentspaces, never tabs
Style2 spaces per level
Carefulindentation is meaning
Helpyour editor checks it

Summary

YAML holds the same settings, with indented levels, and dashes for lists. Use spaces, never tabs. Next, profiles.

Key takeaways
  • application.yml
  • Indented levels
  • - lists
Next up: Profiles — settings per environment