Spring EL - 调用静态接口方法

Spring EL - Invoking static interface method

有没有办法在 SpEL 中调用静态接口方法? 例如:

T(java.util.stream.IntStream).of(new Integer[]{1,2,3}).sum()

当我 运行 时,我得到这个错误:Problem locating method of on type class java.lang.Class

需要"T(java.util.stream.IntStream).of(new int[]{1,2,3}).sum()".

(int[] 不是 Integer[]).

问题是有 2 个 of() 方法,需要从 Integer[] 进行转换,所以你得到

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1033E: Method call of 'of' is ambiguous, supported type conversions allow multiple variants to match

您未能向我们展示更多堆栈跟踪:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1033E: Method call of 'of' is ambiguous, supported type conversions allow multiple variants to match
at org.springframework.expression.spel.support.ReflectiveMethodResolver.resolve(ReflectiveMethodResolver.java:211)

无法通过反射在运行时解析出合适的方法,因为IntStream中有多个of()方法。

这对我有用:

ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("T(java.util.stream.IntStream).of(1,2,3).sum()");

assertThat(expression.getValue()).isEqualTo(6);