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

public Class StandardEvaluationContext

extends Object
implements EvaluationContext
Class Inheritance
All Implemented Interfaces
org.springframework.expression.EvaluationContext
Imports
java.lang.invoke.MethodHandle, java.lang.reflect.Method, java.util.ArrayList, .List, .Map, java.util.concurrent.ConcurrentHashMap, org.springframework.core.convert.TypeDescriptor, org.springframework.expression.BeanResolver, .ConstructorResolver, .EvaluationContext, .MethodFilter, .MethodResolver, .OperatorOverloader, .PropertyAccessor, .TypeComparator, .TypeConverter, .TypeLocator, .TypedValue, org.springframework.lang.Nullable, org.springframework.util.Assert

A powerful and highly configurable EvaluationContext implementation.

This context uses standard implementations of all applicable strategies, based on reflection to resolve properties, methods, and fields. Note, however, that you may need to manually configure a StandardTypeLocator with a specific ClassLoader to ensure that the SpEL expression parser is able to reliably locate user types. See setTypeLocator(TypeLocator) for details.

In addition to support for setting and looking up variables as defined in the EvaluationContext API, StandardEvaluationContext also provides support for registering and looking up functions. The registerFunction(...) methods provide a convenient way to register a function as a Method or a MethodHandle; however, a function can also be registered via setVariable(String, Object) or setVariables(Map). Since functions share a namespace with the variables in this evaluation context, care must be taken to ensure that function names and variable names do not overlap.

For a simpler, builder-style context variant for data-binding purposes, consider using SimpleEvaluationContext instead which allows for opting into several SpEL features as needed by specific use cases.

Authors
Andy Clement, Juergen Hoeller, Sam Brannen, Stephane Nicoll
Since
3.0
See Also
SimpleEvaluationContext, ReflectivePropertyAccessor, ReflectiveConstructorResolver, ReflectiveMethodResolver, StandardTypeLocator, StandardTypeConverter, StandardTypeComparator, StandardOperatorOverloader

Field Summary

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

Constructor Summary

AccessConstructor and Description
public
StandardEvaluationContext()

Create a StandardEvaluationContext with a null root object.

public
StandardEvaluationContext(Object
the root object to use
rootObject
)

Create a StandardEvaluationContext with the given root object.

Method Summary

Modifier and TypeMethod and Description
private static <T> void
addBeforeDefault(List<T> resolvers, T resolver)

public void
public void
public void
public void
applyDelegatesTo(StandardEvaluationContext
the evaluation context to update
evaluationContext
)

Apply the internal delegates of this instance to the specified evaluationContext.

public BeanResolver
getBeanResolver()

Implements org.springframework.expression.EvaluationContext.getBeanResolver.

Return a bean resolver that can look up beans by name.

public List<ConstructorResolver>
getConstructorResolvers()

Implements org.springframework.expression.EvaluationContext.getConstructorResolvers.

Return a list of resolvers that will be asked in turn to locate a constructor.

public List<MethodResolver>
getMethodResolvers()

Implements org.springframework.expression.EvaluationContext.getMethodResolvers.

Return a list of resolvers that will be asked in turn to locate a method.

public OperatorOverloader
getOperatorOverloader()

Implements org.springframework.expression.EvaluationContext.getOperatorOverloader.

Return an operator overloader that may support mathematical operations between more than the standard set of types.

public List<PropertyAccessor>
getPropertyAccessors()

Implements org.springframework.expression.EvaluationContext.getPropertyAccessors.

Return a list of accessors that will be asked in turn to read/write a property.

public TypedValue
getRootObject()

Implements org.springframework.expression.EvaluationContext.getRootObject.

Return the default root context object against which unqualified properties/methods/etc should be resolved.

public TypeComparator
getTypeComparator()

Implements org.springframework.expression.EvaluationContext.getTypeComparator.

Return a type comparator for comparing pairs of objects for equality.

public TypeConverter
getTypeConverter()

Implements org.springframework.expression.EvaluationContext.getTypeConverter.

Return a type converter that can convert (or coerce) a value from one type to another.

public TypeLocator
getTypeLocator()

Implements org.springframework.expression.EvaluationContext.getTypeLocator.

Get the configured TypeLocator that will be used to find types, either by short or fully-qualified name.

private List<ConstructorResolver>
private List<MethodResolver>
private List<PropertyAccessor>
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
registerFunction(String
the name of the function
name
,
Method
the Method to register
method
)

Register the specified Method as a SpEL function.

public void
registerFunction(String
the name of the function
name
,
MethodHandle
the MethodHandle to register
methodHandle
)

Register the specified MethodHandle as a SpEL function.

