Is it possible to use lambda in SpEL inside @ConditionalOnExpression

Is it possible to use lambda in SpEL inside @ConditionalOnExpression

Problem Description:

Is it possible to use lambda for@ConditionalOnExpression in spring expression language (SpEL)?
I have created the following SpEL condition via collection-selection which works fine for me. But I am not satisfied how it looks like.

@ConditionalOnExpression("#{T(java.util.Arrays).asList('${app.sync.white-list}').?[#this.toLowerCase() == 'all' or #this.toLowerCase() == 'phone'].size() > 0}")

In Java the logic for the condition is the following:

Arrays.stream(a).anyMatch(x -> "all".equalsIgnoreCase(x) || "phone".equalsIgnoreCase(x))

I wonder if it is possible to make that SpEL condition look nicer via using anyMatch and lambda?

Solution – 1

There is no reason to over-complicate SpEL: just have that logic in some static method and use it via T operator: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#expressions-types. Or you can register it as a function in the EvaluationContext. See same doc.

The point is that SpEL is an interpreted language: the longer and and complex expression – the slower calculation.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject