Application Properties and Profiles
Externalize configuration with application.properties/yml, define profiles, and inject values with @Value and @ConfigurationProperties.
application.properties vs application.yml
Spring Boot reads configuration from src/main/resources/application.properties or application.yml. YAML supports hierarchical config without key repetition; both are equivalent in features.
# application.properties:
server.port=8080
spring.datasource.url=jdbc:postgresql://localhost/mydb
spring.jpa.hibernate.ddl-auto=validate
# Equivalent application.yml:
server:
port: 8080
spring:
datasource:
url: jdbc:postgresql://localhost/mydb
jpa:
hibernate:
ddl-auto: validateInjecting Values with @Value
Use @Value("${property.key}") to inject a single property. Add a default with ${key:default}. SpEL expressions are supported.
@Service
public class PricingService {
@Value("${pricing.vat-rate:0.20}")
private double vatRate;
@Value("${app.name}")
private String appName;
}All lessons in this course
- Auto-Configuration and Spring Boot Starters
- Application Properties and Profiles
- Bean Wiring: @Component, @Service, @Repository
- Constructor Injection and Circular Dependencies