Spring Expression Language (SpEL) — Dynamic Values in Configuration
Fast recall
Quick revision
Core answer
@Value, @ConditionalOnExpression, @Cacheable keys, security rules, and @PreAuthorize. Syntax: `#{...}` for expressions (compile-time/context-aware) and `${...}` for property placeholders — they're often combined as #{${app.some.prop} + 1}. SpEL can access beans (@beanName.method()), system properties (systemProperties['user.home']), environment variables, call static methods (T(java.lang.Math).random()), navigate objects, and perform arithmetic and logical operations. It's powerful but evaluated at runtime, so errors surface late. Use SpEL deliberately for cases where static configuration isn't enough — overuse turns configuration into an unreadable scripting language.Whether you know the `#{...}` vs `${...}` distinction and can cite realistic use cases beyond simple property injection
Confusing `#{...}` (SpEL) with `${...}` (property placeholder); using SpEL where a simple @Value or @Bean method would do
Cite realistic use cases like @Cacheable(key="#user.id") and @PreAuthorize("hasRole('ADMIN') and #request.owner == authentication.name"); mention SpEL compilation mode for performance
Interview-ready guide
Interview answer
Property placeholders (${key}) do simple lookups. Spring Expression Language (#{}) evaluates arbitrary expressions — method calls, ternary operators, bean references, arithmetic. The syntax difference matters: ${} resolves a value by name; #{} computes one.
The `${}` placeholder
- Resolves against Spring's
Environment—application.properties, environment variables, system properties - Missing key without a default throws
IllegalArgumentExceptionat startup - Colon syntax for defaults:
${key:fallback}— no space after the colon
The `#{}` SpEL syntax
- Evaluates expressions at bean creation time: method calls, ternary operators, bean property access, static method invocation with
T(ClassName) - Most common production pattern:
#{'${csv}'.split(',')}—${}resolves the property, SpEL transforms it into aList; always${}inside#{}, never reversed - If the expression fails at startup:
SpelEvaluationExceptionpointing to the annotation, not to debuggable code
Where SpEL appears beyond `@Value`
- Spring Security:
@PreAuthorize("hasRole('ADMIN') or #userId == authentication.principal.id")— evaluated with security-specific root objects on every call @Cacheable(condition = "#id > 100", unless = "#result == null")—conditionevaluated before execution,unlessafter with access to#result
Keep SpEL expressions simple — one transformation, one method call. Anything more complex belongs in a @PostConstruct method where you have IDE support, proper error handling, and a debugger.
Spring Expression Language (SpEL) — Dynamic Values in Configuration
Direct Answer
Spring Expression Language (SpEL) is a runtime expression engine integrated into Spring — it evaluates expressions embedded in @Value, @ConditionalOnExpression, @Cacheable keys, security rules, and @PreAuthorize. Syntax: #{...} for expressions (compile-time/context-aware) and ${...} for property placeholders — they're often combined as #{${app.some.prop} + 1}. SpEL can access beans (@beanName.method()), system properties (systemProperties['user.home']), environment variables, call static methods (T(java.lang.Math).random()), navigate objects, and perform arithmetic and logical operations. It's powerful but evaluated at runtime, so errors surface late. Use SpEL deliberately for cases where static configuration isn't enough — overuse turns configuration into an unreadable scripting language.
Common Mistakes & Pitfalls
Confusing #{...} (SpEL) with ${...} (property placeholder); using SpEL where a simple @Value or @Bean method would do
Check your understanding
Answer these without looking back at the article.
- What's the difference between `#{...}` and `${...}` in Spring?
- How do you use SpEL in @Cacheable to build dynamic cache keys?
- What is SpEL compilation mode and when should you enable it?
- How would you reference another bean from a SpEL expression?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.