Is it possible for stream or forEach on list in SpEL? e.g.
是否可以在SpEL中列出stream或forEach?例如
List<String> x = new LinkedList<>(Arrays.asList("A","AAB"));
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(x);
parser.parseExpression("x.stream().map(x -> x.replaceAll(\"A\", \"B\")).collect(Collectors.toList())").getValue(context))
1 个解决方案
#1
7
SpEL is not Java, it's a different language; the acronym stands for Spring Expression Language.
SpEL不是Java,它是一种不同的语言;首字母缩写词代表Spring Expression Language。
It doesn't understand Java8 lambdas so can't parse x -> ...
.
它不懂Java8 lambdas所以无法解析x - > ....
Also, static methods are invoked with the T operator.
此外,使用T运算符调用静态方法。
So, this works...
所以,这有效......
List<String> x = new LinkedList<>(Arrays.asList("A","AAB"));
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("stream().collect(T(java.util.stream.Collectors).toList())");
System.out.println(expression.getValue(x));
(but it's not very useful).
(但它不是很有用)。
You can use streams, but only with simple methods that don't take lambdas...
你可以使用流,但只能使用不带lambdas的简单方法...
Expression expression = parser.parseExpression("stream().findFirst().get()");
Expression expression = parser.parseExpression("stream().count()");
or
List<String> x = new LinkedList<>(Arrays.asList("A","AAB", "A"));
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("stream().distinct().collect(T(java.util.stream.Collectors).toList())");
System.out.println(expression.getValue(x));
etc
EDIT
You can, however, register lambdas as SpEL #functions
, so this works fine...
但是,你可以将lambdas注册为SpEL #functions,所以这很好......
public class So48840190Application {
public static void main(String[] args) throws Exception {
List<String> x = new LinkedList<>(Arrays.asList("A","AAB", "A"));
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ec = new StandardEvaluationContext();
ec.registerFunction("aToB", So48840190Application.class.getMethod("aToB"));
Expression expression = parser.parseExpression(
"stream().map(#aToB()).collect(T(java.util.stream.Collectors).toList())");
System.out.println(expression.getValue(ec, x));
}
public static Function<String, String> aToB() {
return s -> s.replaceAll("A", "B");
}
}
and
[B, BBB, B]
EDIT2
Or, more generally...
或者,更一般地......
public class So48840190Application {
public static void main(String[] args) throws Exception {
List<String> x = new LinkedList<>(Arrays.asList("A","AAB", "A"));
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ec = new StandardEvaluationContext();
ec.registerFunction("replaceAll",
So48840190Application.class.getMethod("replaceAll", String.class, String.class));
ec.registerFunction("toLowerCase",
So48840190Application.class.getMethod("toLowerCase"));
Expression expression = parser.parseExpression(
"stream().map(#replaceAll('A', 'B')).map(#toLowerCase()).collect(T(java.util.stream.Collectors).toList())");
System.out.println(expression.getValue(ec, x));
}
public static Function<String, String> replaceAll(String from, String to) {
return s -> s.replaceAll(from, to);
}
public static Function<String, String> toLowerCase() {
return String::toLowerCase;
}
}
#1
7
SpEL is not Java, it's a different language; the acronym stands for Spring Expression Language.
SpEL不是Java,它是一种不同的语言;首字母缩写词代表Spring Expression Language。
It doesn't understand Java8 lambdas so can't parse x -> ...
.
它不懂Java8 lambdas所以无法解析x - > ....
Also, static methods are invoked with the T operator.
此外,使用T运算符调用静态方法。
So, this works...
所以,这有效......
List<String> x = new LinkedList<>(Arrays.asList("A","AAB"));
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("stream().collect(T(java.util.stream.Collectors).toList())");
System.out.println(expression.getValue(x));
(but it's not very useful).
(但它不是很有用)。
You can use streams, but only with simple methods that don't take lambdas...
你可以使用流,但只能使用不带lambdas的简单方法...
Expression expression = parser.parseExpression("stream().findFirst().get()");
Expression expression = parser.parseExpression("stream().count()");
or
List<String> x = new LinkedList<>(Arrays.asList("A","AAB", "A"));
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("stream().distinct().collect(T(java.util.stream.Collectors).toList())");
System.out.println(expression.getValue(x));
etc
EDIT
You can, however, register lambdas as SpEL #functions
, so this works fine...
但是,你可以将lambdas注册为SpEL #functions,所以这很好......
public class So48840190Application {
public static void main(String[] args) throws Exception {
List<String> x = new LinkedList<>(Arrays.asList("A","AAB", "A"));
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ec = new StandardEvaluationContext();
ec.registerFunction("aToB", So48840190Application.class.getMethod("aToB"));
Expression expression = parser.parseExpression(
"stream().map(#aToB()).collect(T(java.util.stream.Collectors).toList())");
System.out.println(expression.getValue(ec, x));
}
public static Function<String, String> aToB() {
return s -> s.replaceAll("A", "B");
}
}
and
[B, BBB, B]
EDIT2
Or, more generally...
或者,更一般地......
public class So48840190Application {
public static void main(String[] args) throws Exception {
List<String> x = new LinkedList<>(Arrays.asList("A","AAB", "A"));
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ec = new StandardEvaluationContext();
ec.registerFunction("replaceAll",
So48840190Application.class.getMethod("replaceAll", String.class, String.class));
ec.registerFunction("toLowerCase",
So48840190Application.class.getMethod("toLowerCase"));
Expression expression = parser.parseExpression(
"stream().map(#replaceAll('A', 'B')).map(#toLowerCase()).collect(T(java.util.stream.Collectors).toList())");
System.out.println(expression.getValue(ec, x));
}
public static Function<String, String> replaceAll(String from, String to) {
return s -> s.replaceAll(from, to);
}
public static Function<String, String> toLowerCase() {
return String::toLowerCase;
}
}