Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
The application.yml
(or its equivalent application.yaml
) file in Spring Boot is an alternative to the traditional application.properties
file for configuration purposes. YAML, which stands for "YAML Ain't Markup Language," is a human-readable data serialization format. Many developers prefer YAML over properties format because of its clear hierarchical structure, which is particularly useful when dealing with complex configurations.
In a Spring Boot application, instead of creating an application.properties
file, you can create an application.yml
file in the src/main/resources
directory.
Here are some common configurations in YAML format:
server: port: 8081
spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate: ddl-auto: update
logging: level: org.springframework: INFO com.example: DEBUG
app: custom: property: value
Just like with application.properties
, you can access these properties in your Spring Beans using the @Value
annotation or by binding them to configuration classes with @ConfigurationProperties
.
Spring Boot also supports profile-specific configurations in YAML. There are a couple of ways to define profile-specific configurations within the application.yml
:
server: port: 8080 --- spring: profiles: dev server: port: 8081 --- spring: profiles: prod server: port: 8082
server: port: 8080 spring: profiles: active: dev --- spring: profiles: dev server: port: 8081 --- spring: profiles: prod server: port: 8082
If you want to include another YAML file's properties, you can use the spring.config.import
property:
spring: config: import: classpath:another-config.yml
Hierarchical Representation: Unlike properties files, YAML files naturally represent hierarchical configurations, making them more readable.
Support for Complex Data Structures: It's easier to represent arrays, lists, and maps in YAML.
Profile-specific Configurations: As demonstrated above, it's convenient to have multiple profile configurations in one file separated by document breaks (---
).
Both application.properties
and application.yml
have their places in Spring Boot applications. Your choice depends on your preference and the complexity of the configurations. However, as configurations grow, the hierarchical format of YAML can be more readable and easier to manage than the flat properties format.
Using YAML configuration in Spring Boot:
# YAML configuration example server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/mydatabase username: root password: password
Common properties in Spring Boot application.yml:
# Common properties in application.yml server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/mydatabase username: root password: password
Profiles and application.yml in Spring Boot:
# Default properties server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/defaultdb # Properties for the development profile --- spring: profiles: dev datasource: url: jdbc:mysql://localhost:3306/devdb
Nesting and structuring YAML in Spring Boot:
# Nested and structured YAML configuration myapp: server: port: 8080 database: url: jdbc:mysql://localhost:3306/mydatabase username: root password: password
Spring Boot YAML array configuration:
# YAML array configuration fruits: - apple - orange - banana
Conditional properties in application.yml:
# Conditional YAML configuration server: port: 8080 spring: profiles: active: dev
Overriding default settings with application.yml:
# Override default server port server: port: 9090
Security considerations for application.yml in Spring Boot:
# Encrypted property db: password: ENC(encrypted_password)