Overview of Common Java Expression Engines and Utility Wrappers
The article surveys popular Java expression engines—including Spring Expression Language, OGNL, Aviator, and MVEL2—detailing their key features and providing lightweight utility wrappers, and highlights Hutool’s unified façade that can encapsulate these engines to simplify dynamic rule evaluation in backend applications.
This article introduces several widely used Java expression engines that are often employed in form or workflow design to evaluate dynamic rules.
Spring Expression Language (SpEL)
Official documentation: https://docs.spring.io/spring-framework/reference/core/expressions.html
Key features include dynamic queries, integration with Spring modules, basic syntax using #{...} , context awareness, type conversion, and security considerations.
Examples:
Access bean property: #{myBean.propertyName}
Method call: #{myBean.myMethod(args)}
Conditional operator: #{condition ? trueValue : falseValue}
List/array access: #{myList[0]}
Arithmetic: #{2+3}
Utility class:
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);
}
}OGNL
Official documentation: https://ognl.orphan.software/language-guide
OGNL provides simple property navigation, chain navigation, collection operations, method calls, constructors, logical operators, and variable assignment, but requires careful security handling.
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;
}
}Aviator
Official site: http://fnil.net/aviator/
Aviator is a lightweight, high‑performance Java expression engine that supports arithmetic, logical, ternary, variable definition, function calls, sandbox mode, JIT compilation and extensibility.
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;
}
}MVEL2
Official documentation: https://juejin.cn/post/mvel.documentnode.com/
MVEL2 offers mixed dynamic/static typing, concise Java‑like syntax, property access, method invocation, control flow, template engine, integration points and performance optimizations.
Hutool Expression Facade
Documentation: https://doc.hutool.cn/pages/ExpressionUtil/#%E4%BB%8B%E7%BB%8D
Since version 5.5.0, Hutool provides a unified façade that can wrap multiple engines (Aviator, Jexl3, MVEL, JfireEL, Rhino, SpEL). Example utility:
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;
}
}Conclusion
The article summarizes the most common expression engine components and shows how Hutool’s façade can unify their usage, providing a convenient toolset for typical backend development scenarios.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.