Spring @Value with SpEL Integration¶
Spring @Value with SpEL Integration refers to the capability of the Spring Framework to inject values into beans using the Spring Expression Language (SpEL). This feature allows developers to dynamically define values using complex expressions, literals, or references to other beans within the application context, rather than relying solely on static property values.^[600-developer__spring__spring-spel.md]
Syntax¶
The integration is primarily utilized via the @Value annotation.^[600-developer__spring__spring-spel.md] While the standard syntax for property placeholders is ${...}, SpEL expressions are delimited using the #{...} syntax^[600-developer__spring__spring-spel.md].
@Value("#{ <expression string> }")
This delimiter configuration is defined by the ParserContext.TEMPLATE_EXPRESSION setting within the underlying expression parser^[600-developer__spring__spring-spel.md].
Core Features¶
SpEL supports a wide range of operations within the @Value annotation, enabling logic that goes beyond simple value injection^[600-developer__spring__spring-spel.md].
Literal and Mathematical Expressions¶
SpEL can evaluate literal values and perform mathematical operations^[600-developer__spring__spring-spel.md].
* Literals: Strings, numbers (integers, decimals), and booleans can be used directly.
* Math: Operators such as +, -, *, /, and ^ (power) are supported.
Logical and Relational Operators¶
Expressions can include comparison and logical checks^[600-developer__spring__spring-spel.md].
* Relational: Supports ==, eq, !=, ne, <, lt, >, gt, <=, le, >=, ge.
* Logical: Supports and, or, not, |, !.
* Ternary: The classic ?: operator (e.g., condition ? trueValue : falseValue) is available for conditional logic^[600-developer__spring__spring-spel.md].
Type References and Methods¶
SpEL allows direct interaction with Java types and methods^[600-developer__spring__spring-spel.md].
* Calling Static Methods: The T() operator is used to reference a class. For example, T(java.lang.Math).PI accesses the PI constant^[600-developer__spring__spring-spel.md].
* Method Invocation: Both instance methods on injected beans and static methods on classes can be called directly within the expression.
Bean References¶
Expressions can reference other beans managed by the Spring container to access their properties or invoke methods^[600-developer__spring__spring-spel.md].
// Example: Referencing a bean 'tutorial' and accessing a list element
@Value("#{tutorial.topicsList[0]}")
private Topic defaultTopic;
Collections and Variables¶
SpEL provides syntax for inline creation of collections and variable management^[600-developer__spring__spring-spel.md].
* List Creation: Lists can be defined inline, e.g., {'value1', 'value2'}.
* Map Creation: Maps can be defined inline, e.g., {'key1':'value1', 'key2':'value2'}.
* Variables: Variables can be defined and referenced within the evaluation context (e.g., #variableName).
Implementation Details¶
The processing of these annotations relies on specific infrastructure components^[600-developer__spring__spring-spel.md].
Components¶
- SpelExpressionParser: The parser responsible for reading the expression string^[600-developer__spring__spring-spel.md].
- EvaluationContext: The context used for setting variables and resolving properties (e.g.,
StandardEvaluationContext)^[600-developer__spring__spring-spel.md]. - Expression: The parsed object obtained after calling
parseExpression(), on whichgetValue()is invoked to retrieve the result^[600-developer__spring__spring-spel.md].
Property Integration¶
SpEL and @Value integrate closely with Spring's environment and property management systems^[600-developer__spring__spring-spel.md].
* PropertySource: SpEL can access properties defined in @PropertySource.
* PropertyResolver: It can utilize PropertyResolver to resolve placeholders and access environment variables.
* Profiles: Expressions can evaluate active Spring profiles to conditionally inject values (e.g., checking environment.acceptsProfiles(...)^[600-developer__spring__spring-spel.md]).
Related Concepts¶
- Spring Framework
- [[Dependency Injection]]
- [[SpEL]]
Sources¶
600-developer__spring__spring-spel.md