YAML Configuration
How to Activate Profiles
Section titled “How to Activate Profiles”From application.yml
Section titled “From application.yml”spring: profiles: active: devFrom CLI
Section titled “From CLI”java -jar app.jar --spring.profiles.active=prodFrom environment variable
Section titled “From environment variable”export SPRING_PROFILES_ACTIVE=testFrom VSCode run config launch.json
Section titled “From VSCode run config launch.json”"args": "--spring.profiles.active=dev",From IDE run config
Section titled “From IDE run config”-Dspring.profiles.active=devSpring Boot YAML basic profile setup
Section titled “Spring Boot YAML basic profile setup”- A common base config (in
application.yml) - Environment-specific profiles (
application-dev.yml,application-test.yml,application-prod.yml) - Each profile extends the common configuration using
spring.config.import
application.yml
Section titled “application.yml”# application.ymlspring: application: name: sample-app
datasource: url: jdbc:mysql://localhost:3306/common_db username: common_user password: common_pass driver-class-name: com.mysql.cj.jdbc.Driver
server: port: 8080
logging: level: root: INFO com.example: INFOapplication-dev.yml
Section titled “application-dev.yml”# application-dev.yml
spring: config: import: application.yml
datasource: url: jdbc:mysql://localhost:3306/dev_db username: dev_user password: dev_pass
server: port: 8081
logging: level: com.example: DEBUGapplication-test.yml
Section titled “application-test.yml”# application-test.yml
spring: config: import: application.yml
spring: datasource: url: jdbc:h2:mem:test_db username: sa password: driver-class-name: org.h2.Driver
server: port: 8082
logging: level: com.example: TRACEapplication-prod.yml
Section titled “application-prod.yml”# application-prod.yml
spring: config: import: application.yml
spring: datasource: url: jdbc:mysql://prod-db:3306/prod_db username: ${DB_USER} password: ${DB_PASS}
server: port: 80
logging: level: root: WARN com.example: INFOSpring Boot YAML advance profile setup
Section titled “Spring Boot YAML advance profile setup”the complete, extended Spring Boot YAML configuration setup
- Common config
- Dev, Test, Production profiles
- Property-map merging examples
- Multiple
importhierarchy - Secrets isolation (
application-secrets.yml) - Full microservice-style config structure
application.yml
Section titled “application.yml”# application.ymlspring: application: name: sample-app
profiles: active: dev # default profile (can be overridden)
datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: common_user password: common_pass url: jdbc:mysql://localhost:3306/common_db
server: port: 8080
logging: level: root: INFO com.sample: INFO
app: name: Sample Application features: cache: false metrics: true
# Import secrets (not committed to Git)spring: config: import: optional:application-secrets.ymlapplication-dev.yml
Section titled “application-dev.yml”# application-dev.ymlspring: config: import: - application.yml - optional:application-secrets.yml
spring: datasource: url: jdbc:mysql://localhost:3306/dev_db username: dev_user password: dev_pass
server: port: 8081
logging: level: com.sample: DEBUG
app: features: cache: false # override base debug-mode: trueapplication-test.yml
Section titled “application-test.yml”# application-test.ymlspring: config: import: - application.yml
spring: datasource: url: jdbc:h2:mem:test_db username: sa password: driver-class-name: org.h2.Driver
server: port: 8082
logging: level: com.sample: TRACE
app: features: cache: false metrics: false # override for testapplication-prod.yml
Section titled “application-prod.yml”# application-prod.ymlspring: config: import: - application.yml - optional:application-secrets.yml
spring: datasource: url: jdbc:mysql://prod-db:3306/prod_db username: ${DB_USER} password: ${DB_PASS}
server: port: 80
logging: level: root: WARN com.sample: INFO
app: features: cache: true metrics: true audit: trueapplication-secrets.yml
Section titled “application-secrets.yml”⚠ Do NOT commit this file to Git. Put it in
.gitignore.
# application-secrets.ymlspring: datasource: username: ${SECRET_DB_USER} password: ${SECRET_DB_PASS}
jwt: secret: ${JWT_SECRET_KEY}
api: key: ${EXTERNAL_API_KEY}Multiple Import Hierarchy Example
Section titled “Multiple Import Hierarchy Example”A profile can import multiple layers:
application.yml → application-common.yml → application-dev.ymlapplication.yml
Section titled “application.yml”spring: config: import: - application-common.yml - optional:application-secrets.ymlapplication-common.yml:
Section titled “application-common.yml:”server: forward-headers-strategy: framework error: include-stacktrace: on_paramapplication-dev.yml:
Section titled “application-dev.yml:”spring: config: import: - application.yml - optional:application-secrets.yml
debug: trueProperty Map Merging Example
Section titled “Property Map Merging Example”Base file:
app: mail: host: smtp.example.com ports: - 587 - 465Dev override (adds new port, merges list):
app: mail: ports: - 2525Merged output during runtime:
app.mail.ports = [587, 465, 2525]Microservice-Friendly Structure (Recommended)
Section titled “Microservice-Friendly Structure (Recommended)”config/ ├── application.yml ├── application-common.yml ├── application-dev.yml ├── application-test.yml ├── application-prod.yml ├── application-secrets.yml └── application-monitoring.ymlExample application-monitoring.yml:
management: endpoints: web: exposure: include: health,info,prometheus metrics: export: prometheus: enabled: trueImported from prod:
spring: config: import: - application.yml - application-monitoring.yml - optional:application-secrets.ymlSpring Boot YAML cloud profile setup
Section titled “Spring Boot YAML cloud profile setup”complete version of the Spring Boot configuration using Spring Cloud Config Server,
- Config Server setup
- Externalized YAML files in a Git repo
- Client-side bootstrap config
- Profiles (dev, test, prod)
- Shared/common config
- Secrets file
- Hierarchical imports
✔ Only configuration files ✔ No Java code
Config Server
Section titled “Config Server”This file lives in the Config Server JAR (not the client apps).
# Config Server: application.ymlserver: port: 8888
spring: application: name: config-server
cloud: config: server: git: uri: https://github.com/your-org/your-config-repo.git clone-on-start: true search-paths: - configsFolder Structure in Git (Config Repo)
Section titled “Folder Structure in Git (Config Repo)”your-config-repo/└── configs/ ├── sample-app.yml ├── sample-app-dev.yml ├── sample-app-test.yml ├── sample-app-prod.yml ├── sample-app-secrets.yml ├── sample-app-common.yml └── sample-app-monitoring.ymlThese YAML files are fetched dynamically by Spring Cloud Config Clients.
Common Config
Section titled “Common Config”# sample-app.yml (default, shared for all profiles)spring: application: name: sample-app
datasource: url: jdbc:mysql://localhost:3306/common_db username: common_user password: common_pass driver-class-name: com.mysql.cj.jdbc.Driver
server: port: 8080
logging: level: root: INFO com.sample: INFO
app: features: metrics: true cache: false
spring: config: import: - optional:sample-app-common.yml - optional:sample-app-secrets.ymlCommon Shared Config
Section titled “Common Shared Config”# sample-app-common.ymlserver: forward-headers-strategy: framework
management: endpoints: web: exposure: include: health,infoSecrets File
Section titled “Secrets File”(Do NOT commit to public GitHub)
# sample-app-secrets.ymlspring: datasource: username: ${SECRET_DB_USER} password: ${SECRET_DB_PASS}
jwt: secret: ${JWT_SECRET_KEY}Dev Profile
Section titled “Dev Profile”# sample-app-dev.ymlspring: config: import: - sample-app.yml - optional:sample-app-secrets.yml
spring: datasource: url: jdbc:mysql://localhost:3306/dev_db username: dev_user password: dev_pass
server: port: 8081
logging: level: com.sample: DEBUG
app: features: debug-mode: true cache: falseTest Profile
Section titled “Test Profile”# sample-app-test.ymlspring: config: import: - sample-app.yml
spring: datasource: url: jdbc:h2:mem:test_db driver-class-name: org.h2.Driver username: sa password:
logging: level: com.sample: TRACE
server: port: 8082
app: features: metrics: falseProduction Profile
Section titled “Production Profile”# sample-app-prod.ymlspring: config: import: - sample-app.yml - optional:sample-app-secrets.yml - optional:sample-app-monitoring.yml
spring: datasource: url: jdbc:mysql://prod-db:3306/prod_db username: ${DB_USER} password: ${DB_PASS}
server: port: 80
logging: level: root: WARN com.sample: INFO
app: features: metrics: true audit: true cache: trueMonitoring Add-on File
Section titled “Monitoring Add-on File”# sample-app-monitoring.ymlmanagement: endpoints: web: exposure: include: health,info,prometheus
management: metrics: export: prometheus: enabled: trueClient App
Section titled “Client App”Every client application needs this file to load config from the Config Server.
# bootstrap.yml (client app)spring: application: name: sample-app
cloud: config: uri: http://localhost:8888 fail-fast: true retry: max-attempts: 5 initial-interval: 2000
# Optional: fallback local settingsspring: config: import: - optional:application-local.ymlClient App Profile Activation
Section titled “Client App Profile Activation”Clients still use local profile activation, but config loads from server.
# application.yml (client app)spring: profiles: active: devHow Each Client App Loads Config
Section titled “How Each Client App Loads Config”Example:
When profile = dev
Client fetches:
GET /sample-app/devConfig from:
sample-app.ymlsample-app-dev.ymlsample-app-common.ymlsample-app-secrets.yml- (optional) monitoring file