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. C#开发BHO插件UrlTrack

    最近忽然突发奇想,想统计一下我最经常上的网站是哪些,并且在这些网站上都停留了多久.为此决定写一个BHO插件来做这件事. BHO(Browser Help Objects)是实现了特定接口(IObjec ...

  2. CodeForces 489C (贪心) Given Length and Sum of Digits...

    题意: 找出m位且各个数位数字之和为s的最大和最小整数,不包括前导0(比如说003是非法的),但0是可以的. 分析: 这题是用贪心来做的,同样是m位数,前面的数字越大这个数就越大. 所以写一个can( ...

  3. Mysql的 时间戳转换 和 c# 的时间戳转换 (以秒来进行转换,非毫秒,主要是mysql不能存毫秒)

    Mysql 时间戳函数 => 从时间 转成 时间戳   UNIX_TIMESTAMP() 获取当前服务器时间的时间戳 UNIX_TIMESTAMP('2013-01-01 12:33:19') ...

  4. apache开源项目-- UIMA

    UIMA (Unstructured Information Management applications) 是一个软件系统,用来分析大量的非结构化信息从而发掘中对最终用户有用的知识点,一个最典型的 ...

  5. nginx - conf.d vs sites-available

    自己理解: conf.d - 扩展配置文件,用户配置文件 sites-available - 配置 虚拟主机(nginx支持多个虚拟主机,sites-enabled(存放 软链接,指向sites-av ...

  6. Egret应用开发实践(01) Egret与WebPack

    Egret Egret引擎是一款使用TypeScript语言构建的开源免费的移动游戏引擎.Egret仅是纯粹的使用TypeScript语言来开发游戏,开发后可以使用Egret来打包为HTML5网页游戏 ...

  7. validate.plugin.js 验证插件

    /*编写时间:2015-6-4*/ (function ($) { $.fn.isValidate = function (obj) { if ($(this).val()!="" ...

  8. window.history

    作者:zccst 旧版: forword() backword() go(number) HTML5中新增了 onhashchange  浏览器兼容性较好,用得较多 pushState / repla ...

  9. 关于UT的一些总结

    本文是个人对于UT的一些想法和总结,参考时建议请查阅官方资料. 转载请注明出处:http://www.cnblogs.com/sizzle/p/4476392.html 测试思想 编写UT测试代码,通 ...

  10. SQL2012远程连接到SQL2008时的问题:已成功与服务器建立连接,但在登陆过程中发生错误。

    服务器装的是2008,我机上装的是2012,结果一远程连接马上报错而且2012直接crash了.后来找到这位兄弟的帖子,http://www.cnblogs.com/liuguozhu2015/p/3 ...