Spring Boot Properties: application.properties vs application.yml

Spring Boot lets you configure your app without touching code by using configuration files. These files live in the src/main/resources folder and help control:

  • Server port
  • Database settings
  • Logging levels
  • Custom application properties

The two most commonly used Spring Boot Properties formats are:

  • application.properties
  • application.yml

Let’s explore how they differ and how to use them effectively.

Spring Boot Properties: application.properties vs application.yml

πŸ“„ What is application.properties?

A simple key-value file format used for defining configuration.

πŸ”§ Example:

βœ… Pros:

  • Simple to read/write for small apps
  • Easy to grep or parse
  • Familiar for traditional Java devs

πŸ“ What is application.yml?

YAML (YAML Ain’t Markup Language) is a structured, hierarchical configuration format.

πŸ”§ Same Example in YAML:

βœ… Pros:

  • Better for complex/nested properties
  • More readable for large configs
  • Preferred in Kubernetes and cloud-native apps

πŸ”€ Key Differences

Featureapplication.propertiesapplication.yml
FormatKey-ValueHierarchical (YAML)
ReadabilityGood for small configsBetter for structured configs
Multi-profile supportSlightly verboseCleaner with nesting
Error sensitivityFewer formatting errorsSensitive to indentation
Community trendOlder Spring projectsIncreasingly popular

🎯 When to Use Which?

  • Use application.properties if:
    • You prefer key-value style
    • Your configs are flat and simple
    • You’re maintaining legacy code
  • Use application.yml if:
    • You need nested properties
    • You use multiple profiles
    • You want cleaner config for larger apps

βœ… Both formats are fully supported by Spring Boot β€” it’s a matter of preference.

πŸ‘₯ Multi-Profile Support

You can create profile-specific files like:

  • application-dev.properties
  • application-prod.yml

Activate a profile via:

Or use --spring.profiles.active=prod when starting your app.

πŸ” Accessing Custom Properties

Example:

You can bind this to a class:

Make sure you add:

🚫 Common Mistakes to Avoid

MistakeProblem
Indentation error in YAMLCauses app to fail silently or misread properties
Mixing application.yml and .properties unintentionallyMay lead to confusion if overlapping values
Not setting active profileSpring uses default profile, might load wrong config
Hardcoding sensitive valuesUse environment variables or vaults for secrets

βœ… Summary

TopicKey Takeaway
Properties vs YAMLChoose based on readability and structure
ProfilesUse application-{profile}.yml/properties
Accessing custom valuesUse @ConfigurationProperties
SecurityAvoid storing secrets directly

Whether you choose .properties or .yml, Spring Boot Properties gives you the power to tweak your app without touching the code.

πŸ“˜ External References