public void
registerMethodFilter(Class<?>
the type for which the filter should be called
type
,
MethodFilter
a MethodFilter, or null to unregister a filter for the type
filter
)

Register a MethodFilter which will be called during method resolution for the specified type.

public boolean
public boolean
public boolean
public void
public void
public void
public void
public void
public void
setRootObject(Object rootObject, TypeDescriptor typeDescriptor)

public void
setRootObject(Object rootObject)

public void
public void
public void
setTypeLocator(TypeLocator
the TypeLocator to use
typeLocator
)

Set the TypeLocator to use to find types, either by short or fully-qualified name.

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

Implements org.springframework.expression.EvaluationContext.setVariable.

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

public void
setVariables(Map<String, Object>
the names and values of the variables to set
variables
)

Set multiple named variables in this evaluation context to the specified values.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanResolverback to summary
private BeanResolver beanResolver
Annotations
@Nullable
constructorResolversback to summary
private volatile List<ConstructorResolver> constructorResolvers
Annotations
@Nullable
methodResolversback to summary
private volatile List<MethodResolver> methodResolvers
Annotations
@Nullable
operatorOverloaderback to summary
private OperatorOverloader operatorOverloader
propertyAccessorsback to summary
private volatile List<PropertyAccessor> propertyAccessors
Annotations
@Nullable
reflectiveMethodResolverback to summary
private volatile ReflectiveMethodResolver reflectiveMethodResolver
Annotations
@Nullable
rootObjectback to summary
private TypedValue rootObject
typeComparatorback to summary
private TypeComparator typeComparator
typeConverterback to summary
private TypeConverter typeConverter
Annotations
@Nullable
typeLocatorback to summary
private TypeLocator typeLocator
Annotations
@Nullable
variablesback to summary
private final Map<String, Object> variables

Constructor Detail

StandardEvaluationContextback to summary
public StandardEvaluationContext()

Create a StandardEvaluationContext with a null root object.

StandardEvaluationContextback to summary
public StandardEvaluationContext(Object rootObject)

Create a StandardEvaluationContext with the given root object.

Parameters
rootObject:Object

the root object to use

See Also
setRootObject

Method Detail

addBeforeDefaultback to summary
private static <T> void addBeforeDefault(List<T> resolvers, T resolver)
addConstructorResolverback to summary
public void addConstructorResolver(ConstructorResolver resolver)
addMethodResolverback to summary
public void addMethodResolver(MethodResolver resolver)
addPropertyAccessorback to summary
public void addPropertyAccessor(PropertyAccessor accessor)
applyDelegatesToback to summary
public void applyDelegatesTo(StandardEvaluationContext evaluationContext)

Apply the internal delegates of this instance to the specified evaluationContext. Typically invoked right after the new context instance has been created to reuse the delegates. Does not modify the root object or any registered variables or functions.

Parameters
evaluationContext:StandardEvaluationContext

the evaluation context to update

Since
6.1.1
getBeanResolverback to summary
public BeanResolver getBeanResolver()

Implements org.springframework.expression.EvaluationContext.getBeanResolver.

Doc from org.springframework.expression.EvaluationContext.getBeanResolver.

Return a bean resolver that can look up beans by name.

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

Implements org.springframework.expression.EvaluationContext.getConstructorResolvers.

Doc from org.springframework.expression.EvaluationContext.getConstructorResolvers.

Return a list of resolvers that will be asked in turn to locate a constructor.

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

Implements org.springframework.expression.EvaluationContext.getMethodResolvers.

Doc from org.springframework.expression.EvaluationContext.getMethodResolvers.

Return a list of resolvers that will be asked in turn to locate a method.

Annotations
@Override
getOperatorOverloaderback to summary
public OperatorOverloader getOperatorOverloader()

Implements org.springframework.expression.EvaluationContext.getOperatorOverloader.

Doc from org.springframework.expression.EvaluationContext.getOperatorOverloader.

Return an operator overloader that may support mathematical operations between more than the standard set of types.

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

Implements org.springframework.expression.EvaluationContext.getPropertyAccessors.

Doc from org.springframework.expression.EvaluationContext.getPropertyAccessors.

Return a list of accessors that will be asked in turn to read/write a property.

Annotations
@Override
getRootObjectback to summary
public TypedValue getRootObject()

Implements org.springframework.expression.EvaluationContext.getRootObject.

Doc from org.springframework.expression.EvaluationContext.getRootObject.

Return the default root context object against which unqualified properties/methods/etc should be resolved. This can be overridden when evaluating an expression.

Annotations
@Override
getTypeComparatorback to summary
public TypeComparator getTypeComparator()

Implements org.springframework.expression.EvaluationContext.getTypeComparator.

