当前位置:网站首页>2022-05-24 SpEL使用
2022-05-24 SpEL使用
2022-07-28 05:26:00 【不爱吃奶昔】
前言
SpEL(Spring Expression Language,Spring 表达式语言)并不需要跟Spring绑定,可以单独拿来使用,对字符串做一些表达式,在很多地方可以拿来使用;先说明SpEL的基础使用,然后贴上目前的一个应用场景。
SpEL
基础使用
SpEL主要分为三个接口
- Expression 表达式
- ExpressionParser 表达式解析
- EvaluationContext 评估上下文
接下也就是围绕这三个Interface实现类,贴出一些基本使用
- 操作字符串
public static void main(String[] args) {
SpelExpressionParser parser = new SpelExpressionParser();
Expression exp1 = parser.parseExpression("'hello,world!!'");
String exp1Str = (String)exp1.getValue();
System.out.println(exp1Str);
}
输出:hello,world!!
SpEL真正强大的地方并不尽尽于此
public static void main(String[] args) {
SpelExpressionParser parser = new SpelExpressionParser();
Expression exp1 = parser.parseExpression("'hello,world!!'.length");
Object value = exp1.getValue();
System.out.println(value);
}
输出:13
而是可以在Expression 字符串中执行逻辑代码
- 算数计算
public static void main(String[] args) {
SpelExpressionParser parser = new SpelExpressionParser();
Expression exp1 = parser.parseExpression("'hello,world!!'.length + 10");
Object value = exp1.getValue();
System.out.println(value);
}
输出:23
- 逻辑运算
public static void main(String[] args) {
SpelExpressionParser parser = new SpelExpressionParser();
Expression exp1 = parser.parseExpression("'hello,world!!'.length + 10");
Object value = exp1.getValue();
System.out.println(value);
}
- 变量
public static void main(String[] args) {
SpelExpressionParser parser = new SpelExpressionParser();
Expression exp1 = parser.parseExpression("#user.username");
StandardEvaluationContext context = new StandardEvaluationContext();
User user = new User();
user.setUsername("zsl");
user.setPassword("a123456");
context.setVariable("user", user);
Object value = exp1.getValue(context, String.class);
System.out.println(value);
}
输出:zsl
将变量存储到EvaluationContext接口实现类的变量中,使用Expression表达式处理回处理Context中的值;还可以对多个变量进行操作:
public static void main(String[] args) {
SpelExpressionParser parser = new SpelExpressionParser();
Expression exp1 = parser.parseExpression("#user.username + #user.password");
StandardEvaluationContext context = new StandardEvaluationContext();
User user = new User();
user.setUsername("zsl");
user.setPassword("a123456");
context.setVariable("user", user);
Object value = exp1.getValue(context, String.class);
System.out.println(value);
}
输出:zsla123456
应用
对于代码中处理操作日志比较麻烦需要调用log.info()方法,可以结合SpEL和注解的方式对日志进行收集
@LogRecord(content = "'用户名从{#oldUsername} 修改为{#request.username}")
public Object update(UserRequest request) {
UserDao.update(request);
}
使用Aop进行拦截被@LogRecord修饰的方法,收集内容并处理,最终使用SpEL进行解析;
后续会将使用SpEL实现日志项目完善提交到Github中,欢迎关注~
https://github.com/zsl0/costom_box
继承关系
ExpressionParser
表达式解析器实现:

组合模式:

实现:

Expression
表达式实现:

EvaluationContext
评估上下文实现:
其它类说明
ExpressionKey:AnnotatedElementKey和表达式

AnnotatedElementKey:存放method和目标对象

相关文章
边栏推荐
- 听说你也在实习|当我采访了几个大三实习生之后。
- Fluke dtx-1800 and its accessories dtx-cha002 channel adapter channel replacement RJ45 socket notes
- 【学习笔记】线程创建
- Fluke dtx-sfm2 single mode module of a company in Hangzhou - repair case
- 七夕礼物送女生什么好?颜值在线又有心意的礼物推荐
- QT batch operation control and set signal slot
- JSON笔记
- What happens when MySQL tables change from compressed tables to ordinary tables
- ubunut 服务器上传下载文件
- Exploration of Clickhouse aggregation internal mechanism of aggregation
猜你喜欢

Problems of font modification and line spacing in word automatic directory

Monitor the CPU temperature of raspberry pie 4B installed with esxi on ARM

七夕礼物送女生什么好?颜值在线又有心意的礼物推荐

Exploration of Clickhouse aggregation internal mechanism of aggregation

到底什么是Hash?(量化交易机器人系统开发)

开放式耳机推荐哪款最好、性价比最高的开放式耳机

微信小程序自定义编译模式

Pycharm2019 set editor theme and default code

Listener

Fluke dtx-sfm2 single mode module of a company in Hangzhou - repair case
随机推荐
七夕送什么礼物好?小众又高级的产品礼物推荐
qt中Qthread线程的使用以及安全关闭
Cronbach’s α?KMO系数?因子载荷?史上最易懂的问卷信效度分析教程!!!(SPSS和AMOS)
【学习笔记】进程创建
Solve the problem that the memory occupation is higher than that of the application process
自定义组件--数据监听器
ubunut 服务器上传下载文件
Esxi on ARM v1.2 (updated in November 2020)
【学习笔记】vim 编辑器
解决内存占用比应用进程占用高的问题
EMC experiment practical case ESD electrostatic experiment
Design and analysis of contactor coil control circuit
【学习笔记】知识管理
Hugging face 的入门使用
气导贴耳式耳机推荐、不需要佩戴入耳的气传导耳机
How to test industrial Ethernet cables (using fluke dsx-8000)?
我的注解笔记
机器学习笔记 5 —— Logistic Regression
Problems of font modification and line spacing in word automatic directory
自定义组件--纯数据字段&组件的生命周期