SpelHelper provides additional functionalities to work with Spring Expression Language (SpEL).
SpelHelper is available under GNU Lesser General Public License (GNU LGPL).
To use SpelHelper with Maven, add the following snippet in the dependencies section of your project's POM file:
<dependency> <groupId>net.abhinavsarkar</groupId> <artifactId>SpelHelper</artifactId> <version>1.1</version> </dependency>
The addition functionalities provided are:
- Implicit methods
- Implicit properties
- Simplified extension functions
- Simplified constructors
Implicit methods allow one to registers methods with SpelHelper and attach them to particular classes. After that, when that method is called on an object of that particular class inside a SpEL expression, SpelHelper redirects the method call to the registered method.
Example: ImplicitMethods#sorted(List) method is automatically registered by SpelHelper. The class that the method should be invoked for is the type of the first parameter of the method. In this case, the class is java.util.List.
So when an expression like
"#list(1,4,2).sorted()" is evaluated, the
ImplicitMethods#sorted(List) method is invoked with the list as its
first parameter and its return value is used in further evaluation of the
Implicit properties allow one to treat no argument methods of an object as properties of the object. SpelHelper intercepts the property resolution of SpEL and if the property name is same as some no-arg method of the target object then it invokes the method on the object and provides its return value as the property value for further evaluation of the expression.
Example: Using implicit properties, the example of implicit methods can be
"#list(1,4,2).sorted" - dropping the parens - and it will return
the same value as the last example.
Implicit property resolution considers both the actual methods of the object and the implicit methods registered on the object's class.
Simplified extension functions
SpEL allows to register extension function on the context by providing a
name and a java.lang.reflect.Method object. SpelHelper simplifies this by taking a class
and registering all the
public static methods of the class which do not
void return type. The methods are registered by their simple name.
Example: All the methods of ExtensionFunctions class are automatically
registered by SpelHelper. Hence the method ExtensionFunctions#list(Object...)
can be called from inside a SpEL expression using the function call syntax:
SpEL allows calling constructors from inside a SpEL expression using the
new operator. But they have to be called with their full name like:
"new org.example.Foo('bar')". SpelHelper simplifies this by taking a class
and registering all its public constructors to the SpEL context by their
Example: After registering the
org.example.Foo class with SpelHelper, its
constructor can be called from inside a SpEL expression by:
In addition to all the above functionalities, SpelHelper automatically registers some extension functions and implicit methods which are always available in the SpEL expressions evaluated through SpelHelper. See ExtensionFunctions and ImplicitMethods for further details.
For more details see the API Javadocs.