@Value and Property Injection — Externalized Configuration
Fast recall
Quick revision
Core answer
@Value("${app.name}")), a SpEL expression (@Value("#{systemProperties['user.home']}")), or a combined form (@Value("#{'${app.tags}'.split(',')}")). Resolved from Spring's Environment — command-line args, env vars, application.properties/yml, profile-specific files. Provide default values with ${app.name:myapp} to avoid startup failures when the property is missing. For multiple related properties, use `@ConfigurationProperties` on a POJO class — type safety, nested structure, validation via @Validated, IDE autocomplete from metadata. @Value for one-off single properties; @ConfigurationProperties for any structured config of 3+ related values.Whether you know the placeholder syntax, default value syntax, and when to prefer @ConfigurationProperties over @Value.
Using @Value for many related properties (repetitive, no type safety); forgetting default values so missing properties break startup; confusing ${} (placeholder) with #{} (SpEL).
Recommend @ConfigurationProperties with a @Validated POJO for any config group larger than 2-3 properties; mention spring-boot-configuration-processor for IDE autocomplete.
Interview-ready guide
Interview answer
@Value is the simplest way to inject external configuration — and the easiest to get wrong. A missing key crashes at startup, a space after the colon silently poisons URLs, and injected values never update without a restart.
The `${}` syntax
@Value("${key}")looks up the key in Spring'sEnvironment; if missing, throwsIllegalArgumentExceptionand the application will not start- Add a default with a colon:
@Value("${key:fallback}")— no space after the colon, or the space becomes part of the injected value - Resolution happens once at bean creation and is cached — live reload requires
@RefreshScopefrom Spring Cloud
The `#{}` SpEL syntax
- SpEL expressions go in
#{}; they can call methods, reference beans, and evaluate ternary operators - Most common pattern:
#{'${csv}'.split(',')}—${}resolves the property first, SpEL transforms it; always${}inside#{}, never reversed - If the SpEL expression fails at startup, you get
SpelEvaluationException— keep expressions simple
When to switch to `@ConfigurationProperties`
- Three or more related properties sharing a prefix
- Binds the entire prefix to a POJO, validates at startup with
@Valid, and gives your IDE auto-completion for your custom properties @Valuefor isolated one-off values;@ConfigurationPropertiesfor anything with a prefix
@Value and Property Injection — Externalized Configuration
Direct Answer
@Value injects externalized configuration into Spring beans. It accepts a property placeholder (@Value("${app.name}")), a SpEL expression (@Value("#{systemProperties['user.home']}")), or a combined form (@Value("#{'${app.tags}'.split(',')}")). Resolved from Spring's Environment — command-line args, env vars, application.properties/yml, profile-specific files. Provide default values with ${app.name:myapp} to avoid startup failures when the property is missing. For multiple related properties, use @ConfigurationProperties on a POJO class — type safety, nested structure, validation via @Validated, IDE autocomplete from metadata. @Value for one-off single properties; @ConfigurationProperties for any structured config of 3+ related values.
Common Mistakes & Pitfalls
Using @Value for many related properties (repetitive, no type safety); forgetting default values so missing properties break startup; confusing ${} (placeholder) with #{} (SpEL).
Check your understanding
Answer these without looking back at the article.
- When should you use @Value vs @ConfigurationProperties?
- How do you provide a default value for a missing property with @Value?
- Can @Value be used on constructor parameters?
- How does spring-boot-configuration-processor enhance @ConfigurationProperties?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.