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 ...
随机推荐
- Vue - watch高阶用法
1. 不依赖新旧值的watch 很多时候,我们监听一个属性,不会使用到改变前后的值,只是为了执行一些方法,这时可以使用字符串代替 data:{ name:'Joe' }, watch:{ name:' ...
- Django 04
目录 视图层 三个常用方法 JsonResponse FBV 和 CBV 模板层 模板语法 模板传值 过滤器 标签 自定义过滤器和标签 模板的继承 模板的导入 视图层 三个常用方法 HttpRespo ...
- 【Canvas】311- 解决 canvas 在高清屏中绘制模糊的问题
点击上方"前端自习课"关注,学习起来~ 一.问题分析 使用 canvas 绘制图片或者是文字在 Retina 屏中会非常模糊.如图: 因为 canvas 不是矢量图,而是像图片一样 ...
- 04Shell流程控制
流程控制 if 单分支结构 if 条件测试 then 命令序列 fi 双分支结构 if 条件测试 then 命令序列 else 命令序列 fi 多分支结构 if 条件测试 1 then 命令序列 [e ...
- Electron开发和Web开发对比
- 通过url返回的状态来抢注好的用户名
之前在注册很多网站时都想取一个好的用户名,但是不知道那些被注册了没有,通常时一个一个测试,但是很慢当时就想过这个思路,由于懒并没有去搞 主要思路就是:很多网站的用户主页的Url都存在用户名,替换为自己 ...
- 解决logstash.outputs.elasticsearch[main] Could not index event to Elasticsearch status 404
现象:lostack启动正常,logstack收集输入redis数据,输出到elasticsearch写入失败 提示:去建索引 的时候elasticsearch返回404 [2019-11-12T11 ...
- JUC-7-lock接口
解决多线程安全的方式 synchronized 隐式锁 1.同步代码块 2.同步方法 3.lock 同步锁 显式锁 lock()方法上锁 unlo ...
- 基于Tomcat部署的Geoserver配置Geowebcache,并转发ArcGIS切片
1.将GeowebCache的war包解压到tomcat的webapp下,打开war包中的WEB-INF中的web.xml文件,同时创建一个你想保存geowencache的切片的空文件夹,例如我命名叫 ...
- MS14-068(CVE-2014-6324)域控提权利用及原理解析
漏洞利用 0x01 漏洞利用前提 1.域控没有打MS14-068的补丁(KB3011780) 2.拿下一台加入域的计算机 3.有这台域内计算机的域用户密码和Sid 0x02 工具下载 Ms14-068 ...