Spring Bean Expression Language(EL)
1, Add dependency.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
2. Test Cases
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext; public class SpringELTest { @Test
public void testEvaluationContext() { Account account = new Account("Deniro");
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext(account);
Expression expression = parser.parseExpression("name");
String result = expression.getValue(context, String.class);
System.out.println("result:" + result); } @Test
public void test1() {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("'SpEL'.concat(' thinking')");
String result = (String) expression.getValue();
System.out.println("result:" + result);
} @Test
public void test2() {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("6+2");
Integer result = (Integer) expression.getValue();
System.out.println("result:" + result);
} @Test
public void test3() { // Spel 解析配置器
SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, SpringELTest.class.getClassLoader()); // 解析器
SpelExpressionParser parser = new SpelExpressionParser(configuration); // 上下文
EvaluationContext context = new StandardEvaluationContext(new Account("Deniro")); // 表达式
String expression = "getName()"; // 解析表达式
SpelExpression spelExpression = parser.parseRaw(expression); System.out.println(spelExpression.getValue(context));
} }
public class Account {
private String name;
public Account(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParseException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class GenericConvertExample {
public List<Integer> nums = new ArrayList<Integer>();
public static void main(String[] args) {
GenericConvertExample example = new GenericConvertExample();
example.nums.add(1);
//创建表达式上下文
StandardEvaluationContext context = new StandardEvaluationContext(example);
//创建表达式解析器
ExpressionParser parser = new SpelExpressionParser();
String expression = "nums[0]";
//自动将 2 转换为 Integer 类型
parser.parseExpression(expression).setValue(context, 2);
System.out.println("nums:" + example.nums);
//抛出 ConverterNotFoundException
try {
parser.parseExpression(expression).setValue(context, true);
} catch (EvaluationException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Spring Bean Expression Language(EL)的更多相关文章
- 【Head First Servlets and JSP】笔记23:Expression Language(EL) 完全攻略
基本上是<Head First Servlets and JSP>内容的整理.扩充.顺便推荐一个供参考的JSP教程:JSP Tutorial内容很全面,还有一些有趣的实例. 完整代码参考 ...
- How to create custom methods for use in spring security expression language annotations
From:http://stackoverflow.com/questions/6632982/how-to-create-custom-methods-for-use-in-spring-secur ...
- Spring生态研习【二】:SpEL(Spring Expression Language)
1. SpEL功能简介 它是spring生态里面的一个功能强大的描述语言,支在在运行期间对象图里面的数据查询和数据操作.语法和标准的EL一样,但是支持一些额外的功能特性,最显著的就是方法调用以及基本字 ...
- SPEL语言-Spring Expression Language
Spring表达式语言全称为"Spring Expression Language",缩写为"SpEL",类似于Struts 2x中使用的OGNL表达式语言,能 ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-004-使用AspectJ’s pointcut expression language定义Pointcut
一. 1.在Spring中,pointcut是通过AspectJ’s pointcut expression language来定义的,但spring只支持它的一部分,如果超出范围就会报Illegal ...
- EL表达式Expression Language
表达式语言Expression Language目的:简化jsp代码 EL内置对象 1.pageContext2.pageScope3.requestScope4.sessionScope5.appl ...
- EL(Expression Language)表达式语言
EL(Expression Language)表达式语言 EL的基本语法是以${开始,以}结束 为了能够方便地输出数据,EL提供了11个内置对象,其中 2个内置对象为了方便输出请求参数 param用来 ...
- EL(Expression Language)和JSTL标签(JSP Standard Tag Library)
一.EL表达式: Expression Language提供了在 JSP 脚本编制元素范围外(例如:脚本标签)使用运行时表达式的功能.脚本编制元素是指页面中能够用于在JSP 文件中嵌入 Java代码的 ...
- Spring Bean
一.Spring的几大模块:Data access & Integration.Transcation.Instrumentation.Core Spring Container.Testin ...
随机推荐
- WebSSH画龙点睛之lrzsz上传下载文件
本篇文章没有太多的源码,主要讲一下实现思路和技术原理 当使用Xshell或者SecureCRT终端工具时,我的所有文件传输工作都是通过lrzsz来完成的,主要是因为其简单方便,不需要额外打开sftp之 ...
- LightOJ 1229 Tablecross
Treblecross is a two player game where the goal is to get three X in a row on a one-dimensional boar ...
- 使用SQL计算宝宝每次吃奶的时间间隔(续)
本文是<使用SQL计算宝宝每次吃奶的时间间隔>的续篇,因为我工作繁忙,时常不能及时帮助媳妇儿记录,为了让不懂数据库的媳妇儿也可以自己用手机熟练操作.我继续做了一些修正和封装: 1.给媳妇儿 ...
- USB3.0之高速视频传输测试 双目相机(mt9p031、mt9m001)带宽高达300M测试 配合isensor测试 500万像素15fps
最近完善了下USB3.0的视频开发测试,主要优化了FPGA程序和固件,及其同步方式.对带宽和图像效果进行了仔细的测试 开发板架构(2CMOS+FPGA+2DDR2+USB3.0) 评估板底板配合2个M ...
- 【Babel】293- 初学 Babel 工作原理
戳蓝字「前端技术优选」关注我们哦! 前言 babel Babel 对于前端开发者来说应该是很熟悉了,日常开发中基本上是离不开它的. 已经9102了,我们已经能够熟练地使用 es2015+ 的语法.但是 ...
- 基于RT-Thread的开源飞控StarryPilot
背景描述 近年来无人机应用市场日趋火热,无人机开始被应用在多个领域之中,比如航拍,植保,运输,安防等.随着应用场景的增加,对于无人机的大脑一飞控,的性能和功能要求也变得越来越高.国内具有一大批优质的无 ...
- 二、Vue 页面渲染过程
前言 上篇博文我们依葫芦画瓢已经将hello world 展现在界面上啦,但是是不是感觉新虚虚的,总觉得这么多文件,项目怎么就启动起来了呢?怎么访问到8080 端口就能进入到我们的首页呢.整个的流程是 ...
- Appium移动端自动化测试--控件定位方法
常用定位手段 id Accessibility ID XPath 控件基础知识 DOM: Document Object Model文档对象模型 DOM应用:最早应用于HTML和Javascript的 ...
- wc命令统计目录下所有文件行数
想统计一下最近一个项目的代码行数,一个一个文件统计显然不是程序员的思维,wc命令可以统计一个文本的行数,结合find命令可以实现我的需求(注意符号):
- uni-app实现图片和视频上传功能
使用uni-app实现点击上传,既可以上传视频,有可以上传图片,图片预览,删除图片和视频功能,最终效果如下.uni-app里面没有提供同时上传视频和图片这个插件,只能靠自己手写, 1.页面布局 通过 ...