Understanding the Nuances: .yml vs .properties Files in Spring Boot

Naveen Metta
4 min readNov 30, 2023

--

Introduction

In the realm of Spring Boot configuration, developers often encounter the choice between .yml and .properties files. Both formats serve the purpose of externalizing configuration, but they differ in syntax and features. In this comprehensive article, we will delve into the distinctions between these two file formats and provide extensive code examples to illustrate their usage in a Spring Boot application.

YAML (.yml) Files

YAML, which stands for “YAML Ain’t Markup Language” or sometimes “Yet Another Markup Language,” is a human-readable data serialization format. In the context of Spring Boot, .yml files are commonly used for configuration due to their simplicity and readability.

Example 1: Basic .yml Configuration
Let’s start with a basic example of a .yml configuration file:

# application.yml

server:
port: 8080

spring:
datasource:
url: jdbc:mysql://localhost:3306/mydatabase
username: root
password: secret
jpa:
hibernate:
ddl-auto: update
show-sql: true

In this example, we define server properties for the embedded Tomcat server and configure a MySQL datasource for a Spring Data JPA application. The structure is hierarchical and uses indentation to represent relationships between properties.

Example 2: List and Map in .yml
YAML supports the representation of lists and maps, making it easy to configure complex structures:

# application.yml

app:
users:
- username: john
role: user
- username: admin
role: admin
settings:
max-connections: 100
timeout:
read: 5000
connect: 5000

Here, we define a list of users and a map of application settings. The indentation reflects the structure of the data.

Example 3: Advanced .yml Features
YAML offers advanced features that enhance configuration readability and maintainability. One such feature is the ability to use anchors and aliases, enabling the reuse of common configuration snippets.

# application.yml

common-config: &common
max-retries: 3
timeout: 5000

app-1:
<<: *common
api-url: https://api.app1.com

app-2:
<<: *common
api-url: https://api.app2.com

In this example, the common-config anchor defines common properties, and <<: *common is used to include those properties in app-1 and app-2 without duplicating the code. This promotes consistency and reduces the chances of configuration errors.

Example 4: Multi-document .yml
Another powerful feature of YAML is the ability to define multiple documents in a single file, allowing for modular and organized configuration.

# application.yml

---
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydatabase
username: root
password: secret
---
server:
port: 8080

Here, we have two separate documents — one for the datasource configuration and another for the server configuration. This modular approach can enhance maintainability in large projects.

Properties (.properties) Files

.properties files are a traditional configuration format using a key-value pair structure. While they lack the hierarchical organization of .yml files, they are widely used and understood, especially in legacy systems.

Example 5: Basic .properties Configuration
Let’s recreate the first example using a .properties file:

# application.properties

server.port=8080

spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

In this .properties file, each property is represented by a key-value pair. The structure is flat, and properties are organized using dots to denote hierarchy.

Example 6: Conditional Configuration in .properties
.properties files support conditional configuration based on profiles. Profiles allow developers to customize the application’s behavior for different environments.

# application-dev.properties

spring.datasource.url=jdbc:mysql://localhost:3306/dev_database

In this example, the application-dev.properties file overrides the spring.datasource.url property for the development profile.

Example 7: Placeholder Substitution in .properties
.properties files support placeholder substitution, allowing developers to externalize configuration values and inject them dynamically.

# application.properties

app.base-url=https://myapp.com
app.api-endpoint=${app.base-url}/api/data

Here, the app.api-endpoint property dynamically incorporates the value of app.base-url.

Example 8: Properties File with Comments
.properties files support comments, providing a way for developers to document configurations and add context.

# application.properties

# Server Configuration
server.port=8080

# DataSource Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=secret

# JPA Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Choosing Between .yml and .properties

The choice between .yml and .properties depends on factors like readability, ease of maintenance, and personal preference. YAML is often favored for its human-readable syntax and support for complex data structures, while .properties files are more concise and familiar to developers with a background in Java.

When to Use .yml
Readability and Conciseness: .yml files are generally more readable and concise, making them a good choice for simple to moderately complex configurations.

Nested Structures: If your configuration involves nested structures or complex hierarchies, .yml is a more natural fit due to its support for indentation.

Advanced Features: For projects that require advanced features like anchors and aliases, .yml provides more flexibility and maintainability.

Multi-document Support: When dealing with modular configuration, .yml’s ability to support multiple documents in a single file can enhance organization and maintainability.

When to Use .properties
Legacy Systems: In scenarios where compatibility with older systems or tools that expect .properties files is crucial, opting for .properties files may be more practical.

Flat Configuration: For straightforward configurations without complex nesting, .properties files offer a flat structure that is easy to understand.

Conditional Configuration: If your project requires conditional configuration based on profiles, .properties files support this feature seamlessly.

Placeholder Substitution: .properties files are well-suited for placeholder substitution, allowing dynamic injection of values.

Comments for Documentation: If documentation is a priority, .properties files support comments, providing context and explanation for each configuration.

Conclusion

In the Spring Boot ecosystem, the choice between .yml and .properties files is a matter of preference and project requirements. Both formats are capable of achieving the same goal — externalizing configuration — and developers should choose the one that aligns with their team’s expertise and the readability needs of their application.

In this article, we explored the fundamental differences between .yml and .properties files through various code examples. Whether you opt for the clarity of YAML or the simplicity of properties, Spring Boot provides seamless support for both, ensuring flexibility in configuring your applications.

--

--

Naveen Metta

I'm a Full Stack Developer with 2.5 years of experience. feel free to reach out for any help : mettanaveen701@gmail.com