Spring expression language (SpEL) supports many functionality, and you can test those expression features with this special “ExpressionParser” interface.

Here’s two code snippets, show the basic usage of using Spring EL.

SpEL to evaluate the literal string expression.

ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'put spel expression here'");
String msg = exp.getValue(String.class);

SpEL to evaluate the bean property – “item.name”.

Item item = new Item("mkyong", 100);
StandardEvaluationContext itemContext = new StandardEvaluationContext(item); //display the value of item.name property
Expression exp = parser.parseExpression("name");
String msg = exp.getValue(itemContext, String.class);

Few examples to test SpEL. The codes and comments should be self-exploratory.

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext; public class App {
public static void main(String[] args) { ExpressionParser parser = new SpelExpressionParser(); //literal expressions
Expression exp = parser.parseExpression("'Hello World'");
String msg1 = exp.getValue(String.class);
System.out.println(msg1); //method invocation
Expression exp2 = parser.parseExpression("'Hello World'.length()");
int msg2 = (Integer) exp2.getValue();
System.out.println(msg2); //Mathematical operators
Expression exp3 = parser.parseExpression("100 * 2");
int msg3 = (Integer) exp3.getValue();
System.out.println(msg3); //create an item object
Item item = new Item("mkyong", 100);
//test EL with item object
StandardEvaluationContext itemContext = new StandardEvaluationContext(item); //display the value of item.name property
Expression exp4 = parser.parseExpression("name");
String msg4 = exp4.getValue(itemContext, String.class);
System.out.println(msg4); //test if item.name == 'mkyong'
Expression exp5 = parser.parseExpression("name == 'mkyong'");
boolean msg5 = exp5.getValue(itemContext, Boolean.class);
System.out.println(msg5); }
}
public class Item {

	private String name;

	private int qty;

	public Item(String name, int qty) {
super();
this.name = name;
this.qty = qty;
} //...
}

Output

Hello World
11
200
mkyong
true

Test Spring el with ExpressionParser的更多相关文章

  1. spring 使用Spring表达式(Spring EL)

    Spring还提供了更灵活的注入方式,那就是Spring表达式,实际上Spring EL远比以上注入方式强大,我们需要学习它.Spring EL拥有很多功能. 使用Bean的id来引用Bean. •调 ...

  2. Spring3系列6 - Spring 表达式语言(Spring EL)

    Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...

  3. Spring EL regular expression example

    Spring EL supports regular expression using a simple keyword "matches", which is really aw ...

  4. Spring EL Lists, Maps example

    In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...

  5. Spring EL ternary operator (if-then-else) example

    Spring EL supports ternary operator , perform "if then else" conditional checking. For exa ...

  6. Spring EL Operators example

    Spring EL supports most of the standard mathematical, logical or relational operators. For example, ...

  7. Spring EL method invocation example

    In Spring EL, you can reference a bean, and nested properties using a 'dot (.)' symbol. For example, ...

  8. Spring EL hello world example

    The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation ti ...

  9. Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)

    一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...

随机推荐

  1. OEM - emctl resetTZ agent 设置时区

    [oracle@redhat4 config]$ cd $ORACLE_HOME/redhat4.7_orcl/sysman/config [oracle@redhat4 config]$ pwd/u ...

  2. Android Material Design-TabLayout的使用

    TabLayout 位于 android.support.design.widget.TabLayout. 一般与 ViewPager 结合在一起使用.以前有开源库 viewpagerindicato ...

  3. [HDOJ1827]Summer Holiday(强连通分量,缩点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1827 缩点后统计入度和当前强连通分量中最小花费,然后记录入度为0的点的个数和花费和就行了. /* ━━ ...

  4. tranform-scale 缩小元素,移上去文字抖动

    元素缩小后,鼠标移上去之后文字会出现抖动, -webkit-transform:scale(0.5); 修复代码如下: *{ -webkit-backface-visibility: hidden; ...

  5. mysql_create_frm

    http://www.cnblogs.com/jiangxu67/p/4755097.html http://www.cnblogs.com/jiangxu67/p/4755097.html http ...

  6. UVa 10892 (GCD) LCM Cardinality

    我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会. 最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数 #include <cstdio> ...

  7. 为什么多数游戏服务端是用 C++ 来写

    早年开发游戏必须用C++,这没得说,2000-2004年,java还没有nio,其他动态语言不抗重负,只能C/C++能开发出完整可用的游戏服务端.直到2005年,韩国的游戏很多都还是纯C++写服务端, ...

  8. BZOJ 3527 力

    fft推下公式.注意两点: (1)数组从0开始以避免出错. (2)i*i爆long long #include<iostream> #include<cstdio> #incl ...

  9. 无线端不响应键盘事件(keydown,keypress,keyup)

    今天在项目时,在android手机上使用输入法的智能推荐的词的话,不会触发keyup事件,一开始想到在focus时使用一个定时器,每隔100ms检测输入框的值是否发生了改变,如果改变了就作对应的处理, ...

  10. 【转】linux中waitpid及wait的用法

    原文网址:http://www.2cto.com/os/201203/124851.html wait(等待子进程中断或结束) 表头文件      #include<sys/types.h> ...