Why this matters
- Hardcoding database URLs or API keys in Java forces recompilation for every environment change.
- Profiles activate bean sets and property files per environment —
devuses H2,produses PostgreSQL. @ConfigurationPropertiesbinds typed configuration objects, replacing scattered@Valueinjections.
application.yml hierarchy
L1 · FresherProperties in application.yml and profile files (application-dev.yml) externalize config. BookStore URLs, pool sizes, and feature flags belong here — not hard-coded in Java.
Profiles for environments
L2 · IntermediateActivate with spring.profiles.active=dev. Beans annotated @Profile("prod") only load in production — ideal for mock payment clients vs real ones.
@ConfigurationProperties binding
L3 · AdvancedBind a prefix like bookstore.catalog to a typed record or class with validation. Safer than dozens of @Value fields and easier to test without the context.
Property source precedence
L4 · ExpertCommand-line args, OS env vars, and config server overrides beat local YAML. Knowing order explains "it works locally but not in K8s" incidents.
application.yml structure
spring:
application:
name: bookstore-api
datasource:
url: jdbc:postgresql://127.0.0.1:5432/bookstore
username: bookstore
password: ${DB_PASSWORD:localdev}
server:
port: 8080
bookstore:
pricing:
service-url: https://pricing.internal.example.com
timeout-ms: 3000
catalog:
page-size: 20
Spring binds spring.* to built-in properties and custom bookstore.* keys to your @ConfigurationProperties class.
Typed configuration properties
// learn:fragment
@ConfigurationProperties(prefix = "bookstore.pricing")
public record PricingProperties(
String serviceUrl,
int timeoutMs
) {}
@Configuration
@EnableConfigurationProperties(PricingProperties.class)
public class BookStoreConfig { }
Injection into services:
// learn:fragment
@Service
public class PricingService {
private final RestClient client;
public PricingService(PricingProperties props) {
this.client = RestClient.builder()
.baseUrl(props.serviceUrl())
.build();
}
}
Records make immutable config objects with validation support via @Validated and @NotBlank.
Profiles
Activate environment-specific configuration:
# application-dev.yml
spring:
datasource:
url: jdbc:h2:mem:bookstore
h2:
console:
enabled: true
logging:
level:
com.example.bookstore: DEBUG
# application-prod.yml
spring:
datasource:
url: jdbc:postgresql://${DB_HOST}:5432/bookstore
Activate with:
java -jar bookstore-api.jar --spring.profiles.active=prod
Or in application.yml:
spring:
profiles:
active: dev
Profile strategies
application-{profile}.yml— Property overrides per profile.@Profile("dev")on beans — Register beans only in matching environments.spring.profiles.active— Comma-separated list; later profiles override earlier ones.
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 6 sections
- Profile-specific beans
- @Value for simple cases
- Property precedence
- Relaxed binding
- Validation on startup
- Beans initialised from configuration
Test yourself
Answer these before moving on — recall is what makes it stick.