Home  >  Spring Core

Spring Expression Language (SpEL) Examples

By Arvind Rai, October 05, 2013
Spring Expression Language (SpEL) API is used to parse String. The API used is class SpelExpressionParser and the interface ExpressionParser. parseExpression is called that take input as String. If String contains more than one word, enclose String with single quote. Find the different example to see the usage of Spring Expression Language.

Simple SpelExpressionParser Example

package com.concretepage;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
public class SpELTest {
	public static void main(String[] args) {
		ExpressionParser parser = new SpelExpressionParser();
		Expression exp = parser.parseExpression("'You are reading concretepage.com'");
		String msg = (String) exp.getValue();
		System.out.println(msg);
	}
}
 



Using concat in Spring Expression Language

package com.concretepage;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
public class SpELTest {
	public static void main(String[] args) {
		ExpressionParser parser = new SpelExpressionParser();
		Expression exp = parser.parseExpression("'You are reading concretepage.com'.concat('!')");
		String msg = (String) exp.getValue();
		System.out.println(msg);
	}
}
 

String property 'Bytes' in Spring Expression Language

package com.concretepage;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
public class SpELTest {
	public static void main(String[] args) {
		ExpressionParser parser = new SpelExpressionParser();
		Expression exp = parser.parseExpression("'You are reading concretepage.com'.bytes");
		byte[] bytes = (byte[]) exp.getValue();
		System.out.println(bytes.length);
	}
}
 

toUpperCase() in Spring Expression Language

package com.concretepage;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
public class SpELTest {
	public static void main(String[] args) {
		ExpressionParser parser = new SpelExpressionParser();
		Expression exp = parser.parseExpression("'You are reading concretepage.com'.toUpperCase()");
		String msg = (String) exp.getValue();
		System.out.println(msg);
	}
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us