← All articles
Backend1 min read

Spring Boot Configuration and Profiles

Externalized config, type-safe properties, and environment profiles — how to manage settings across dev, staging, and production.

S

Swapnika Voora

Author

Hardcoding configuration is a fast path to production incidents. Spring Boot's externalized configuration lets the same artifact run in every environment by reading settings from properties, YAML, environment variables, and more.

Type-safe configuration properties

Instead of scattering @Value annotations, bind related settings to a record with @ConfigurationProperties.

MailProperties.java
@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(
    String host,
    int port,
    String from
) {}
AppConfig.java
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class AppConfig {
}

Profile-specific files

Spring loads application.yml first, then overlays a profile-specific file when a profile is active.

application-prod.yml
app:
  mail:
    host: smtp.internal.example.com
    port: 587
    from: no-reply@example.com

Activate a profile at runtime:

run.sh
java -jar app.jar --spring.profiles.active=prod

Precedence rules

Configuration sources have a defined order. Later sources override earlier ones. The most useful ones to remember:

  • Command-line arguments (highest).
  • Environment variables.
  • Profile-specific properties.
  • Base application.yml (lowest).

Keep secrets out of files

Never commit credentials. Inject them through environment variables or a secrets manager, and reference them in YAML with placeholders.

application.yml
spring:
  datasource:
    password: ${DB_PASSWORD}

With type-safe properties and profiles, one build promotes cleanly from your laptop to production.

#java#spring-boot#configuration#devops

More in Backend