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, for example, 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 data binding in SpEL expressions:
- Data binding for read-only access
- Data binding for read and write access
- A custom
PropertyAccessor(typically not reflection-based), potentially combined with aDataBindingPropertyAccessor
Conveniently, forReadOnlyDataBinding() enables
read-only access to properties via DataBindingPropertyAccessor. Similarly,
forReadWriteDataBinding() enables read and write access
to properties. Alternatively, configure custom accessors via
forPropertyAccessors(org.springframework.expression.PropertyAccessor...), potentially
disable assignment, and optionally
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 predefined
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
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder forSimpleEvaluationContext. -
Method Summary
Modifier and TypeMethodDescriptionassignVariable(String name, Supplier<TypedValue> valueSupplier) SimpleEvaluationContextdoes not support variable assignment within expressions.forPropertyAccessors(PropertyAccessor... accessors) Create aSimpleEvaluationContextfor the specifiedPropertyAccessordelegates: typically a customPropertyAccessorspecific to a use case — for example, for attribute resolution in a custom data structure — potentially combined with aDataBindingPropertyAccessorif property dereferences are needed as well.Create aSimpleEvaluationContextfor read-only access to public properties viaDataBindingPropertyAccessor.Create aSimpleEvaluationContextfor read-write access to public properties viaDataBindingPropertyAccessor.SimpleEvaluationContextdoes not support the use of bean references.Return the specifiedIndexAccessordelegates, if any.Return the specifiedMethodResolverdelegates, if any.Return an instance ofStandardOperatorOverloader.Return the specifiedPropertyAccessordelegates, if any.Return the specified root object, if any.Return an instance ofStandardTypeComparator.The configuredTypeConverter.SimpleEvaluationContextdoes not support use of type references.booleanDetermine if assignment is enabled within expressions evaluated by this evaluation context.lookupVariable(String name) Look up a named variable or function within this evaluation context.voidsetVariable(String name, @Nullable 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, waitMethods inherited from interface org.springframework.expression.EvaluationContext
getConstructorResolvers
-
Method Details
-
getRootObject
Return the specified root object, if any.- Specified by:
getRootObjectin interfaceEvaluationContext
-
getPropertyAccessors
Return the specifiedPropertyAccessordelegates, if any.- Specified by:
getPropertyAccessorsin interfaceEvaluationContext- See Also:
-
getIndexAccessors
Return the specifiedIndexAccessordelegates, if any.- Specified by:
getIndexAccessorsin interfaceEvaluationContext- Since:
- 6.2
- See Also:
-
getMethodResolvers
Return the specifiedMethodResolverdelegates, if any.- Specified by:
getMethodResolversin interfaceEvaluationContext- See Also:
-
getBeanResolver
SimpleEvaluationContextdoes not support the use of bean references.- Specified by:
getBeanResolverin interfaceEvaluationContext- Returns:
- always
null
-
getTypeLocator
SimpleEvaluationContextdoes not support use of type references.- Specified by:
getTypeLocatorin interfaceEvaluationContext- Returns:
TypeLocatorimplementation that raises aSpelEvaluationExceptionwithSpelMessage.TYPE_NOT_FOUND
-
getTypeConverter
The configuredTypeConverter.By default this is
StandardTypeConverter. -
getTypeComparator
Return an instance ofStandardTypeComparator.- Specified by:
getTypeComparatorin interfaceEvaluationContext
-
getOperatorOverloader
Return an instance ofStandardOperatorOverloader.- Specified by:
getOperatorOverloaderin interfaceEvaluationContext
-
assignVariable
SimpleEvaluationContextdoes not support variable assignment within expressions.- Specified by:
assignVariablein interfaceEvaluationContext- Parameters:
name- the name of the variable to assignvalueSupplier- the supplier of the value to be assigned to the variable- Returns:
- a
TypedValuewrapping 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
Methodor aMethodHandle.Note that variables and functions share a common namespace in this evaluation context. See the class-level documentation for details.
- Specified by:
setVariablein 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:
lookupVariablein interfaceEvaluationContext- Parameters:
name- the name of the variable or function to look up- Returns:
- the value of the variable or function, or
nullif not found
-
isAssignmentEnabled
public boolean isAssignmentEnabled()Determine if assignment is enabled within expressions evaluated by this evaluation context.If this method returns
false, the assignment (=), increment (++), and decrement (--) operators are disabled.- Specified by:
isAssignmentEnabledin interfaceEvaluationContext- Returns:
trueif assignment is enabled;falseotherwise- Since:
- 5.3.38
- See Also:
-
forPropertyAccessors
Create aSimpleEvaluationContextfor the specifiedPropertyAccessordelegates: typically a customPropertyAccessorspecific to a use case — for example, for attribute resolution in a custom data structure — potentially combined with aDataBindingPropertyAccessorif property dereferences are needed as well.By default, assignment is enabled within expressions evaluated by the context created via this factory method; however, assignment can be disabled via
SimpleEvaluationContext.Builder.withAssignmentDisabled().- Parameters:
accessors- the accessor delegates to use- See Also:
-
forReadOnlyDataBinding
Create aSimpleEvaluationContextfor read-only access to public properties viaDataBindingPropertyAccessor.Assignment is disabled within expressions evaluated by the context created via this factory method.
-
forReadWriteDataBinding
Create aSimpleEvaluationContextfor read-write access to public properties viaDataBindingPropertyAccessor.By default, assignment is enabled within expressions evaluated by the context created via this factory method. Assignment can be disabled via
SimpleEvaluationContext.Builder.withAssignmentDisabled(); however, it is preferable to useforReadOnlyDataBinding()if you desire read-only access.
-