Thursday, March 29, 2012

SpEL: I am loving it

Sorry, as usual, got distracted from cucumber to SpEL (Spring Expression Language). I was discussing about SpEL with a friend other day, so thought of reading little bit more about it.

I should accept I am simply blown away and not sure why I was not using it all the while. Starting from “literal expression”-to-“expression templating”. All the expression usage will boost JAVA developer’s productivity.

My favorites so are:
1) Elvis Operator: The Elvis operator is a shortening of the ternary operator syntax. i.e. in ternary operator we write code like below

String name = "Elvis Presley";
String displayName = name != null ? name : "Unknown";

Using Elvis Operator it can be written like

ExpressionParser parser = new SpelExpressionParser();
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
String name = parser.parseExpression("Name?:'Elvis Presley'")
.getValue(context, String.class); System.out.println(name); // Nikola Tesla

The Elvis operator can be used to apply default values in expressions

2) Safe Navigation operator: The Safe Navigation operator is used to avoid a NullPointerException. Here are some example code from Spring Documentation.

ExpressionParser parser = new SpelExpressionParser();  
Inventor tesla = new Inventor("Nikola Tesla", "Serbian"); tesla.setPlaceOfBirth(new PlaceOfBirth("Smiljan"));  
StandardEvaluationContext context = new StandardEvaluationContext(tesla);  
String city= parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class); 
System.out.println(city); // Smiljan  
tesla.setPlaceOfBirth(null);  city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class);  
System.out.println(city); // null - does not throw NullPointerException!!!

3) Collection Selection: This is my favorite, don’t get me wrong all of the ones I have listed are great but this is super. Selection is a powerful expression language feature that allows you to transform some source collection into another by selecting from its entries. Selection is possible upon both lists and maps

Selection uses the syntax ?[selectionExpression]. This will filter the collection and return a new collection containing a subset of the original elements. For example, selection would allow us to easily get a list of Serbian inventors.

List list = (List)parser.parseExpression           
                ("Members.?[Nationality == 'Serbian']")

4) Collection Projection: Projection allows a collection to drive the evaluation of a sub-expression and the result is a new collection. The syntax for projection is ![projectionExpression]. Most easily understood by example, suppose we have a list of inventors but want the list of cities where they were born. Effectively we want to evaluate '' for every entry in the inventor list. Using projection:

// returns [ 'Smiljan', 'Idvor' ]
List placesOfBirth = (List)parser.parseExpression("Members.![]");



  1. Looks a lot like Groovy, doesn't it? :)

  2. Absolutely Sir.. I believe it is inspired by Groovy language only..