Class SimpleEvaluationContext
- All Implemented Interfaces:
EvaluationContext
EvaluationContext
that focuses on a subset
of essential SpEL features and customization options, targeting simple
condition evaluation and in particular data binding scenarios.
In many cases, the full extent of the SpEL language is not required and
should be meaningfully restricted. Examples include but are not limited to
data binding expressions, property-based filters, and others. To that effect,
SimpleEvaluationContext
is tailored to support only a subset of the
SpEL language syntax, e.g. excluding references to Java types, constructors,
and bean references.
When creating a SimpleEvaluationContext
you need to choose the
level of support that you need for property access in SpEL expressions:
- A custom
PropertyAccessor
(typically not reflection-based), potentially combined with aDataBindingPropertyAccessor
- Data binding properties for read-only access
- Data binding properties for read and write
Conveniently, forReadOnlyDataBinding()
enables read access to properties via DataBindingPropertyAccessor
;
same for forReadWriteDataBinding()
when
write access is needed as well. Alternatively, configure custom accessors
via forPropertyAccessors(org.springframework.expression.PropertyAccessor...)
, and potentially
activate method resolution and/or a type converter through the builder.
Note that SimpleEvaluationContext
is typically not configured
with a default root object. Instead it is meant to be created once and
used repeatedly through getValue
calls on a pre-compiled
Expression
with both an
EvaluationContext
and a root object as arguments:
Expression.getValue(EvaluationContext, Object)
.
In addition to support for setting and looking up variables as defined in
the EvaluationContext
API, SimpleEvaluationContext
also
provides support for registering and
looking up functions as variables. Since
functions share a common namespace with the variables in this evaluation
context, care must be taken to ensure that function names and variable names
do not overlap.
For more power and flexibility, in particular for internal configuration
scenarios, consider using StandardEvaluationContext
instead.
- Since:
- 4.3.15
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Sam Brannen
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Builder forSimpleEvaluationContext
. -
Method Summary
Modifier and TypeMethodDescriptionassignVariable
(String name, Supplier<TypedValue> valueSupplier) SimpleEvaluationContext
does not support variable assignment within expressions.forPropertyAccessors
(PropertyAccessor... accessors) Create aSimpleEvaluationContext
for the specifiedPropertyAccessor
delegates: typically a customPropertyAccessor
specific to a use case (e.g.Create aSimpleEvaluationContext
for read-only access to public properties viaDataBindingPropertyAccessor
.Create aSimpleEvaluationContext
for read-write access to public properties viaDataBindingPropertyAccessor
.SimpleEvaluationContext
does not support the use of bean references.Return the specifiedIndexAccessor
delegates, if any.Return the specifiedMethodResolver
delegates, if any.Return an instance ofStandardOperatorOverloader
.Return the specifiedPropertyAccessor
delegates, if any.Return the specified root object, if any.Return an instance ofStandardTypeComparator
.The configuredTypeConverter
.SimpleEvaluationContext
does not support use of type references.lookupVariable
(String name) Look up a named variable or function within this evaluation context.void
setVariable
(String name, Object value) Set a named variable or function in this evaluation context to the specified value.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.expression.EvaluationContext
getConstructorResolvers
-
Method Details
-
getRootObject
Return the specified root object, if any.- Specified by:
getRootObject
in interfaceEvaluationContext
-
getPropertyAccessors
Return the specifiedPropertyAccessor
delegates, if any.- Specified by:
getPropertyAccessors
in interfaceEvaluationContext
- See Also:
-
getIndexAccessors
Return the specifiedIndexAccessor
delegates, if any.- Specified by:
getIndexAccessors
in interfaceEvaluationContext
- Since:
- 6.2
- See Also:
-
getMethodResolvers
Return the specifiedMethodResolver
delegates, if any.- Specified by:
getMethodResolvers
in interfaceEvaluationContext
- See Also:
-
getBeanResolver
SimpleEvaluationContext
does not support the use of bean references.- Specified by:
getBeanResolver
in interfaceEvaluationContext
- Returns:
- always
null
-
getTypeLocator
SimpleEvaluationContext
does not support use of type references.- Specified by:
getTypeLocator
in interfaceEvaluationContext
- Returns:
TypeLocator
implementation that raises aSpelEvaluationException
withSpelMessage.TYPE_NOT_FOUND
-
getTypeConverter
The configuredTypeConverter
.By default this is
StandardTypeConverter
. -
getTypeComparator
Return an instance ofStandardTypeComparator
.- Specified by:
getTypeComparator
in interfaceEvaluationContext
-
getOperatorOverloader
Return an instance ofStandardOperatorOverloader
.- Specified by:
getOperatorOverloader
in interfaceEvaluationContext
-
assignVariable
SimpleEvaluationContext
does not support variable assignment within expressions.- Specified by:
assignVariable
in interfaceEvaluationContext
- Parameters:
name
- the name of the variable to assignvalueSupplier
- the supplier of the value to be assigned to the variable- Returns:
- a
TypedValue
wrapping the assigned value - Throws:
SpelEvaluationException
- withSpelMessage.VARIABLE_ASSIGNMENT_NOT_SUPPORTED
- Since:
- 5.2.24
-
setVariable
Set a named variable or function in this evaluation context to the specified value.A function can be registered as a
Method
or aMethodHandle
.Note that variables and functions share a common namespace in this evaluation context. See the class-level documentation for details.
- Specified by:
setVariable
in interfaceEvaluationContext
- Parameters:
name
- the name of the variable or function to setvalue
- the value to be placed in the variable or function- See Also:
-
lookupVariable
Look up a named variable or function within this evaluation context.Note that variables and functions share a common namespace in this evaluation context. See the class-level documentation for details.
- Specified by:
lookupVariable
in interfaceEvaluationContext
- Parameters:
name
- the name of the variable or function to look up- Returns:
- the value of the variable or function, or
null
if not found
-
forPropertyAccessors
Create aSimpleEvaluationContext
for the specifiedPropertyAccessor
delegates: typically a customPropertyAccessor
specific to a use case (e.g. attribute resolution in a custom data structure), potentially combined with aDataBindingPropertyAccessor
if property dereferences are needed as well.- Parameters:
accessors
- the accessor delegates to use- See Also:
-
forReadOnlyDataBinding
Create aSimpleEvaluationContext
for read-only access to public properties viaDataBindingPropertyAccessor
. -
forReadWriteDataBinding
Create aSimpleEvaluationContext
for read-write access to public properties viaDataBindingPropertyAccessor
.
-