Spring Boot: From Basics to Production · Spring Core and Spring Boot Fundamentals
Configuration Properties and Profiles
Externalize configuration with application.yml, typed properties and profiles.
Spring Boot reads configuration from application.properties/application.yml, environment variables (SPRING_DATASOURCE_URL), command-line args and more, in a defined precedence order. Bind related settings to a @ConfigurationProperties record with validation instead of scattering @Value. Profiles (application-dev.yml, spring.profiles.active=prod) switch settings per environment. Keep secrets in environment variables or a secret manager.
@ConfigurationProperties(prefix = "shop.payments")
@Validated
public record PaymentProperties(
@NotBlank String apiKey,
@NotNull URI baseUrl,
@DurationUnit(ChronoUnit.SECONDS) Duration timeout) {}
@SpringBootApplication
@ConfigurationPropertiesScan
public class ShopApplication { /* ... */ }
# application.yml
shop:
payments:
base-url: https://api.stripe.com
timeout: 5
api-key: ${STRIPE_API_KEY}
---
spring:
config:
activate:
on-profile: dev
shop:
payments:
base-url: http://localhost:12111
Common pitfalls
- Committing secrets in application.yml.
- Many
@Valuestrings with typos that fail only at runtime. - Profile-specific beans that make production behave differently from tests.
Key points
- Typed @ConfigurationProperties records
- Environment variables override files
- Profiles per environment
Further reading: Spring Boot: Externalized configuration
Practice questions
Why prefer @ConfigurationProperties over @Value?
Answer
It groups related settings into a typed, validated object with IDE metadata, avoiding scattered string keys and runtime typos.
How does SHOP_PAYMENTS_API_KEY relate to shop.payments.api-key?
Answer
Spring Boot's relaxed binding maps environment variables (uppercase, underscores) to property names, so the env var overrides the YAML value.
Open in the app to mark lessons complete, take notes and track your progress.