In Spring Boot applications, configuration is used to control application behavior without changing the source code. Along with application.properties, Spring Boot also supports application.yml or application.yaml for defining configuration settings.
- YAML configuration files allow developers to organize settings in a hierarchical and readable format.
- They are commonly used to configure properties such as server settings, database connections, logging levels, and microservice configurations.
Why Use application.yml?
- Provides a structured and hierarchical configuration format.
- More readable for complex configurations.
- Reduces repetition compared to application.properties.
- Widely used in microservices and cloud-based Spring Boot applications.
Common Configuration Examples
1. Server Configuration
We can change the default Spring Boot server port using the server.port property.
- Changes the application's running port.
- Default Spring Boot port is 8080.

2. Defining the Application Name
We can set a custom name for the Spring Boot application using the spring.application.name property.
- Assigns a unique application name.
- Useful in microservices architecture.

3. Database Configuration
Database connection properties are defined under the spring.datasource section.
- Stores database URL, username, and password.
- Supports multiple databases.
For MySQL Database

For H2 Database
H2 Database is an in-memory database commonly used for development and testing.
- Mainly used for development and testing.
- No separate database installation required.

For MongoDB Database
To connect with MongoDB, we define properties under spring.data.mongodb.
- Configuration placed under spring.data.mongodb.
- Commonly used in modern microservices.

4. Connecting with an Eureka Server
Netflix Eureka acts as a service registry where microservices register themselves. Enables service discovery.
- Allows dynamic communication between services.
- Commonly used in Spring Cloud microservices.

Advantages of application.yml
- More readable and organized.
- Supports nested configurations.
- Reduces duplication of property names.
- Easier maintenance for large projects.
- Well-suited for microservices architectures.
- Simplifies complex configuration management.
application.yml vs application.properties
| Feature | application.properties | application.yml |
|---|---|---|
| Format | Key-value pairs | Hierarchical |
| Readability | Less readable for large configs | More readable |
| Structure | Flat structure | Nested structure |
| Maintenance | Difficult for large files | Easier to maintain |
| Usage | Simple configuration | Complex configuration |
| Repetition | More repetitive | Less repetitive |