Bid Farewell to If-Else: 5 Recommended Java Expression Engines
Bid Farewell to If-Else: 5 Recommended Java Expression Engines
DiebugForeword
When designing forms or workflow engines, we often encounter scenarios requiring expressions or rules to drive business logic. In this article, we review five commonly used Java expression engines that can replace cumbersome if-else blocks, making your code cleaner and more maintainable.
These expression engines are well-known and widely used. Let’s dive in for a quick refresher.
1. Spring Expression Language (SpEL)
Official Documentation: Spring
Expression Language Documentation
Official Examples: Spring
Framework GitHub
Spring Expression Language (SpEL) is a powerful expression language built into the Spring Framework for runtime object querying and manipulation. Key features include:
- Dynamic Querying and Manipulation: Query bean properties, invoke methods, perform arithmetic, and logical operations.
- Spring Integration: Used in Spring Security, Spring Data, and Spring Integration.
- Flexible Syntax: Supports expressions within
#{...}
, e.g.,#{bean.property}
or#{list[0]}
. - Context Awareness: Access Spring beans and application context within expressions.
- Type Conversion: Built-in service for automatic or explicit type conversion.
- Security: Configurable to prevent injection attacks.
Example Usage:
1 | #{myBean.propertyName} |
Utility Class:
1 | public class SpringExpressionUtil { |
2. OGNL (Object-Graph Navigation Language)
Official Documentation: OGNL Language
Guide
Official Examples: OGNL GitHub
OGNL is a powerful expression language for accessing and modifying Java object properties. It is widely used in frameworks like Apache Struts2.
Key Features:
- Object Graph Navigation: Easily access nested
properties (e.g.,
customer.address.street
). - Collection Operations: Filter, project, and
traverse collections (e.g.,
customers.{name}
). - Method Calls: Supports calling methods and creating objects.
- Dynamic Context: Variables and expressions adapt to their runtime context.
Utility Class:
1 | public class OgnlExpressionUtil { |
3. Aviator
Official Documentation: Aviator Documentation
Official Examples: AviatorScript
GitHub
Aviator is a lightweight and high-performance expression engine designed for scenarios requiring dynamic computations at runtime.
Key Features:
- High Performance: Optimized for financial and real-time computing.
- Rich Expression Support: Includes arithmetic, logical, and string operations.
- JIT Compilation: Converts expressions to Java bytecode for faster execution.
- Extensibility: Define custom functions for specialized logic.
- Sandbox Mode: Restrict execution permissions for security.
Utility Class:
1 | public final class AviatorExpressionUtil { |
4. MVEL (MVFLEX Expression Language)
Official Documentation: MVEL Documentation
Official Examples: MVEL GitHub
MVEL2 offers a powerful and flexible way to parse and execute Java expressions. It is particularly effective for scenarios requiring lightweight scripting or embedded template generation.
Key Features:
- Simple Syntax: Concise Java-like syntax for expressions and logic.
- Dynamic and Static Typing: Combines flexibility and safety.
- Template Engine: Supports generating text output via templates.
- Efficient Execution: Optimized execution engine for performance-sensitive tasks.
5. Hutool Expression Engine Facade
Official Documentation: Hutool ExpressionUtil
Hutool provides a unified API for multiple expression engines, including Aviator, Apache Jexl3, MVEL, JfireEL, Rhino, and SpEL. It also supports custom engines via SPI.
Utility Class:
1 | public class HutoolExpressionUtil { |
Conclusion
These five Java expression engines offer robust alternatives to traditional if-else logic. Depending on your use case, such as performance sensitivity, ease of integration, or dynamic capabilities, you can choose the most suitable engine for your needs. Each engine simplifies complex logic, improves maintainability, and enhances readability.