Spring Boot load property từ nhiều nguồn, nguồn sau override nguồn trước (cao nhất → thấp nhất):
1. Command-line args (--server.port=9090) — cao nhất
2. SPRING_APPLICATION_JSON env var
3. Env vars (SERVER_PORT=9090) — relaxed binding
4. application-{profile}.properties bên ngoài JAR
5. application.properties bên ngoài JAR
6. @PropertySource annotation
7. application-{profile}.properties trong JAR
8. application.properties trong JAR
9. @Value default — thấp nhất
# Ví dụ override khi deploy:
java -jar app.jar --spring.datasource.url=jdbc:postgresql://prod-db/myapp
# Hoặc env var (Docker/K8s):
SPRING_DATASOURCE_URL=jdbc:postgresql://prod-db/myapp java -jar app.jarRelaxed binding: env var SPRING_DATASOURCE_URL = property spring.datasource.url — Spring tự convert _ → ., uppercase → lowercase.
Profile-specific luôn override base: application-prod.yml ghi đè application.yml khi profile=prod.
Thực tế: base config trong JAR (application.yml) + secrets/env-specific qua env var → không commit secret vào git.
Spring Boot loads properties from multiple sources, each overriding the previous (highest → lowest priority):
1. Command-line args (--server.port=9090) — highest
2. SPRING_APPLICATION_JSON env var
3. Env vars (SERVER_PORT=9090) — relaxed binding
4. application-{profile}.properties outside the JAR
5. application.properties outside the JAR
6. @PropertySource annotation
7. application-{profile}.properties inside the JAR
8. application.properties inside the JAR
9. @Value defaults — lowest
# Override at deploy time:
java -jar app.jar --spring.datasource.url=jdbc:postgresql://prod-db/myapp
# Or via env var (Docker/K8s):
SPRING_DATASOURCE_URL=jdbc:postgresql://prod-db/myapp java -jar app.jarRelaxed binding: env var SPRING_DATASOURCE_URL maps to property spring.datasource.url — Spring converts _ → . and uppercase → lowercase automatically.
Profile-specific always wins over base: application-prod.yml overrides application.yml when profile=prod.
In practice: base config inside JAR (application.yml) + secrets/env-specific via env vars → no secrets committed to git.