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 property access in SpEL expressions:
PropertyAccessor
(typically not reflection-based),
potentially combined with a DataBindingPropertyAccessor
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)
.
For more power and flexibility, in particular for internal configuration
scenarios, consider using StandardEvaluationContext
instead.
forPropertyAccessors(org.springframework.expression.PropertyAccessor...)
,
forReadOnlyDataBinding()
,
forReadWriteDataBinding()
,
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
(e.g. |
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. |
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 static SimpleEvaluationContext.Builder forPropertyAccessors(PropertyAccessor... accessors)
SimpleEvaluationContext
for the specified PropertyAccessor
delegates: typically a custom PropertyAccessor
specific to a use case
(e.g. attribute resolution in a custom data structure), potentially combined with
a DataBindingPropertyAccessor
if property dereferences are needed as well.accessors
- the accessor delegates to useDataBindingPropertyAccessor.forReadOnlyAccess()
,
DataBindingPropertyAccessor.forReadWriteAccess()
public static SimpleEvaluationContext.Builder forReadOnlyDataBinding()
SimpleEvaluationContext
for read-only access to
public properties via DataBindingPropertyAccessor
.public static SimpleEvaluationContext.Builder forReadWriteDataBinding()
SimpleEvaluationContext
for read-write access to
public properties via DataBindingPropertyAccessor
.