Doc from org.springframework.expression.EvaluationContext.getTypeComparator.

Return a type comparator for comparing pairs of objects for equality.

Annotations
@Override
getTypeConverterback to summary
public TypeConverter getTypeConverter()

Implements org.springframework.expression.EvaluationContext.getTypeConverter.

Doc from org.springframework.expression.EvaluationContext.getTypeConverter.

Return a type converter that can convert (or coerce) a value from one type to another.

Annotations
@Override
getTypeLocatorback to summary
public TypeLocator getTypeLocator()

Implements org.springframework.expression.EvaluationContext.getTypeLocator.

Get the configured TypeLocator that will be used to find types, either by short or fully-qualified name.

See setTypeLocator(TypeLocator) for further details.

Annotations
@Override
See Also
setTypeLocator(TypeLocator)
initConstructorResolversback to summary
private List<ConstructorResolver> initConstructorResolvers()
initMethodResolversback to summary
private List<MethodResolver> initMethodResolvers()
initPropertyAccessorsback to summary
private List<PropertyAccessor> initPropertyAccessors()
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
registerFunctionback to summary
public void registerFunction(String name, Method method)

Register the specified Method as a SpEL function.

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 function

method:Method

the Method to register

See Also
registerFunction(String, MethodHandle)
registerFunctionback to summary
public void registerFunction(String name, MethodHandle methodHandle)

Register the specified MethodHandle as a SpEL function.

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 function

methodHandle:MethodHandle

the MethodHandle to register

Since
6.1
See Also
registerFunction(String, Method)
registerMethodFilterback to summary
public void registerMethodFilter(Class<?> type, MethodFilter filter) throws IllegalStateException

Register a MethodFilter which will be called during method resolution for the specified type.

The MethodFilter may remove methods and/or sort the methods which will then be used by SpEL as the candidates to look through for a match.

Parameters
type:Class<?>

the type for which the filter should be called

filter:MethodFilter

a MethodFilter, or null to unregister a filter for the type

Exceptions
IllegalStateException:
if the ReflectiveMethodResolver is not in use
removeConstructorResolverback to summary
public boolean removeConstructorResolver(ConstructorResolver resolver)
removeMethodResolverback to summary
public boolean removeMethodResolver(MethodResolver methodResolver)
removePropertyAccessorback to summary
public boolean removePropertyAccessor(PropertyAccessor accessor)
setBeanResolverback to summary
public void setBeanResolver(BeanResolver beanResolver)
setConstructorResolversback to summary
public void setConstructorResolvers(List<ConstructorResolver> constructorResolvers)
setMethodResolversback to summary
public void setMethodResolvers(List<MethodResolver> methodResolvers)
setOperatorOverloaderback to summary
public void setOperatorOverloader(OperatorOverloader operatorOverloader)
setPropertyAccessorsback to summary
public void setPropertyAccessors(List<PropertyAccessor> propertyAccessors)
setRootObjectback to summary
public void setRootObject(Object rootObject, TypeDescriptor typeDescriptor)
setRootObjectback to summary
public void setRootObject(Object rootObject)
setTypeComparatorback to summary
public void setTypeComparator(TypeComparator typeComparator)
setTypeConverterback to summary
public void setTypeConverter(TypeConverter typeConverter)
setTypeLocatorback to summary
public void setTypeLocator(TypeLocator typeLocator)

Set the TypeLocator to use to find types, either by short or fully-qualified name.

By default, a StandardTypeLocator will be used.

Note

Even if a StandardTypeLocator is sufficient, you may need to manually configure a StandardTypeLocator with a specific ClassLoader to ensure that the SpEL expression parser is able to reliably locate user types.

Parameters
typeLocator:TypeLocator

the TypeLocator to use

See Also
StandardTypeLocator#StandardTypeLocator(ClassLoader), getTypeLocator()
setVariableback to summary
public void setVariable(String name, Object value)

Implements org.springframework.expression.EvaluationContext.setVariable.

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

If the specified name is null, it will be ignored. If the specified value is null, the named variable will be removed from this evaluation context.

In contrast to assignVariable(String, java.util.function.Supplier), this method should only be invoked programmatically when interacting directly with the EvaluationContext — for example, to provide initial configuration for the 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 to set

value:Object

the value to be placed in the variable

Annotations
@Override
See Also
setVariables(Map), registerFunction(String, Method), registerFunction(String, MethodHandle), lookupVariable(String)
setVariablesback to summary
public void setVariables(Map<String, Object> variables)

Set multiple named variables in this evaluation context to the specified values.

This is a convenience variant of setVariable(String, Object).

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

Parameters
variables:Map<String, Object>

the names and values of the variables to set

See Also
setVariable(String, Object)