Let's say we have the following code fragment that populates the list with a few primes and defines it as a variable in the SpEL context:
// create an array of integers List<Integer> primes = new ArrayList<Integer>(); primes.addAll(Arrays.asList(2,3,5,7,11,13,17)); // create parser and set variable 'primes' as the array of integers ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.setVariable("primes", primes);
Now, if we want to narrow the list and get all the primes that are> 10, can we use the selection operator ?[] . The statement will be as follows:
Expression expression = parser.parseExpression("#primes.?[#this>10]"); List<Integer> primesGreaterThanTen = (List<Integer>) expression.getValue(ctx);
As you can see, the expression to be evaluated is #primes.?[#this > 10] . How it works?
#primes belongs to the primes list.- The selection operator
?[] Corresponds to each object i in the primes list, which is not null and meets the criteria indicated in brackets. In our example, the criteria #this > 10 . #this refers to the current evaluation object, which in our example will be the object from the list that is currently being checked for non-null and> 10.
The result of the evaluation will be a list containing:
[11, 13, 17]
The SpEL context may have the #root variable. Let this simple class:
public class SomeCustomObject { public int stringLength(String input) { if (input == null) return 0; return input.length(); } }
and define an instance of our SomeCustomObject as the #root variable.
SomeCustomObject someObject = new SomeCustomObject(); context.setRootObject(someObject);
This will create the variable someObject root object for the SpEL context.
A simple example with the #root variable.
String name = "kocko"; ctx.setVariable("name", kocko); String statement = "#root.stringLength(#kocko) == 5"; Expression expression = parser.parseExpression(statement); boolean result = expression.getValue(context, Boolean.class);
The result variable will evaluate to true .
What is the power of #root ?
Through a single root object, you can expose your expressions to a comprehensive custom environment, such as custom methods and / or variables .
Additional Information: