Core Syntax
YAML configuration provides a hierarchical structure for Spring Boot applications. Proper indentation and spacing are essential:
# Server configuration
server:
port: 8081
address: 0.0.0.0
# Database setup
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://dbhost:3306/app_db
username: admin
password: securepass
# Custom object mapping
employee:
firstName: John
lastName: Doe
position: Developer
experienceYears: 5
# Collection definition
programmingLanguages:
- Java
- Python
- JavaScript
Advanced Configuration
YAML supports complex configurations beyond basic properties:
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/prod_db
username: dbadmin
password: strongpassword
servlet:
multipart:
max-file-size: 15MB
max-request-size: 150MB
web:
resources:
static-locations: file:/var/www/images/
# MyBatis settings
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
# Cloud storage configuration
cloud:
storage:
endpoint: https://storage.example.com/api
access-key: placeholder-access-key
secret-key: placeholder-secret-key
container: app-container
Properties File Equivalent
Traditional properties format compared to YAML:
File upload limits
spring.servlet.multipart.max-file-size=15MB spring.servlet.multipart.max-request-size=150MB
Static resources
spring.web.resources.static-locations=file:/var/www/images/
MyBatis configuration
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis.configuration.map-underscore-to-camel-case=true
Cloud storage
cloud.storage.endpoint=https://storage.example.com/api cloud.storage.access-key=placeholder-access-key cloud.storage.secret-key=placeholder-secret-key cloud.storage.container=app-container
</details>