Profiles cho phép có nhiều config khác nhau cho từng môi trường (dev, staging, prod) — deploy 1 JAR cho tất cả.
Tạo profile-specific config:
application.yml ← base config
application-dev.yml ← merge/override khi profile=dev
application-prod.yml ← merge/override khi profile=prodActivate:
bash
SPRING_PROFILES_ACTIVE=prod java -jar app.jarBean theo profile:
java
@Component
@Profile("dev")
class MockMailSender implements MailSender {}
@Component
@Profile("prod")
class SmtpMailSender implements MailSender {}
@Profile("!prod") // không phải prod
class DevOnlyFilter {}Profile groups (Spring Boot 2.4+):
yaml
spring.profiles.group.production: prod, monitoring, securityLưu ý: secrets (DB password, API key) không để trong application-prod.yml trong repo → dùng env var hoặc secret manager.
Profiles allow different configurations per environment (dev, staging, prod) — deploy one JAR everywhere.
Profile-specific config:
application.yml ← base config
application-dev.yml ← merged when profile=dev
application-prod.yml ← merged when profile=prodActivating:
bash
SPRING_PROFILES_ACTIVE=prod java -jar app.jarProfile-specific beans:
java
@Component
@Profile("dev")
class MockMailSender implements MailSender {}
@Component
@Profile("prod")
class SmtpMailSender implements MailSender {}
@Profile("!prod") // not prod
class DevOnlyFilter {}Profile groups (Spring Boot 2.4+):
yaml
spring.profiles.group.production: prod, monitoring, securityNote: secrets should not go in application-prod.yml in the repo → use env vars or a secret manager.