Top Description Inners Fields Constructors Methods
org.springframework.expression.spel.support

public final Class SimpleEvaluationContext

extends Object
implements EvaluationContext
Class Inheritance
All Implemented Interfaces
org.springframework.expression.EvaluationContext
Imports
java.util.Arrays, .Collections, .HashMap, .List, .Map, java.util.function.Supplier, org.springframework.core.convert.ConversionService, .TypeDescriptor, org.springframework.expression.BeanResolver, .ConstructorResolver, .EvaluationContext, .MethodResolver, .OperatorOverloader, .PropertyAccessor, .TypeComparator, .TypeConverter, .TypeLocator, .TypedValue, org.springframework.expression.spel.SpelEvaluationException, .SpelMessage, org.springframework.lang.Nullable

A basic implementation of 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:

Conveniently, SimpleEvaluationContext#forReadOnlyDataBinding() enables read access to properties via DataBindingPropertyAccessor; same for SimpleEvaluationContext#forReadWriteDataBinding() when write access is needed as well. Alternatively, configure custom accessors via SimpleEvaluationContext#forPropertyAccessors, 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 org.springframework.expression.Expression with both an EvaluationContext and a root object as arguments: org.springframework.expression.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.

Authors
Rossen Stoyanchev, Juergen Hoeller, Sam Brannen
Since
4.3.15
See Also
forPropertyAccessors, forReadOnlyDataBinding(), forReadWriteDataBinding(), StandardEvaluationContext, StandardTypeConverter, DataBindingPropertyAccessor, DataBindingMethodResolver

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
SimpleEvaluationContext.Builder

Builder for SimpleEvaluationContext.

Field Summary

Modifier and TypeField and Description
private final List<MethodResolver>
private final OperatorOverloader
private final List<PropertyAccessor>
private final TypedValue
private final TypeComparator
private final TypeConverter
private static final TypeLocator
private final Map<String, Object>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public TypedValue
assignVariable(String
the name of the variable to assign
name
,
Supplier<TypedValue>
the supplier of the value to be assigned to the variable
valueSupplier
)

Overrides default org.springframework.expression.EvaluationContext.assignVariable.

SimpleEvaluationContext does not support variable assignment within expressions.

