SpEL is an exression language supporting the features of querying and manipulating an object graph at runtime.
There are many expression languages available such as JSP EL, OGNL, MVEL and JBoss EL. SpEL provides some additional features such as method invocation and string templating functionality.
SpEL API
The SpEL API provides many interfaces and classes. They are as follows:
- Expression interface
- SpelExpression class
- ExpressionParser interface
- SpelExpressionParser class
- EvaluationContext interface
- StandardEvaluationContext class
Hello SPEL Example
- import org.springframework.expression.Expression;
- import org.springframework.expression.ExpressionParser;
- import org.springframework.expression.spel.standard.SpelExpressionParser;
-
- public class Test {
- public static void main(String[] args) {
- ExpressionParser parser = new SpelExpressionParser();
-
- Expression exp = parser.parseExpression("'Hello SPEL'");
- String message = (String) exp.getValue();
- System.out.println(message);
-
-
- }
- }
Other SPEL Example
Let's see a lot of useful examples of SPEL. Here, we are assuming all the examples have been written inside the main() method.
Using concat() method with String
- ExpressionParser parser = new SpelExpressionParser();
- Expression exp = parser.parseExpression("'Welcome SPEL'.concat('!')");
- String message = (String) exp.getValue();
- System.out.println(message);
Converting String into byte array
- Expression exp = parser.parseExpression("'Hello World'.bytes");
- byte[] bytes = (byte[]) exp.getValue();
- for(int i=0;i<bytes.length;i++){
- System.out.print(bytes[i]+" ");
- }
Getting length after converting string into bytes
- Expression exp = parser.parseExpression("'Hello World'.bytes.length");
- int length = (Integer) exp.getValue();
- System.out.println(length);
Converting String contents into uppercase letter
- Expression exp = parser.parseExpression("new String('hello world').toUpperCase()");
- String message = exp.getValue(String.class);
- System.out.println(message);
-
- System.out.println(parser.parseExpression("'hello world'.toUpperCase()").getValue());
ReplyDeleteGreat blog. You put Good stuff. All the topics were explained briefly. So quickly understand for me. I am waiting for your next fantastic blog. Thanks for sharing. Any course related details learn...
Php course in chennai
thanks
ReplyDeleteHey, Your post is very informative and helpful for us.
ReplyDeleteIn fact i am looking this type of article from some days.
Thanks a lot to share this informative article.
hardware and networking course in hyderabad