public 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 DataBindingPropertyAccessorConveniently, 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| 限定符和类型 | 类和说明 |
|---|---|
static class |
SimpleEvaluationContext.Builder
Builder for
SimpleEvaluationContext. |
| 限定符和类型 | 方法和说明 |
|---|---|
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. 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. |
Object |
lookupVariable(String name)
Look up a named variable within this evaluation context.
|
void |
setVariable(String name,
Object value)
Set a named variable within this evaluation context to a specified value.
|
public TypedValue getRootObject()
getRootObject 在接口中 EvaluationContextpublic List<PropertyAccessor> getPropertyAccessors()
PropertyAccessor delegates, if any.public List<ConstructorResolver> getConstructorResolvers()
getConstructorResolvers 在接口中 EvaluationContextpublic List<MethodResolver> getMethodResolvers()
MethodResolver delegates, if any.@Nullable public BeanResolver getBeanResolver()
SimpleEvaluationContext does not support the use of bean references.getBeanResolver 在接口中 EvaluationContextnullpublic TypeLocator getTypeLocator()
SimpleEvaluationContext does not support use of type references.getTypeLocator 在接口中 EvaluationContextTypeLocator implementation that raises a
SpelEvaluationException with SpelMessage.TYPE_NOT_FOUND.public TypeConverter getTypeConverter()
TypeConverter.
By default this is StandardTypeConverter.
public TypeComparator getTypeComparator()
StandardTypeComparator.getTypeComparator 在接口中 EvaluationContextpublic OperatorOverloader getOperatorOverloader()
StandardOperatorOverloader.getOperatorOverloader 在接口中 EvaluationContextpublic void setVariable(String name, @Nullable Object value)
EvaluationContextsetVariable 在接口中 EvaluationContextname - variable to setvalue - value to be placed in the variable@Nullable public Object lookupVariable(String name)
EvaluationContextlookupVariable 在接口中 EvaluationContextname - variable to lookupnull 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.