Repaso
Español
Spring, JPA & Java WebIntermediateSPR-10

Where do you usually put settings such as the database URL in Spring Boot, and how do you switch between environments?

Options

  1. Hard-coded in the Java classes, changed by recompiling
  2. In application.yml, with spring.profiles.active (Correct option)
  3. Only in pom.xml, inside the dependencies section
  4. 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 variable

Remember

Config in application.yml, per-environment via profiles, secrets via env vars.

Key terms

Practice this question in Repaso