public static SimpleEvaluationContext.Builder
forPropertyAccessors(PropertyAccessor...
the accessor delegates to use
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.

public static SimpleEvaluationContext.Builder
forReadOnlyDataBinding()

Create a SimpleEvaluationContext for read-only access to public properties via DataBindingPropertyAccessor.

public static SimpleEvaluationContext.Builder
forReadWriteDataBinding()

Create a SimpleEvaluationContext for read-write access to public properties via DataBindingPropertyAccessor.

public BeanResolver

Returns:

always null
getBeanResolver
()

Implements org.springframework.expression.EvaluationContext.getBeanResolver.

SimpleEvaluationContext does not support the use of bean references.

public List<ConstructorResolver>
getConstructorResolvers()

Implements org.springframework.expression.EvaluationContext.getConstructorResolvers.

Return an empty list, always, since this context does not support the use of type references.

public List<MethodResolver>
public OperatorOverloader
public List<PropertyAccessor>
public TypedValue
getRootObject()

Implements org.springframework.expression.EvaluationContext.getRootObject.

Return the specified root object, if any.

public TypeComparator
public TypeConverter
public TypeLocator

Returns:

TypeLocator implementation that raises a SpelEvaluationException with SpelMessage#TYPE_NOT_FOUND
getTypeLocator
()

Implements org.springframework.expression.EvaluationContext.getTypeLocator.

SimpleEvaluationContext does not support use of type references.

public Object

Returns:

the value of the variable or function, or null if not found
lookupVariable
(String
the name of the variable or function to look up
name
)

Implements org.springframework.expression.EvaluationContext.lookupVariable.

Look up a named variable or function within this evaluation context.

public void
setVariable(String
the name of the variable or function to set
name
,
Object
the value to be placed in the variable or function
value
)

Implements org.springframework.expression.EvaluationContext.setVariable.

Set a named variable or function in this evaluation context to the specified value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

methodResolversback to summary
private final List<MethodResolver> methodResolvers
operatorOverloaderback to summary
private final OperatorOverloader operatorOverloader
propertyAccessorsback to summary
private final List<PropertyAccessor> propertyAccessors
rootObjectback to summary
private final TypedValue rootObject
typeComparatorback to summary
private final TypeComparator typeComparator
typeConverterback to summary
private final TypeConverter typeConverter
typeNotFoundTypeLocatorback to summary
private static final TypeLocator typeNotFoundTypeLocator
variablesback to summary
private final Map<String, Object> variables

Constructor Detail

SimpleEvaluationContextback to summary
private SimpleEvaluationContext(List<PropertyAccessor> accessors, List<MethodResolver> resolvers, TypeConverter converter, TypedValue rootObject)

Method Detail

assignVariableback to summary
public TypedValue assignVariable(String name, Supplier<TypedValue> valueSupplier)

Overrides default org.springframework.expression.EvaluationContext.assignVariable.

SimpleEvaluationContext does not support variable assignment within expressions.

Parameters
name:String

Doc from org.springframework.expression.EvaluationContext.assignVariable.

the name of the variable to assign

valueSupplier:Supplier<TypedValue>

Doc from org.springframework.expression.EvaluationContext.assignVariable.

the supplier of the value to be assigned to the variable

Returns:TypedValue

Doc from org.springframework.expression.EvaluationContext.assignVariable.

a TypedValue wrapping the assigned value

Annotations
@Override
Exceptions
SpelEvaluationException:
with SpelMessage#VARIABLE_ASSIGNMENT_NOT_SUPPORTED
Since
5.2.24
forPropertyAccessorsback to summary
public 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.

Parameters
accessors:PropertyAccessor[]

the accessor delegates to use

See Also
DataBindingPropertyAccessor#forReadOnlyAccess(), DataBindingPropertyAccessor#forReadWriteAccess()
forReadOnlyDataBindingback to summary
public static SimpleEvaluationContext.Builder forReadOnlyDataBinding()

Create a SimpleEvaluationContext for read-only access to public properties via DataBindingPropertyAccessor.

See Also
DataBindingPropertyAccessor#forReadOnlyAccess(), forPropertyAccessors
forReadWriteDataBindingback to summary
public static SimpleEvaluationContext.Builder forReadWriteDataBinding()

Create a SimpleEvaluationContext for read-write access to public properties via DataBindingPropertyAccessor.

See Also
DataBindingPropertyAccessor#forReadWriteAccess(), forPropertyAccessors
getBeanResolverback to summary
public BeanResolver getBeanResolver()

Implements org.springframework.expression.EvaluationContext.getBeanResolver.

SimpleEvaluationContext does not support the use of bean references.

Returns:BeanResolver

always null

Annotations
@Override
@Nullable
getConstructorResolversback to summary
public List<ConstructorResolver> getConstructorResolvers()

Implements org.springframework.expression.EvaluationContext.getConstructorResolvers.

Return an empty list, always, since this context does not support the use of type references.

Annotations
@Override
getMethodResolversback to summary
public List<MethodResolver> getMethodResolvers()

Implements org.springframework.expression.EvaluationContext.getMethodResolvers.

Return the specified MethodResolver delegates, if any.

Annotations
@Override
See Also
Builder#withMethodResolvers
getOperatorOverloaderback to summary
public OperatorOverloader getOperatorOverloader()

Implements org.springframework.expression.EvaluationContext.getOperatorOverloader.

Return an instance of StandardOperatorOverloader.

Annotations
@Override
getPropertyAccessorsback to summary
public List<PropertyAccessor> getPropertyAccessors()

Implements org.springframework.expression.EvaluationContext.getPropertyAccessors.

Return the specified PropertyAccessor delegates, if any.

Annotations
@Override
See Also
forPropertyAccessors
getRootObjectback to summary
public TypedValue getRootObject()

Implements org.springframework.expression.EvaluationContext.getRootObject.

Return the specified root object, if any.

Annotations
@Override
getTypeComparatorback to summary
public TypeComparator getTypeComparator()

Implements org.springframework.expression.EvaluationContext.getTypeComparator.

Return an instance of StandardTypeComparator.

Annotations
@Override
getTypeConverterback to summary
public TypeConverter getTypeConverter()

Implements org.springframework.expression.EvaluationContext.getTypeConverter.

The configured TypeConverter.

By default this is StandardTypeConverter.

Annotations
@Override
See Also
Builder#withTypeConverter, Builder#withConversionService
getTypeLocatorback to summary
public TypeLocator getTypeLocator()

Implements org.springframework.expression.EvaluationContext.getTypeLocator.

SimpleEvaluationContext does not support use of type references.

Returns:TypeLocator

TypeLocator implementation that raises a SpelEvaluationException with SpelMessage#TYPE_NOT_FOUND

Annotations
@Override
lookupVariableback to summary
public Object lookupVariable(String name)

Implements org.springframework.expression.EvaluationContext.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.

Parameters
name:String

the name of the variable or function to look up

Returns:Object

the value of the variable or function, or null if not found

Annotations
@Override
@Nullable
setVariableback to summary
public void setVariable(String name, Object value)

Implements org.springframework.expression.EvaluationContext.setVariable.

Set a named variable or function in this evaluation context to the specified value.

A function can be registered as a java.lang.reflect.Method or a java.lang.invoke.MethodHandle.

Note that variables and functions share a common namespace in this evaluation context. See the class-level documentation for details.

Parameters
name:String

the name of the variable or function to set

value:Object

the value to be placed in the variable or function

Annotations
@Override
See Also
lookupVariable(String)
org.springframework.expression.spel.support back to summary

public final Class SimpleEvaluationContext.Builder

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.expression.spel.support.SimpleEvaluationContext.Builder

Builder for SimpleEvaluationContext.

Field Summary

Modifier and TypeField and Description
private final List<PropertyAccessor>
private List<MethodResolver>
private TypedValue
private TypeConverter

Constructor Summary

AccessConstructor and Description
private
Builder(PropertyAccessor... accessors)

Method Summary

Modifier and TypeMethod and Description
public SimpleEvaluationContext
public SimpleEvaluationContext.Builder
withConversionService(ConversionService conversionService)

Register a custom ConversionService.

public SimpleEvaluationContext.Builder
withInstanceMethods()

Register a DataBindingMethodResolver for instance method invocation purposes (i.e. not supporting static methods) in addition to the specified property accessors, typically in combination with a DataBindingPropertyAccessor.

public SimpleEvaluationContext.Builder
withMethodResolvers(MethodResolver...
the resolver delegates to use
resolvers
)

Register the specified MethodResolver delegates for a combination of property access and method resolution.

public SimpleEvaluationContext.Builder
withRootObject(Object rootObject)

Specify a default root object to resolve against.

public SimpleEvaluationContext.Builder
withTypeConverter(TypeConverter converter)

Register a custom TypeConverter.

public SimpleEvaluationContext.Builder
withTypedRootObject(Object rootObject, TypeDescriptor typeDescriptor)

Specify a typed root object to resolve against.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

accessorsback to summary
private final List<PropertyAccessor> accessors
resolversback to summary
private List<MethodResolver> resolvers
rootObjectback to summary
private TypedValue rootObject
Annotations
@Nullable
typeConverterback to summary
private TypeConverter typeConverter
Annotations
@Nullable

Constructor Detail

Builderback to summary
private Builder(PropertyAccessor... accessors)

Method Detail

buildback to summary
public SimpleEvaluationContext build()
withConversionServiceback to summary
public SimpleEvaluationContext.Builder withConversionService(ConversionService conversionService)

Register a custom ConversionService.

By default a StandardTypeConverter backed by a org.springframework.core.convert.support.DefaultConversionService is used.

See Also
withTypeConverter, StandardTypeConverter#StandardTypeConverter(ConversionService)
withInstanceMethodsback to summary
public SimpleEvaluationContext.Builder withInstanceMethods()

Register a DataBindingMethodResolver for instance method invocation purposes (i.e. not supporting static methods) in addition to the specified property accessors, typically in combination with a DataBindingPropertyAccessor.

See Also
withMethodResolvers, SimpleEvaluationContext#forReadOnlyDataBinding(), SimpleEvaluationContext#forReadWriteDataBinding()
withMethodResolversback to summary
public SimpleEvaluationContext.Builder withMethodResolvers(MethodResolver... resolvers)

Register the specified MethodResolver delegates for a combination of property access and method resolution.

Parameters
resolvers:MethodResolver[]

the resolver delegates to use

See Also
withInstanceMethods(), SimpleEvaluationContext#forPropertyAccessors
withRootObjectback to summary
public SimpleEvaluationContext.Builder withRootObject(Object rootObject)

Specify a default root object to resolve against.

Default is none, expecting an object argument at evaluation time.

See Also
org.springframework.expression.Expression#getValue(EvaluationContext), org.springframework.expression.Expression#getValue(EvaluationContext, Object)
withTypeConverterback to summary
public SimpleEvaluationContext.Builder withTypeConverter(TypeConverter converter)

Register a custom TypeConverter.

By default a StandardTypeConverter backed by a org.springframework.core.convert.support.DefaultConversionService is used.

See Also
withConversionService, StandardTypeConverter#StandardTypeConverter()
withTypedRootObjectback to summary
public SimpleEvaluationContext.Builder withTypedRootObject(Object rootObject, TypeDescriptor typeDescriptor)

Specify a typed root object to resolve against.

Default is none, expecting an object argument at evaluation time.

See Also
org.springframework.expression.Expression#getValue(EvaluationContext), org.springframework.expression.Expression#getValue(EvaluationContext, Object)