🎯 What you will learn
Production applications never hardcode environment-specific values. Spring Boot's config and profile system lets the same JAR read different properties — rate limits, logging levels, API endpoints — based on where it's deployed. By the end of this lesson you will understand:
- The layered config hierarchy:
application.propertiesvsapplication-{profile}.properties - Activating profiles via CLI, env var, JVM flag, or properties file
@Valuefor injecting individual property values@ConfigurationPropertiesfor binding a whole prefix to a typed object@Profilefor creating beans only in a specific environment- Spring Actuator
/actuator/infoand/actuator/healthendpoints
🌍 Real-world scenario
NairaSwift runs with loose rate limits and DEBUG logging in dev, but strict limits and WARN-only logging in production. Samuel uses Spring Profiles so the same JAR reads the right config automatically — no manual property changes before deployment.
⚙️ Block 1 — application.properties + profile files
application.properties is always loaded. application-{profile}.properties is loaded additionally when that profile is active, and its values override any matching keys from the base file.
Override order: Profile-specific keys always win over the base application.properties. The base file provides safe defaults; profile files specialise them. This means you never need an if-else in Java for environment branching.
🔧 Block 2 — @Value and @ConfigurationProperties
@Value is quick for one-off injections. @ConfigurationProperties is the clean solution when you have a group of related properties — it binds an entire prefix to a typed record or class, with IDE completion and startup validation support.
🏷️ Block 3 — @Profile beans + activating profiles
@Profile("dev") on a @Component or @Bean means the Spring container only creates that bean when the matching profile is active. This is the clean way to swap entire implementations between environments — no if/else, no magic strings in business logic.
Activation priority: Command-line args and environment variables override application.properties. In containers and CI/CD, set SPRING_PROFILES_ACTIVE as an environment variable — never commit the active profile in code.
📋 Property source hierarchy (highest → lowest priority)
| Priority | Source | Example |
|---|---|---|
| 1 (highest) | Command-line arguments | --server.port=9090 |
| 2 | OS environment variables | SPRING_PROFILES_ACTIVE=prod |
| 3 | JVM system properties | -Dserver.port=9090 |
| 4 | application-{profile}.properties | application-prod.properties |
| 5 | application.properties | Base config file |
| 6 | @PropertySource annotations | @PropertySource("classpath:custom.properties") |
| 7 (lowest) | Default values in @Value | @Value("${key:default}") |
🎤 Interview Q&A
application.properties loads for every profile. application-dev.properties loads additionally when the "dev" profile is active. Profile-specific values override shared values for matching keys.
Use the colon syntax: @Value("${nairaswift.env:local}") — if the property is missing, "local" is used.
Add @Validated to the @ConfigurationProperties class and use Jakarta constraint annotations (@NotNull, @Min, etc.) on the fields. Spring validates them during context refresh and fails fast if invalid.
Yes. Set spring.profiles.active=dev,debug (comma-separated). Beans annotated @Profile("dev") or @Profile("debug") will both be created. Use @Profile("dev & debug") for AND logic.
Spring resolves properties in this order (highest wins): command-line arguments → SPRING_APPLICATION_JSON env var → OS environment variables → application-{profile}.properties → application.properties → @PropertySource annotations → default values in @Value. Understanding this order prevents subtle config bugs in deployed containers.
📝 Quiz — 5 questions
Next: Spring Security Basics →
⚠️ Common mistakes
✅ Key takeaways
- application-{profile}.properties overrides application.properties per environment
- Activate profiles via CLI arg, env var, or JVM system property — not in code
- @Value for individual properties; @ConfigurationProperties for a typed config object
- @Profile("prod") beans are only created in the matching environment
- Never put secrets in committed .properties files — use env vars or a secrets manager
One JAR. Every environment.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →