当前位置:网站首页>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 )
边栏推荐
- MFC uses the console to print program information
- Pytorch learning notes
- Explain the installation of MSDN 2015 and precautions
- What's a gift for girls on Chinese Valentine's day? Selfie online and thoughtful gift recommendation
- 一、ffmpeg录制音频为pcm文件
- What happens when MySQL tables change from compressed tables to ordinary tables
- Combine multiple ICs calendars into a single ICs calendar
- Graduation thesis | how to write literature review
- Cronbach’s α? Kmo coefficient? Factor load? The most understandable course of questionnaire reliability and validity analysis in history!!! (SPSS and AMOS)
- set_ multicycle_ path
猜你喜欢
随机推荐
C语言的编译和预处理
scrapy 命令
Perl introductory learning (VIII) subroutine
夹子套利/搬砖套利系统开发
Listener
OpenGL快速配置方法
qt实现将相关信息输出到日志文件
OpenGL的开发环境配置【VS2017】+常见问题
Matlab simulation of radar imaging 3 - multi-target detection
一、ffmpeg录制音频为pcm文件
npm yarn相关的操作
气传导蓝牙耳机怎么样、最值得入手的气传导耳机
Several methods of QT setting loading interface
Hugging face 的入门使用
JSP should pass parameters to the background while realizing the file upload function
Talk about the "hybrid mode" of esxi virtual switch and port group
到底什么是Hash?(量化交易机器人系统开发)
气传导蓝牙耳机哪个好、气传导蓝牙耳机排行榜
ubuntu mysql 设置远程访问权限
MFC 使用控制台打印程序信息









