@@ -181,23 +181,30 @@ create a boolean condition:
181
181
=== `EvaluationContext`
182
182
183
183
The interface `EvaluationContext` is used when evaluating an expression to resolve
184
- properties, methods, fields, and to help perform type conversion. The out-of-the-box
185
- implementations, `SimpleEvalutationContext` and `StandardEvaluationContext`, use
186
- reflection to manipulate the object, caching `java.lang.reflect.Method`,
187
- `java.lang.reflect.Field`, and `java.lang.reflect.Constructor` instances for increased
188
- performance.
189
-
190
- `SimpleEvaluationContext` exposes a subset of essential SpEL language features and
191
- configuration options. Certain categories of expressions, do not require the full extent
192
- of the SpEL language syntax and arguably should be meaningfully restricted. Examples
184
+ properties, methods, fields, and to help perform type conversion. There are two
185
+ out-of-the-box implementations.
186
+
187
+ * `SimpleEvaluationContext` -- exposes a subset of essential SpEL language features and
188
+ configuration options, for categories of expressions that do not require the full extent
189
+ of the SpEL language syntax and should be meaningfully restricted. Examples
193
190
include but are not limited to data binding expressions, property-based filters, and
194
- others. To effect, `SimpleEvaluationContext` supports a subset of the SpEL language syntax
195
- that excludes references to Java types, constructors, and bean references.
191
+ others.
196
192
197
- `StandardEvaluationContext` exposes the full set of SpEL language features and
193
+ * `StandardEvaluationContext` -- exposes the full set of SpEL language features and
198
194
configuration options. You may use it to specify a default root object, and to configure
199
195
every available evaluation-related strategy.
200
196
197
+ `SimpleEvaluationContext` is designed to support only a subset of the SpEL language syntax.
198
+ It excludes Java type references, constructors, and bean references. It also requires
199
+ explicit choosing the level of support for properties and methods in expressions.
200
+ By default, the `create()` static factory method enables only read access to properties.
201
+ You can also obtain a builder to configure the exact level of support needed, targeting
202
+ one of, or some combination of the following:
203
+
204
+ . Custom {@code PropertyAccessor} only (no reflection).
205
+ . Data binding properties for read-only access.
206
+ . Data binding properties for read and write.
207
+
201
208
202
209
[[expressions-type-conversion]]
203
210
==== Type conversion
@@ -225,7 +232,7 @@ being placed in it. A simple example:
225
232
Simple simple = new Simple();
226
233
simple.booleanList.add(true);
227
234
228
- SimpleEvaluationContext context = new SimpleEvaluationContext();
235
+ SimpleEvaluationContext context = SimpleEvaluationContext().create ();
229
236
230
237
// false is passed in here as a string. SpEL and the conversion service will
231
238
// correctly recognize that it needs to be a Boolean and convert it
@@ -602,7 +609,7 @@ arrays and lists are obtained using square bracket notation.
602
609
[subs="verbatim,quotes"]
603
610
----
604
611
ExpressionParser parser = new SpelExpressionParser();
605
- SimpleEvaluationContext context = new SimpleEvaluationContext();
612
+ SimpleEvaluationContext context = SimpleEvaluationContext.create ();
606
613
607
614
// Inventions Array
608
615
@@ -885,7 +892,7 @@ done within a call to `setValue` but can also be done inside a call to `getValue
885
892
[subs="verbatim,quotes"]
886
893
----
887
894
Inventor inventor = new Inventor();
888
- SimpleEvaluationContext context = new SimpleEvaluationContext();
895
+ SimpleEvaluationContext context = SimpleEvaluationContext.create ();
889
896
890
897
parser.parseExpression("Name").setValue(context, inventor, "Alexander Seovic2");
891
898
@@ -953,7 +960,7 @@ are set using the method setVariable on `EvaluationContext` implementations.
953
960
[subs="verbatim,quotes"]
954
961
----
955
962
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
956
- SimpleEvaluationContext context = new SimpleEvaluationContext();
963
+ SimpleEvaluationContext context = SimpleEvaluationContext.create ();
957
964
context.setVariable("newName", "Mike Tesla");
958
965
959
966
parser.parseExpression("Name = #newName").getValue(context, tesla);
@@ -979,7 +986,7 @@ an expression are evaluated, #root always refers to the root.
979
986
980
987
// create parser and set variable 'primes' as the array of integers
981
988
ExpressionParser parser = new SpelExpressionParser();
982
- SimpleEvaluationContext context = new SimpleEvaluationContext();
989
+ SimpleEvaluationContext context = SimpleEvaluationContext.create ();
983
990
context.setVariable("primes",primes);
984
991
985
992
// all prime numbers > 10 from the list (using selection ?{...})
@@ -1001,7 +1008,7 @@ expression string. The function is registered through the `EvaluationContext`.
1001
1008
----
1002
1009
Method method = ...;
1003
1010
1004
- SimpleEvaluationContext context = new SimpleEvaluationContext();
1011
+ SimpleEvaluationContext context = SimpleEvaluationContext.create ();
1005
1012
context.setVariable("myFunction", method);
1006
1013
----
1007
1014
@@ -1028,7 +1035,7 @@ The above method can then be registered and used as follows:
1028
1035
[subs="verbatim,quotes"]
1029
1036
----
1030
1037
ExpressionParser parser = new SpelExpressionParser();
1031
- SimpleEvaluationContext context = new SimpleEvaluationContext();
1038
+ SimpleEvaluationContext context = SimpleEvaluationContext.create ();
1032
1039
1033
1040
context.setVariable("reverseString",
1034
1041
StringUtils.class.getDeclaredMethod("reverseString", String.class));
@@ -1049,7 +1056,7 @@ lookup beans from an expression using the (@) symbol.
1049
1056
[subs="verbatim,quotes"]
1050
1057
----
1051
1058
ExpressionParser parser = new SpelExpressionParser();
1052
- StandardEvaluationContext context = new StandardEvaluationContext();
1059
+ StandardEvaluationContext context = StandardEvaluationContext.create ();
1053
1060
context.setBeanResolver(new MyBeanResolver());
1054
1061
1055
1062
// This will end up calling resolve(context,"foo") on MyBeanResolver during evaluation
@@ -1062,7 +1069,7 @@ To access a factory bean itself, the bean name should instead be prefixed with a
1062
1069
[subs="verbatim,quotes"]
1063
1070
----
1064
1071
ExpressionParser parser = new SpelExpressionParser();
1065
- StandardEvaluationContext context = new StandardEvaluationContext();
1072
+ StandardEvaluationContext context = StandardEvaluationContext.create ();
1066
1073
context.setBeanResolver(new MyBeanResolver());
1067
1074
1068
1075
// This will end up calling resolve(context,"&foo") on MyBeanResolver during evaluation
@@ -1140,7 +1147,7 @@ Here is a more complex example.
1140
1147
ExpressionParser parser = new SpelExpressionParser();
1141
1148
1142
1149
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
1143
- SimpleEvaluationContext context = new SimpleEvaluationContext();
1150
+ SimpleEvaluationContext context = SimpleEvaluationContext.create ();
1144
1151
1145
1152
String name = parser.parseExpression("Name?:'Elvis Presley'").getValue(context, tesla, String.class);
1146
1153
@@ -1172,7 +1179,7 @@ safe navigation operator will simply return null instead of throwing an exceptio
1172
1179
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
1173
1180
tesla.setPlaceOfBirth(new PlaceOfBirth("Smiljan"));
1174
1181
1175
- SimpleEvaluationContext context = new SimpleEvaluationContext();
1182
+ SimpleEvaluationContext context = SimpleEvaluationContext.create ();
1176
1183
1177
1184
String city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, tesla, String.class);
1178
1185
System.out.println(city); // Smiljan
@@ -1240,6 +1247,7 @@ first or the last value. To obtain the first entry matching the selection the sy
1240
1247
1241
1248
[[expressions-collection-projection]]
1242
1249
=== Collection Projection
1250
+
1243
1251
Projection allows a collection to drive the evaluation of a sub-expression and the
1244
1252
result is a new collection. The syntax for projection is `![projectionExpression]`. Most
1245
1253
easily understood by example, suppose we have a list of inventors but want the list of
@@ -1262,6 +1270,7 @@ expression against each map entry.
1262
1270
1263
1271
[[expressions-templating]]
1264
1272
=== Expression templating
1273
+
1265
1274
Expression templates allow a mixing of literal text with one or more evaluation blocks.
1266
1275
Each evaluation block is delimited with prefix and suffix characters that you can
1267
1276
define, a common choice is to use `#{ }` as the delimiters. For example,
@@ -1307,6 +1316,7 @@ The definition of `TemplateParserContext` is shown below.
1307
1316
1308
1317
[[expressions-example-classes]]
1309
1318
== Classes used in the examples
1319
+
1310
1320
Inventor.java
1311
1321
1312
1322
[source,java,indent=0]
0 commit comments