Backend Development 16 min read

Overview of Common Java Expression Engines and Utility Classes

This article provides a comprehensive overview of popular Java expression engines—including Spring EL, OGNL, Aviator, MVEL2—and demonstrates how the Hutool facade unifies them with utility classes, offering developers a clear guide to choose and integrate the right engine for their backend applications.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Overview of Common Java Expression Engines and Utility Classes

When designing forms or workflow engines, developers often need various expressions or rules to drive business processes. This article provides a comprehensive overview of commonly used Java expression engines.

Spring EL

Official resources: https://docs.spring.io/spring-framework/reference/core/expressions.html and https://github.com/spring-projects/spring-framework/tree/master/spring-expression .

Spring Expression Language (SpEL) offers dynamic data handling, deep Spring integration, unique syntax (e.g., #{...}), context awareness, type conversion, and security mechanisms.

Example

// 访问 Bean 属性
#{myBean.propertyName}
// 方法调用
#{myBean.myMethod(args)}
// 三元运算符
#{condition ? trueValue : falseValue}
// 列表和数组访问
#{myList[0]}
// 算术运算
#{2 + 3}

Utility class

public class SpringExpressionUtil {
    private static final SpelExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();
    private SpringExpressionUtil() {}
    /**
     * Evaluate a Spring EL expression against the provided root object.
     * @param rootObject the root object for expression evaluation
     * @param expressionString the Spring EL expression to evaluate
     * @param returnType the expected return type
     * @return the result of the expression evaluation
     */
    public static
T evaluateExpression(Map
rootObject, String expressionString, Class
returnType) {
        StandardEvaluationContext context = new StandardEvaluationContext(rootObject);
        rootObject.forEach(context::setVariable);
        return EXPRESSION_PARSER.parseExpression(expressionString).getValue(context, returnType);
    }
    public static void main(String[] args) {
        Map
map = new HashMap<>();
        map.put("name", "lybgeek");
        map.put("hello", "world");
        System.out.println(evaluateExpression(map, "#root.get('name')", String.class));
    }
}

OGNL

Official resources: https://ognl.orphan.software/language-guide and https://github.com/orphan-oss/ognl .

OGNL provides concise syntax, chain navigation, collection operations, context sensitivity, method/constructor support, logical operators, and variable assignment with security considerations.

Utility class

public class OgnlExpressionUtil {
    private OgnlExpressionUtil() {}
    /**
     * Evaluate an OGNL expression against the provided root object.
     * @param rootObject the root object for expression evaluation
     * @param expressionString the OGNL expression to evaluate
     * @param returnType the expected return type
     * @return the result of the expression evaluation
     */
    public static
T evaluateExpression(Map
rootObject, String expressionString, Class
returnType) {
        Object value = OgnlCache.getValue(expressionString, rootObject);
        if (value != null && value.getClass().isAssignableFrom(returnType)) {
            return (T) value;
        }
        return null;
    }
    public static void main(String[] args) {
        Map
map = new HashMap<>();
        map.put("name", "lybgeek");
        map.put("hello", "world");
        System.out.println(OgnlExpressionUtil.evaluateExpression(map, "#root.name", String.class));
        System.out.println(SpringExpressionUtil.evaluateExpression(map, "#root.get('hello')", String.class));
    }
}

Aviator

Official resources: http://fnil.net/aviator/ and https://github.com/killme2008/aviatorscript .

Aviator is a lightweight, high‑performance Java expression engine designed for dynamic calculations, featuring JIT compilation, sandboxing, dynamic script execution, seamless data binding, and extensibility through custom functions.

Utility class

public final class AviatorExpressionUtil {
    private AviatorExpressionUtil() {}
    /**
     * Execute an Aviator expression and return the result.
     * @param env the context environment containing variables and functions
     * @param expression the Aviator expression string
     * @param returnType the expected return type
     * @return the result of the expression execution
     */
    public static
T evaluateExpression(Map
env, String expression, Class
returnType) {
        Object value = AviatorEvaluator.execute(expression, env);
        if (value != null && value.getClass().isAssignableFrom(returnType)) {
            return (T) value;
        }
        return null;
    }
    public static void main(String[] args) {
        Map
map = new HashMap<>();
        map.put("name", "lybgeek");
        map.put("hello", "world");
        Map
env = new HashMap<>();
        env.put("root", map);
        System.out.println(evaluateExpression(env, "#root.name", String.class));
    }
}

MVEL2

Official resources: https://juejin.cn/post/mvel.documentnode.com/ and https://github.com/mvel/mvel .

MVEL2 offers mixed dynamic/static typing, concise Java‑like syntax, easy property and method access, rich control‑flow statements (if, else, switch, for, while), a powerful template engine, flexible variable and function definitions, and performance optimizations.

Hutool Expression Facade

Hutool provides a unified API that abstracts multiple expression engines (Aviator, Apache Jexl3, MVEL, JfireEL, Rhino, Spring Expression Language) and supports SPI for custom extensions, simplifying engine selection and integration.

Utility class

public class HutoolExpressionUtil {
    private HutoolExpressionUtil() {}
    /**
     * Execute an expression and return the result.
     * @param expression the expression string
     * @param variables a map of variable names to values
     * @param returnType the expected return type
     * @return the result of the expression evaluation
     */
    public static
T evaluateExpression(Map
variables, String expression, Class
returnType) {
        try {
            Object value = ExpressionUtil.eval(expression, variables);
            if (value != null && value.getClass().isAssignableFrom(returnType)) {
                return (T) value;
            }
        } catch (Exception e) {
            throw new RuntimeException("Error executing expression: " + expression, e);
        }
        return null;
    }
    public static void main(String[] args) {
        Map
map = new HashMap<>();
        map.put("name", "lybgeek");
        map.put("hello", "world");
        Map
variables = new HashMap<>();
        variables.put("root", map);
        System.out.println(evaluateExpression(variables, "root.name", String.class));
    }
}

Conclusion

The article reviews several popular Java expression engines, highlighting their distinct features and suitable scenarios, and demonstrates how Hutool’s facade offers a consistent interface that greatly simplifies development and integration of these engines.

Javaaviatorexpression-engineHutoolmvelOGNLspring-el
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.