public final class SimpleEvaluationContext extends Object implements 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 data binding in SpEL expressions:
PropertyAccessor
(typically not reflection-based), potentially
combined with a DataBindingPropertyAccessor
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)
.
For more power and flexibility, in particular for internal configuration
scenarios, consider using StandardEvaluationContext
instead.
forReadOnlyDataBinding()
,
forReadWriteDataBinding()
,
forPropertyAccessors(org.springframework.expression.PropertyAccessor...)
,
StandardEvaluationContext
,
StandardTypeConverter
,
DataBindingPropertyAccessor
Modifier and Type | Class and Description |
---|---|
static class |
SimpleEvaluationContext.Builder
Builder for
SimpleEvaluationContext . |
Modifier and Type | Method and Description |
---|---|
TypedValue |
assignVariable(String name,
Supplier<TypedValue> valueSupplier)
SimpleEvaluationContext does not support variable assignment within
expressions. |
static SimpleEvaluationContext.Builder |
forPropertyAccessors(PropertyAccessor... accessors)
Create a
SimpleEvaluationContext for the specified PropertyAccessor
delegates: typically a custom PropertyAccessor specific to a use case —
for example, for attribute resolution in a custom data structure — potentially
combined with a DataBindingPropertyAccessor if property dereferences are
needed as well. |
static SimpleEvaluationContext.Builder |
forReadOnlyDataBinding()
Create a
SimpleEvaluationContext for read-only access to
public properties via DataBindingPropertyAccessor . |
static SimpleEvaluationContext.Builder |
forReadWriteDataBinding()
Create a
SimpleEvaluationContext for read-write access to
public properties via DataBindingPropertyAccessor . |
BeanResolver |
getBeanResolver()
SimpleEvaluationContext does not support the use of bean references. |
List<ConstructorResolver> |
getConstructorResolvers()
Return an empty list, always, since this context does not support the
use of type references.
|
List<MethodResolver> |
getMethodResolvers()
Return the specified
MethodResolver delegates, if any. |
OperatorOverloader |
getOperatorOverloader()
Return an instance of
StandardOperatorOverloader . |
List<PropertyAccessor> |
getPropertyAccessors()
Return the specified
PropertyAccessor delegates, if any. |
TypedValue |
getRootObject()
Return the specified root object, if any.
|
TypeComparator |
getTypeComparator()
Return an instance of
StandardTypeComparator . |
TypeConverter |
getTypeConverter()
The configured
TypeConverter . |
TypeLocator |
getTypeLocator()
SimpleEvaluationContext does not support use of type references. |
boolean |
isAssignmentEnabled()
Determine if assignment is enabled within expressions evaluated by this evaluation
context.
|
Object |
lookupVariable(String name)
Look up a named variable within this evaluation context.
|
void |
setVariable(String name,
Object value)
Set a named variable in this evaluation context to a specified value.
|
public TypedValue getRootObject()
getRootObject
in interface EvaluationContext
public List<PropertyAccessor> getPropertyAccessors()
PropertyAccessor
delegates, if any.getPropertyAccessors
in interface EvaluationContext
forPropertyAccessors(org.springframework.expression.PropertyAccessor...)
public List<ConstructorResolver> getConstructorResolvers()
getConstructorResolvers
in interface EvaluationContext
public List<MethodResolver> getMethodResolvers()
MethodResolver
delegates, if any.getMethodResolvers
in interface EvaluationContext
SimpleEvaluationContext.Builder.withMethodResolvers(org.springframework.expression.MethodResolver...)
@Nullable public BeanResolver getBeanResolver()
SimpleEvaluationContext
does not support the use of bean references.getBeanResolver
in interface EvaluationContext
null
public TypeLocator getTypeLocator()
SimpleEvaluationContext
does not support use of type references.getTypeLocator
in interface EvaluationContext
TypeLocator
implementation that raises a
SpelEvaluationException
with SpelMessage.TYPE_NOT_FOUND
.public TypeConverter getTypeConverter()
TypeConverter
.
By default this is StandardTypeConverter
.
public TypeComparator getTypeComparator()
StandardTypeComparator
.getTypeComparator
in interface EvaluationContext
public OperatorOverloader getOperatorOverloader()
StandardOperatorOverloader
.getOperatorOverloader
in interface EvaluationContext
public TypedValue assignVariable(String name, Supplier<TypedValue> valueSupplier)
SimpleEvaluationContext
does not support variable assignment within
expressions.assignVariable
in interface EvaluationContext
name
- the name of the variable to assignvalueSupplier
- the supplier of the value to be assigned to the variableTypedValue
wrapping the assigned valueSpelEvaluationException
- with SpelMessage.VARIABLE_ASSIGNMENT_NOT_SUPPORTED
public void setVariable(String name, @Nullable Object value)
EvaluationContext
In contrast to EvaluationContext.assignVariable(String, Supplier)
, this method
should only be invoked programmatically when interacting directly with the
EvaluationContext
— for example, to provide initial
configuration for the context.
setVariable
in interface EvaluationContext
name
- the name of the variable to setvalue
- the value to be placed in the variable@Nullable public Object lookupVariable(String name)
EvaluationContext
lookupVariable
in interface EvaluationContext
name
- the name of the variable to look upnull
if not foundpublic boolean isAssignmentEnabled()
If this method returns false
, the assignment (=
), increment
(++
), and decrement (--
) operators are disabled.
isAssignmentEnabled
in interface EvaluationContext
true
if assignment is enabled; false
otherwiseforReadOnlyDataBinding()
,
SimpleEvaluationContext.Builder.withAssignmentDisabled()
public static SimpleEvaluationContext.Builder forPropertyAccessors(PropertyAccessor... accessors)
SimpleEvaluationContext
for the specified PropertyAccessor
delegates: typically a custom PropertyAccessor
specific to a use case —
for example, for attribute resolution in a custom data structure — potentially
combined with a DataBindingPropertyAccessor
if 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()
.
accessors
- the accessor delegates to useDataBindingPropertyAccessor.forReadOnlyAccess()
,
DataBindingPropertyAccessor.forReadWriteAccess()
,
isAssignmentEnabled()
,
SimpleEvaluationContext.Builder.withAssignmentDisabled()
public static SimpleEvaluationContext.Builder forReadOnlyDataBinding()
SimpleEvaluationContext
for read-only access to
public properties via DataBindingPropertyAccessor
.
Assignment is disabled within expressions evaluated by the context created via this factory method.
public static SimpleEvaluationContext.Builder forReadWriteDataBinding()
SimpleEvaluationContext
for read-write access to
public properties via DataBindingPropertyAccessor
.
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 use
forReadOnlyDataBinding()
if you desire read-only access.