Backend Development 12 min read

A Comparative Overview of Java Expression Engines: Spring EL, OGNL, Aviator, MVEL, and Hutool

This article introduces and compares several Java expression engines—including Spring Expression Language, OGNL, Aviator, MVEL, and Hutool's expression facade—detailing their key features, typical use cases, security considerations, and providing utility class examples with code snippets for each engine.

Architecture Digest
Architecture Digest
Architecture Digest
A Comparative Overview of Java Expression Engines: Spring EL, OGNL, Aviator, MVEL, and Hutool

Expression engines allow dynamic evaluation of expressions in Java applications, enabling flexible business logic, data binding, and runtime calculations.

Spring Expression Language (SpEL) is integrated into the Spring framework, supporting dynamic queries, bean access, type conversion, and security considerations. Example usage: #{myBean.propertyName} , #{myBean.myMethod(args)} , #{condition ? trueValue : falseValue} . A utility class is provided:

public class SpringExpressionUtil {
    private static final SpelExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();
    private SpringExpressionUtil() {}
    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 (Object‑Graph Navigation Language) offers simple property navigation, collection handling, method invocation, and variable assignment, commonly used in Apache Struts2. Example utility class:

public class OgnlExpressionUtil {
    private OgnlExpressionUtil() {}
    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(evaluateExpression(map,"#root.name",String.class));
        System.out.println(SpringExpressionUtil.evaluateExpression(map,"#root.get('hello')",String.class));
    }
}

Aviator is a lightweight high‑performance Java expression engine with JIT compilation, sandbox mode, and extensive operator support. Example utility class:

public final class AviatorExpressionUtil {
    private AviatorExpressionUtil() {}
    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 provides a flexible expression language with dynamic/static typing, concise syntax, control‑flow statements, and a template engine, making it suitable for embedding scripts in Java applications.

Hutool Expression Facade unifies multiple engines (Aviator, Apache JEXL3, MVEL, JfireEL, Rhino, SpEL) under a common API and supports custom extensions via SPI. Example utility class:

public class HutoolExpressionUtil {
    private HutoolExpressionUtil() {}
    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));
    }
}

The article provides official documentation links, highlights each engine's strengths and security considerations, and supplies ready‑to‑use utility classes, helping developers select the appropriate expression engine for their backend projects.

JavaaviatorHutoolmvelOGNLspring-elExpression Language
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

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.