DEV Community

Cover image for Learn about the priority levels of config files and distinguish between .yaml and .properties files in Java Spring Boot.
Quang Hieu (Bee)
Quang Hieu (Bee)

Posted on

1

Learn about the priority levels of config files and distinguish between .yaml and .properties files in Java Spring Boot.

Hello everyone! Today, I will guide you through the priority levels of configuration files in a Java Spring Boot project and the differences between .yaml and .properties files. Let’s dive in!

1. Priority Levels of Configuration Files

In Spring Boot, configuration files can be placed in various locations, each with its own priority level. The common structure of configuration files in a project is as follows:

Image description

The structure of configuration files in the project will be arranged in the following order of priority:

Image description

2. Checking the Priority Levels

To test if the priority levels work as described, I changed the port in several configuration files:

  • File 1: port 8080

Image description

  • File 2: port 8081

Image description

  • File 3: port 8082

Image description

  • File 4: port 8083

Image description

After running the application, the port that was set is 8080.

Image description

Then, I deleted the outermost configuration file and ran the application again. The result showed the port as 8081.

Image description

Similarly, you can test with the remaining configuration files.

Isn’t it amazing? From these examples, we can clearly distinguish the priority of configuration files in a project.

3. Differentiating .properties and .yaml Files
In Java Spring Boot, both .properties and .yaml files are used to configure the application. However, they have some differences:

  • .properties
    • Simple "key-value" structure.
    • Stores data as strings. For complex data types, additional processing is required.
    • Example:
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
Enter fullscreen mode Exit fullscreen mode
  • .yaml:
    • Hierarchical structure, making it easy to read and organize complex information.
    • Supports multiple data types (strings, numbers, arrays, objects, booleans), making the configuration more flexible.
    • Example
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
Enter fullscreen mode Exit fullscreen mode

4. Priority Levels When Both .properties and .yaml Files Exist

Let’s say we have both .properties and .yaml files in the project. Let’s check the priority level:

File .properties: port 8083

Image description

File .yaml: port 8084

Image description

After running the application, the port that was set is 8083.

Image description

Conclusion

From the examples above, you should now have a clearer understanding of the priority levels of configuration files in Spring Boot and the differences between .properties and .yaml. I hope this article helps you gain more useful knowledge. If you found it helpful, don’t forget to leave me a reaction!

If you found this article useful and interesting, please share it with your friends and family. I hope you found it helpful. Thanks for reading 🙏

Let's get connected! You can find me on:

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay