Class SimpleEvaluationContext
- java.lang.Object
- org.springframework.expression.spel.support.SimpleEvaluationContext
- All Implemented Interfaces:
EvaluationContext
public class SimpleEvaluationContext extends Object implements EvaluationContext
A basic implementation ofEvaluationContextthat 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,
SimpleEvaluationContextis 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
SimpleEvaluationContextyou 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 viaDataBindingPropertyAccessor; same forforReadWriteDataBinding()when write access is needed as well. Alternatively, configure custom accessors viaforPropertyAccessors(org.springframework.expression.PropertyAccessor...), and potentially activate method resolution and/or a type converter through the builder.Note that
SimpleEvaluationContextis typically not configured with a default root object. Instead it is meant to be created once and used repeatedly throughgetValuecalls on a pre-compiledExpressionwith both anEvaluationContextand a root object as arguments:Expression.getValue(EvaluationContext, Object).For more power and flexibility, in particular for internal configuration scenarios, consider using
StandardEvaluationContextinstead.- Since:
- 4.3.15
- Author:
- Rossen Stoyanchev, Juergen Hoeller
- See Also:
forPropertyAccessors(org.springframework.expression.PropertyAccessor...),forReadOnlyDataBinding(),forReadWriteDataBinding(),StandardEvaluationContext,StandardTypeConverter,DataBindingPropertyAccessor
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSimpleEvaluationContext.BuilderBuilder forSimpleEvaluationContext.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SimpleEvaluationContext.BuilderforPropertyAccessors(PropertyAccessor... accessors)Create aSimpleEvaluationContextfor the specifiedPropertyAccessordelegates: typically a customPropertyAccessorspecific to a use case (e.g.static SimpleEvaluationContext.BuilderforReadOnlyDataBinding()Create aSimpleEvaluationContextfor read-only access to public properties viaDataBindingPropertyAccessor.static SimpleEvaluationContext.BuilderforReadWriteDataBinding()Create aSimpleEvaluationContextfor read-write access to public properties viaDataBindingPropertyAccessor.BeanResolvergetBeanResolver()SimpleEvaluationContextdoes 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 specifiedMethodResolverdelegates, if any.OperatorOverloadergetOperatorOverloader()Return an instance ofStandardOperatorOverloader.List<PropertyAccessor>getPropertyAccessors()Return the specifiedPropertyAccessordelegates, if any.TypedValuegetRootObject()Return the specified root object, if any.TypeComparatorgetTypeComparator()Return an instance ofStandardTypeComparator.TypeConvertergetTypeConverter()The configuredTypeConverter.TypeLocatorgetTypeLocator()SimpleEvaluationContextdoes not support use of type references.ObjectlookupVariable(String name)Look up a named variable within this evaluation context.voidsetVariable(String name, Object value)Set a named variable within this evaluation context to a specified value.
Method Detail
getRootObject
public TypedValue getRootObject()
Return the specified root object, if any.- Specified by:
getRootObjectin interfaceEvaluationContext
getPropertyAccessors
public List<PropertyAccessor> getPropertyAccessors()
Return the specifiedPropertyAccessordelegates, if any.- Specified by:
getPropertyAccessorsin interfaceEvaluationContext- See Also:
forPropertyAccessors(org.springframework.expression.PropertyAccessor...)
getConstructorResolvers
public List<ConstructorResolver> getConstructorResolvers()
Return an empty list, always, since this context does not support the use of type references.- Specified by:
getConstructorResolversin interfaceEvaluationContext
getMethodResolvers
public List<MethodResolver> getMethodResolvers()
Return the specifiedMethodResolverdelegates, if any.- Specified by:
getMethodResolversin interfaceEvaluationContext- See Also:
SimpleEvaluationContext.Builder.withMethodResolvers(org.springframework.expression.MethodResolver...)
getBeanResolver
public BeanResolver getBeanResolver()
SimpleEvaluationContextdoes not support the use of bean references.- Specified by:
getBeanResolverin interfaceEvaluationContext- Returns:
- always
null
getTypeLocator
public TypeLocator getTypeLocator()
SimpleEvaluationContextdoes not support use of type references.- Specified by:
getTypeLocatorin interfaceEvaluationContext- Returns:
TypeLocatorimplementation that raises aSpelEvaluationExceptionwithSpelMessage.TYPE_NOT_FOUND.
getTypeConverter
public TypeConverter getTypeConverter()
The configuredTypeConverter.By default this is
StandardTypeConverter.
getTypeComparator
public TypeComparator getTypeComparator()
Return an instance ofStandardTypeComparator.- Specified by:
getTypeComparatorin interfaceEvaluationContext
getOperatorOverloader
public OperatorOverloader getOperatorOverloader()
Return an instance ofStandardOperatorOverloader.- Specified by:
getOperatorOverloaderin interfaceEvaluationContext
setVariable
public void setVariable(String name, Object value)
Description copied from interface:EvaluationContextSet a named variable within this evaluation context to a specified value.- Specified by:
setVariablein interfaceEvaluationContext- Parameters:
name- variable to setvalue- value to be placed in the variable
lookupVariable
public Object lookupVariable(String name)
Description copied from interface:EvaluationContextLook up a named variable within this evaluation context.- Specified by:
lookupVariablein interfaceEvaluationContext- Parameters:
name- variable to lookup- Returns:
- the value of the variable, or
nullif not found
forPropertyAccessors
public static SimpleEvaluationContext.Builder forPropertyAccessors(PropertyAccessor... accessors)
Create aSimpleEvaluationContextfor the specifiedPropertyAccessordelegates: typically a customPropertyAccessorspecific to a use case (e.g. attribute resolution in a custom data structure), potentially combined with aDataBindingPropertyAccessorif property dereferences are needed as well.- Parameters:
accessors- the accessor delegates to use- See Also:
DataBindingPropertyAccessor.forReadOnlyAccess(),DataBindingPropertyAccessor.forReadWriteAccess()
forReadOnlyDataBinding
public static SimpleEvaluationContext.Builder forReadOnlyDataBinding()
Create aSimpleEvaluationContextfor read-only access to public properties viaDataBindingPropertyAccessor.
forReadWriteDataBinding
public static SimpleEvaluationContext.Builder forReadWriteDataBinding()
Create aSimpleEvaluationContextfor read-write access to public properties viaDataBindingPropertyAccessor.