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.
@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(
String host,
int port,
String from
) {}@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.
app:
mail:
host: smtp.internal.example.com
port: 587
from: no-reply@example.comActivate a profile at runtime:
java -jar app.jar --spring.profiles.active=prodPrecedence 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.
spring:
datasource:
password: ${DB_PASSWORD}With type-safe properties and profiles, one build promotes cleanly from your laptop to production.