Where do you usually put settings such as the database URL in Spring Boot, and how do you switch between environments?
Options
- Hard-coded in the Java classes, changed by recompiling
- In
application.yml, withspring.profiles.active(Correct option) - Only in
pom.xml, inside the dependencies section - In the browser, sent along with each user request
Answer
In application.properties or application.yml, with profiles (application-dev.yml, application-prod.yml) activated by spring.profiles.active
Why
Spring Boot reads configuration from application.properties or application.yml in src/main/resources.
For different environments you create profile files like application-dev.yml and application-prod.yml and activate one with spring.profiles.active=prod (or the SPRING_PROFILES_ACTIVE environment variable).
Secrets (passwords, API keys) should not be committed: override them with environment variables or a secrets manager.
spring:
datasource:
url: jdbc:postgresql://localhost:5432/app
username: app
password: ${DB_PASSWORD} # from an environment variableRemember
Config in application.yml, per-environment via profiles, secrets via env vars.