当前位置:网站首页>2022-05-24 use of spiel
2022-05-24 use of spiel
2022-07-28 06:35:00 【Don't like milkshakes】
SpEL Use
Preface
SpEL(Spring Expression Language,Spring Expression language ) No need to follow Spring binding , It can be used alone , Make some expressions for strings , It can be used in many places ; First explain SpEL The basic use of , Then post a current application scenario .
SpEL
Based on using
SpEL It is mainly divided into three interfaces
- Expression expression
- ExpressionParser Expression parsing
- EvaluationContext Evaluate context
Next is around these three Interface Implementation class , Post some basic usage
- Operation string
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);
}
Output :hello,world!!
SpEL The really powerful places are not all here
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);
}
Output :13
It can be in Expression Execute logical code in string
- Arithmetic calculation
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);
}
Output :23
- Logical operations
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);
}
- Variable
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);
}
Output :zsl
Store variables in EvaluationContext In the variables of the interface implementation class , Use Expression Expression processing back Context The value in ; You can also operate on multiple variables :
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);
}
Output :zsla123456
application
It is troublesome to process the operation log in the code, and you need to call log.info() Method , Can combine SpEL And annotations to collect logs
@LogRecord(content = "' User name from {#oldUsername} It is amended as follows {#request.username}")
public Object update(UserRequest request) {
UserDao.update(request);
}
Use Aop Intercepted by @LogRecord The method of decoration , Collect content and process , End use SpEL To analyze ;
Later, we will use SpEL Realize the improvement of log items and submit them to Github in , Welcome to your attention ~
https://github.com/zsl0/costom_box
Inheritance relationships
ExpressionParser
Expression parser implementation :

Portfolio model :

Realization :

Expression
Expression implementation :

EvaluationContext
Evaluate context implementation :
Description of other classes
ExpressionKey:AnnotatedElementKey And expressions

AnnotatedElementKey: Deposit method And the target

Related articles
How to record the operation log gracefully ?
Spring SpEL Expression language
Spring Cache annotations SpEL Expression parsing
Spring Cache Expression evaluation in ( And Spring Cache Summary )
边栏推荐
- Hugging face 的入门使用
- MySQL安装与使用
- Listener
- 【学习笔记】知识管理
- QT solves the problem of rebuilding UI files every time they are modified
- QT implementation outputs relevant information to log file
- JSP should pass parameters to the background while realizing the file upload function
- Matlab simulation of radar imaging 2 - pulse compression and windowing
- 正反斜杠笔记
- Icc2 use report_ Placement check floorplan
猜你喜欢

小程序自定义组件-数据,方法和属性

多个ics日历合并成单个ics日历

Pytorch learning notes 3 - datasets & dataloaders & transforms

What's a gift for girls on Chinese Valentine's day? Selfie online and thoughtful gift recommendation

set_ multicycle_ path

qt绘画事件-设置背景图片

Hugging face 的问题记录 I

小程序navigator无法跳转(debug)

qt中Qthread线程的使用以及安全关闭

Several methods of QT setting loading interface
随机推荐
Matlab simulation of radar imaging 3 - multi-target detection
Redhawk Dynamic Analysis
error: redefinition of ‘xxx‘
自定义组件--纯数据字段&组件的生命周期
【学习笔记】链表操作
小程序创建组件
当前学习进度
ClickHouse 中的公共表表达式CTE
2022-07-19 达梦数据库 连接实例、执行脚本、系统命令
夹子套利/搬砖套利系统开发
[yolov5] environment construction: win11 + mx450
Perl introductory learning (XI) file operation
2022年七夕礼物推荐!好看便宜又实用的礼物推荐
Icc2 use report_ Placement check floorplan
C语言的编译和预处理
Vscode中,无法打开源文件 “Adafruit_GFX.h“
Combine multiple ICs calendars into a single ICs calendar
pyppeteer 下拉 selenium下拉
OpenGL快速配置方法
Word自动目录字体修改和行间距的问题