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容器中只 ...
随机推荐
- [Codeforces670A]Holidays(数学,构造)
题目链接:http://codeforces.com/contest/670/problem/A 题意:给n天,问这n天最少和最多有多少个休息日,不用区分平闰年. 这题写几个例子,YY一下就构造出来解 ...
- hdu 1243 反恐训练营(dp 最大公共子序列变形)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1243 d[i][j] 代表第i 个字符与第 j 个字符的最大的得分.,, 最大公共子序列变形 #inclu ...
- jquery 字符串转json
这里考虑的都是服务器返回JSON形式的字符串的形式 代码如下: var data=" { root: [ {name:'1',value:'0'}, {name:'6101',value:' ...
- android webview js alert对话框 不能弹出 解决办法
在配置了webview的 setting属性后,以前设置的都是可以直接弹出来的,今天写一个小demo时候莫名其妙的发现alert怎么也出来,即使设置了这么多也不行: webSettings.setJa ...
- hdu 4612 Warm up(缩点+树上最长链)
本来就是自己负责图论,结果水了= = 题目其实很裸,就是求桥的数量,只是要新加上一条边罢了.做法:先缩点.再在树上搜最长链(第一场多校的hdu 4607Park Visit就考了最长链,小样,套个马甲 ...
- <摘录>PS和TS流的区别
在 MPEG-2系统中,信息复合/分离的过程称为系统复接/分接,由视频,音频的ES流和辅助数据复接生成的用于实际传输的标准信息流称为MPEG-2传送 流(TS:TransportStream).据传输 ...
- Mybaits+SpringMVC项目(含代码生成工具源码)
大家下载下来修改数据库配置应该就能运行起来,里面有一个SM的简单案例了,还有说明文件. 运行效果 工具类可以生成Springmvc+mybatis的相关类和配置文件,并具有增删查改的功能, ...
- WPF应用加载图片URI指定需要注意的地方
应用程序(.exe)加载图片: 可以省略"pack://application:,,," 打头,因为系统运行时需要的图片文件在Exe程序集(组合体)中:譬如: <Image ...
- Oracle 介绍 (未完待续)
关键字含义 1. DML.DDL.DCL DML----Data Manipulation Language 数据操纵语言例如:insert,delete,update,select(插入.删除.修改 ...
- history命令
在 Linux 下面可以使用 history 命令查看用户的所有历史操作,同时 shell 命令操作记录默认保存在用户目录的 .bash_history 文件中.通过这个文件可以查询 shell 命令 ...