🎯 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.properties vs application-{profile}.properties
  • Activating profiles via CLI, env var, JVM flag, or properties file
  • @Value for injecting individual property values
  • @ConfigurationProperties for binding a whole prefix to a typed object
  • @Profile for creating beans only in a specific environment
  • Spring Actuator /actuator/info and /actuator/health endpoints
💼 Interview reality: "How do you manage dev vs prod configuration in Spring Boot?" is a near-universal question. Knowing Profiles, the property source hierarchy, and @ConfigurationProperties signals production experience at any level.

🌍 Real-world scenario

🌍
NairaSwift (Lagos, Nigeria)
Dev vs Prod — one codebase, different configs

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.

curl -s http://localhost:8765/actuator/info
application.propertiesLesson 24 · shared across all environments
application-dev.propertiesLesson 24 · dev profile overrides
application-prod.propertiesLesson 24 · prod profile overrides
REAL — /actuator/info{ "app": { "name": "NairaSwift API", "description": "Cross-border payments — Lagos", "version": "1.0.0" } }

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.

curl -s http://localhost:8765/actuator/health
TransferLimitsBean.java + TransferConfig.javaLesson 24 · @Value and @ConfigurationProperties
REAL — /actuator/health{ "status": "UP", "components": { "diskSpace": { "status": "UP", "details": { "total": 494384795648, "free": 160877821952, "threshold": 10485760, "path": "/private/tmp/spring-arc-d/.", "exists": true } }, "ping": { "status": "UP" } } }

🏷️ 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.

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
DevDataSeeder.java + LiveFxRateClient.javaLesson 24 · @Profile beans + activation methods
startup log excerpt — dev profile active :: Spring Boot :: (v3.3.5) INFO nairaswift --- [ restartedMain] i.n.nairaswift.NairaswiftApplication : The following 1 profile is active: "dev" INFO nairaswift --- [ restartedMain] i.n.nairaswift.NairaswiftApplication : Started NairaswiftApplication in 0.851 seconds

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)

PrioritySourceExample
1 (highest)Command-line arguments--server.port=9090
2OS environment variablesSPRING_PROFILES_ACTIVE=prod
3JVM system properties-Dserver.port=9090
4application-{profile}.propertiesapplication-prod.properties
5application.propertiesBase config file
6@PropertySource annotations@PropertySource("classpath:custom.properties")
7 (lowest)Default values in @Value@Value("${key:default}")

🎤 Interview Q&A

EasyWhat is the difference between application.properties and application-dev.properties?
+

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.

EasyHow do you provide a default value in @Value?
+

Use the colon syntax: @Value("${nairaswift.env:local}") — if the property is missing, "local" is used.

MediumHow do you validate @ConfigurationProperties at startup?
+

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.

MediumCan you have multiple active profiles at once?
+

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.

HardHow does Spring Boot's property source hierarchy work?
+

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

Question 1 of 5
How do you activate a Spring profile via the command line?
Question 2 of 5
What annotation injects a single property value into a field?
Question 3 of 5
Which file is loaded only when the "prod" profile is active?
Question 4 of 5
What is @ConfigurationProperties used for?
Question 5 of 5
Which annotation marks a bean to be created only for a specific profile?
0
/ 5
Quiz complete!

Next: Spring Security Basics →

⚠️ Common mistakes

Hardcoding environment values in code
Using if (env.equals("prod")) in Java code instead of profile-specific beans or properties makes tests brittle and configs un-auditable.
Forgetting @EnableConfigurationProperties
@ConfigurationProperties classes need either @EnableConfigurationProperties(MyConfig.class) on a @Configuration or @ConfigurationPropertiesScan on the main class to be registered.
Committing secrets to application.properties
Never put API keys or passwords in committed files. Use environment variables, Vault, or spring.config.import from a secrets manager.
Property key typo — silent null
@Value("${nairaswift.trasfer.fee-pct}") silently injects null/0 if the key doesn't exist and no default is provided. Add @Validated to catch this at startup.

✅ 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
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →