application.properties / YAML
BeginnerExternalise configuration via key-value properties or structured YAML; Spring Boot resolves values from multiple sources following a strict precedence order.
Overview
Spring Boot externalises configuration through a layered property source hierarchy — properties defined later in the list override earlier ones. The full precedence order (highest first) is: command-line args → environment variables → application-{profile}.properties → application.properties → @PropertySource → default values. @ConfigurationProperties binds a prefix of properties to a typed Java bean — cleaner than scattered @Value annotations for structured configuration. Profiles (spring.profiles.active) allow environment-specific property files (application-dev.properties, application-prod.properties) to activate selectively without code changes.
Property sources and precedence
Multiple sources are merged; later sources override earlier ones. Understanding the order prevents surprising configuration in production.
# application.properties (lowest priority in the set)
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=app
# Overridden by application-prod.properties (active profile)
# application-prod.properties
server.port=80
spring.datasource.url=jdbc:mysql://prod-db:3306/mydb
# Overridden by environment variable (higher priority)
# SPRING_DATASOURCE_URL=jdbc:mysql://rds-host:3306/mydb
# Overridden by command-line arg (highest priority)
# java -jar app.jar --spring.datasource.url=jdbc:mysql://override:3306/mydb
# Activate profile
spring.profiles.active=prod
# Or: SPRING_PROFILES_ACTIVE=prod (env var)
# Or: java -jar app.jar --spring.profiles.active=prod (CLI)
# YAML equivalent (application.yml)
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: app
profiles:
active: dev@ConfigurationProperties for typed configuration
@ConfigurationProperties binds a property prefix to a Java bean with validation support — far cleaner than @Value for grouped settings.
# application.properties
payment.gateway.url=https://api.stripe.com
payment.gateway.api-key=${STRIPE_API_KEY}
payment.gateway.timeout-seconds=30
payment.gateway.retry-count=3
payment.gateway.supported-currencies=USD,EUR,GBP
@ConfigurationProperties(prefix = "payment.gateway")
@Validated // enables @NotBlank, @Min etc on fields
@Data
public class PaymentGatewayProperties {
@NotBlank
private String url;
@NotBlank
private String apiKey;
@Min(1) @Max(120)
private int timeoutSeconds = 30;
private int retryCount = 3;
private List<String> supportedCurrencies = List.of("USD");
}
@Configuration
@EnableConfigurationProperties(PaymentGatewayProperties.class)
public class PaymentConfig {
@Bean
public PaymentGatewayClient client(PaymentGatewayProperties props) {
return new PaymentGatewayClient(props.getUrl(),
props.getApiKey(),
Duration.ofSeconds(props.getTimeoutSeconds()));
}
}Profiles and profile-specific properties
Profiles activate environment-specific configuration files. Spring Boot 2.4+ also supports profile groups and import-based config.
# File layout
# src/main/resources/
# application.properties ← shared base
# application-dev.properties ← local development
# application-test.properties ← test slice config
# application-prod.properties ← production (never commit secrets)
# application-dev.properties
spring.datasource.url=jdbc:h2:mem:devdb
spring.jpa.show-sql=true
logging.level.com.example=DEBUG
# application-prod.properties
spring.datasource.url=${DB_URL} # injected at runtime
spring.datasource.password=${DB_PASSWORD}
logging.level.com.example=WARN
# Profile groups (Spring Boot 2.4+): activate multiple at once
spring.profiles.group.production=prod,metrics,security
# Test-specific: @ActiveProfiles("test") in test class
@SpringBootTest
@ActiveProfiles("test")
class ServiceTest { ... }
# Check active profile in code
@Component
public class FeatureFlag {
@Value("${spring.profiles.active:default}")
private String activeProfile;
public boolean isProd() {
return "prod".equals(activeProfile);
}
}Key Points to Remember
- 1Property source precedence (highest first): CLI args → env vars → profile properties → base properties.
- 2@ConfigurationProperties binds a prefix to a typed bean — prefer over scattered @Value for grouped config.
- 3@Validated on @ConfigurationProperties enables JSR-303 constraint checking at startup.
- 4Never commit secrets to application.properties — inject via environment variables or a secrets manager.
- 5spring.profiles.active selects environment-specific files; profile groups (Spring Boot 2.4+) activate multiple at once.
- 6Environment variables override properties: SPRING_DATASOURCE_URL overrides spring.datasource.url.
Interview Questions
Sign in to ask AriaWhat is the property source precedence order in Spring Boot?
What advantage does @ConfigurationProperties have over @Value?
How do Spring Boot profiles work and how do you activate a profile at runtime?
How would you inject a secret from AWS Secrets Manager into a Spring Boot application without committing it to application.properties?
What is the difference between spring.config.import and spring.profiles.include?
Ask Aria about application.properties / YAML
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.