Thursday, April 16, 2015

Spring Expression Language

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html

You can find very descriptive details from the above mention link in the spring docs. The definition include as below..

The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.


I am not going to rewrite what has already being well described with the spring docs, instead i thought of sharing some exceptions which might you experience.

This is about the initial example of "Hello world " as Spring expression.

You can simply create a class and inside main method you can directly run the code. I have just included inside static method.

So below is the code


ExpressionEvaluationWithSEI.java
------------------------------------------------------------------------------------------------------
package com.spel;



import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

/**
 * http://docs.spring.io/spring/docs/current/spring-framework-reference/html/
 * expressions.html
 *
 * Expression Evaluation using Spring’s Expression Interface
 *
 * @author APrasad
 *
 */
public class ExpressionEvaluationWithSEI {

public static void evaluateLiteralStringExpression() {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("'Hello World'");
String message = (String)expression.getValue();
System.out.println("message ="+message);

}

public static void main(String[] args) {
ExpressionEvaluationWithSEI.evaluateLiteralStringExpression();
}
}


------------------------------------------------------------------------------------------------------
This will have simple output

output
=============================================================
message =Hello World
=============================================================


Note : The word "Hello World"  is within the single quotes..... So if any case if you miss the single quotes , then you will probably witness below exception.

change new code

------------------------------------------------------------------------------------------------------
Expression expression = parser.parseExpression("Hello World");
//Note :: I have remove the single quotes around the literal expression Hello World
------------------------------------------------------------------------------------------------------

output
=============================================================
Exception in thread "main" org.springframework.expression.spel.SpelParseException: EL1041E:(pos 6): After parsing a valid expression, there is still more data in the expression: 'World'
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:118)
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:56)
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:1)
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:66)
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:56)
at com.spel.ExpressionEvaluationWithSEI.evaluateLiteralStringExpression(ExpressionEvaluationWithSEI.java:22)
at com.spel.ExpressionEvaluationWithSEI.main(ExpressionEvaluationWithSEI.java:29)

=============================================================



Now let's also check other functionality given with the expression.


ExpressionEvaluationWithSEI.java
------------------------------------------------------------------------------------------------------
package com.spel;



import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

/**
 * http://docs.spring.io/spring/docs/current/spring-framework-reference/html/
 * expressions.html
 *
 * Expression Evaluation using Spring’s Expression Interface
 *
 * @author APrasad
 *
 */
public class ExpressionEvaluationWithSEI {

public static void evaluateLiteralStringExpression() {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("'Hello World'");
String message = (String)expression.getValue();
System.out.println("message ="+message);

//newly added
System.out.println("getExpressionString ="+expression.getExpressionString());
System.out.println("toString ="+expression.toString());
System.out.println("getValueType ="+expression.getValueType());
}

public static void main(String[] args) {
ExpressionEvaluationWithSEI.evaluateLiteralStringExpression();
}
}

------------------------------------------------------------------------------------------------------

output
=============================================================
message =Hello World
getExpressionString ='Hello World'
toString =org.springframework.expression.spel.standard.SpelExpression@18019707
getValueType =class java.lang.String

=============================================================



No comments:

Post a Comment