You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document that "functions are variables" in SpEL evaluation contexts
Although EvaluationContext defines the API for setting and looking up
variables, the internals of the Spring Expression Language (SpEL)
actually provide explicit support for registering functions as
variables.
This is self-evident in the two registerFunction() variants in
StandardEvaluationContext; however, functions can also be registered as
variables when using the SimpleEvaluationContext.
Since custom functions are also viable in use cases involving the
SimpleEvaluationContext, this commit documents that functions may be
registered in a SimpleEvaluationContext via setVariable().
This commit also explicitly documents the "function as a variable"
behavior in the class-level Javadoc for both StandardEvaluationContext
and SimpleEvaluationContext, as well as in the reference manual.
Closesgh-32258
Copy file name to clipboardExpand all lines: spring-expression/src/main/java/org/springframework/expression/spel/support/StandardEvaluationContext.java
+48-8
Original file line number
Diff line number
Diff line change
@@ -48,6 +48,16 @@
48
48
* to reliably locate user types. See {@link #setTypeLocator(TypeLocator)} for
49
49
* details.
50
50
*
51
+
* <p>In addition to support for setting and looking up variables as defined in
52
+
* the {@link EvaluationContext} API, {@code StandardEvaluationContext} also
53
+
* provides support for registering and looking up functions. The
54
+
* {@code registerFunction(...)} methods provide a convenient way to register a
55
+
* function as a {@link Method} or a {@link MethodHandle}; however, a function
56
+
* can also be registered via {@link #setVariable(String, Object)} or
57
+
* {@link #setVariables(Map)}. Since functions share a namespace with the variables
58
+
* in this evaluation context, care must be taken to ensure that function names
59
+
* and variable names do not overlap.
60
+
*
51
61
* <p>For a simpler, builder-style context variant for data-binding purposes,
52
62
* consider using {@link SimpleEvaluationContext} instead which allows for
53
63
* opting into several SpEL features as needed by specific use cases.
@@ -253,6 +263,25 @@ public OperatorOverloader getOperatorOverloader() {
253
263
returnthis.operatorOverloader;
254
264
}
255
265
266
+
/**
267
+
* Set a named variable in this evaluation context to a specified value.
268
+
* <p>If the specified {@code name} is {@code null}, it will be ignored. If
269
+
* the specified {@code value} is {@code null}, the named variable will be
270
+
* removed from this evaluation context.
271
+
* <p>In contrast to {@link #assignVariable(String,java.util.function.Supplier)},
272
+
* this method should only be invoked programmatically when interacting directly
273
+
* with the {@code EvaluationContext} — for example, to provide initial
274
+
* configuration for the context.
275
+
* <p>Note that variables and functions share a common namespace in this
276
+
* evaluation context. See the {@linkplain StandardEvaluationContext
277
+
* class-level documentation} for details.
278
+
* @param name the name of the variable to set
279
+
* @param value the value to be placed in the variable
0 commit comments