Class SimpleEvaluationContext

java.lang.Object
org.springframework.expression.spel.support.SimpleEvaluationContext
All Implemented Interfaces:
EvaluationContext

public final class SimpleEvaluationContext extends Object implements EvaluationContext
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 — for example, 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 data binding in SpEL expressions:

  • Data binding for read-only access
  • Data binding for read and write access
  • A custom PropertyAccessor (typically not reflection-based), potentially combined with a DataBindingPropertyAccessor

Conveniently, forReadOnlyDataBinding() enables read-only access to properties via DataBindingPropertyAccessor. Similarly, forReadWriteDataBinding() enables read and write access to properties. Alternatively, configure custom accessors via forPropertyAccessors(PropertyAccessor...), consider disabling assignment (recommended), and optionally 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 predefined Expression with both an EvaluationContext and a root object as arguments: 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.

WARNING: Although SimpleEvaluationContext restricts the SpEL language to a subset of its features, that restriction is provided on a best-effort basis and does not guarantee that evaluation of an expression is safe; SimpleEvaluationContext must not be considered safe for evaluating a SpEL expression obtained from an untrusted source. This responsibility extends to property accessors: restricting a SimpleEvaluationContext to read-only data binding governs only whether assignment to a property is permitted and does not verify that reading a property is free of side effects. See the Security Considerations and Object Design sections of the Spring Framework reference documentation, as well as the class-level documentation for ReflectivePropertyAccessor and DataBindingPropertyAccessor, for details.

Because a parsed Expression may cache accessor and executor state resolved against a particular EvaluationContext configuration, a parsed Expression must never be evaluated first against a StandardEvaluationContext (or any other, less restrictive, EvaluationContext) and later against a SimpleEvaluationContext. If the same expression string must be evaluated under both kinds of contexts, parse it into two distinct Expression instances instead. See Expression for further details on this lifecycle contract.

Since:
4.3.15
Author:
Rossen Stoyanchev, Juergen Hoeller, Sam Brannen
See Also: