Test Spring el with ExpressionParser
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的更多相关文章
- spring 使用Spring表达式(Spring EL)
Spring还提供了更灵活的注入方式,那就是Spring表达式,实际上Spring EL远比以上注入方式强大,我们需要学习它.Spring EL拥有很多功能. 使用Bean的id来引用Bean. •调 ...
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
- Spring EL regular expression example
Spring EL supports regular expression using a simple keyword "matches", which is really aw ...
- 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 ...
- Spring EL ternary operator (if-then-else) example
Spring EL supports ternary operator , perform "if then else" conditional checking. For exa ...
- Spring EL Operators example
Spring EL supports most of the standard mathematical, logical or relational operators. For example, ...
- Spring EL method invocation example
In Spring EL, you can reference a bean, and nested properties using a 'dot (.)' symbol. For example, ...
- Spring EL hello world example
The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation ti ...
- Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)
一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...
随机推荐
- GetKeyState和GetAsyncKeyState以及GetKeyboardState函数的用法与区别2-------C#检查键盘大小写锁定状态
1.命名空间:using System.Runtime.InteropServices;2.导入方法[DllImport("user32.dll", EntryPoint = &q ...
- oracle Instance status: READY–lsnrctl status|start|stop
监听器启动,并不一定会认识数据库实例,启动监听器,请判别相关实例是否 READY [oracle@redhat4 ~]$ lsnrctl status LSNRCTL for Linux: Versi ...
- 面试题_93_to_102_编程和代码相关的面试题
93)怎么检查一个字符串只包含数字?(解决方案) 94)Java 中如何利用泛型写一个 LRU 缓存?(答案<) 95)写一段 Java 程序将 byte 转换为 long?(答案) 95)在不 ...
- 函数get_table_share
得到一个table_share 1)先从table_def_cache中查找, 如果有, 直接返回 2)如果没有找到, 为table_share分配内存,初始化,打开.frm文件,并将share ...
- 函数ut_2_log
计算某个数的对数(最大的) 例如 16 计算后为 4 2的4次方为16 例如15 计算后为3 2的3次方为8 /******************************************** ...
- UVa 1103 (利用连通块来判断字符) Ancient Messages
本题就是灵活运用DFS来求连通块来求解的. 题意: 给出一幅黑白图像,每行相邻的四个点压缩成一个十六进制的字符.然后还有题中图示的6中古老的字符,按字母表顺序输出这些字符的标号. 分析: 首先图像是被 ...
- CodeForces 489B (贪心 或 最大匹配) BerSU Ball
题意: 有m个男孩和n个女孩,每个人都有一个舞蹈熟练度,用一个不超过100的正整数来表示. 一个男孩和一个女孩能够结为舞伴当且仅当两人的熟练度相差不超过1. 问最多能结成多少对舞伴 分析: 这是一个二 ...
- Sublime-text markdown with Vim mode and auto preview
说明 最近看到markdown相关的东西,被其书写方式吸引,其实以前就在找这种类似的工具,但是也没找到,由于习惯了Vim,可Vim不支持markdown预览,这点可能不是很好,于是找到Sublime- ...
- UVA 575 Skew Binary (水)
题意:根据这种进制的算法,例如,给你一个左式,要求推出右式.(其实右式就是一个十进制数,根据这种进位的方法来转成特殊进制的数.) 思路:观察转换特点,有点类似于二进制,但是其在后面还减一了.比如25- ...
- poj 2184 Cow Exhibition
// 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n<=100,-1000 ...