首页 > 其他分享 >Spring表达式(Spring Expression Language,SpringEL)官方文档

Spring表达式(Spring Expression Language,SpringEL)官方文档

时间:2023-05-12 16:15:22浏览次数:33  
标签:parseExpression String Language int Spring parser getValue SpringEL class

SpringEL是一个强大的表达式语言,支持在运行时查询和操作对象图。

官方地址:https://docs.spring.io/spring-framework/docs/5.3.18/reference/html/core.html#expressions

需要引入依赖:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>5.3.18</version>
</dependency>

引用:

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

文档部分例子:

ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'"); 
String message = (String) exp.getValue();


ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'.concat('!')"); 
String message = (String) exp.getValue();


// invokes 'getBytes().length'
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'.bytes.length"); 
int length = (Integer) exp.getValue();


ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("new String('hello world').toUpperCase()"); 
String message = exp.getValue(String.class);


ExpressionParser parser = new SpelExpressionParser();
// evals to "Hello World"
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();
// evals to 2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
Object nullValue = parser.parseExpression("null").getValue();

// evals to 1856
int year = (Integer) parser.parseExpression("birthdate.year + 1900").getValue(context);
String city = (String) parser.parseExpression("placeOfBirth.city").getValue(context);


// evaluates to a Java list containing the four numbers
List numbers = (List) parser.parseExpression("{1,2,3,4}").getValue(context);
List listOfLists = (List) parser.parseExpression("{{'a','b'},{'x','y'}}").getValue(context);


// evaluates to a Java map containing the two entries
Map inventorInfo = (Map) parser.parseExpression("{name:'Nikola',dob:'10-July-1856'}").getValue(context);
Map mapOfMaps = (Map) parser.parseExpression("{name:{first:'Nikola',last:'Tesla'},dob:{day:10,month:'July',year:1856}}").getValue(context);


int[] numbers1 = (int[]) parser.parseExpression("new int[4]").getValue(context);
// Array with initializer
int[] numbers2 = (int[]) parser.parseExpression("new int[]{1,2,3}").getValue(context);
// Multi dimensional array
int[][] numbers3 = (int[][]) parser.parseExpression("new int[4][5]").getValue(context);


// string literal, evaluates to "bc"
String bc = parser.parseExpression("'abc'.substring(1, 3)").getValue(String.class);
// evaluates to true
boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(societyContext, Boolean.class);


// evaluates to true
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);
// evaluates to false
boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class);
// evaluates to true
boolean trueValue = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);


// -- AND --
// evaluates to false
boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class);
// evaluates to true
String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
// -- OR --
// evaluates to true
boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);
// evaluates to true
String expression = "isMember('Nikola Tesla') or isMember('Albert Einstein')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
// -- NOT --
// evaluates to false
boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);
// -- AND and NOT --
String expression = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')";
boolean falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);


// Addition
int two = parser.parseExpression("1 + 1").getValue(Integer.class);  // 2
String testString = parser.parseExpression("'test' + ' ' + 'string'").getValue(String.class);  // 'test string'
// Subtraction
int four = parser.parseExpression("1 - -3").getValue(Integer.class);  // 4
double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class);  // -9000
// Multiplication
int six = parser.parseExpression("-2 * -3").getValue(Integer.class);  // 6
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class);  // 24.0
// Division
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class);  // -2
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class);  // 1.0
// Modulus
int three = parser.parseExpression("7 % 4").getValue(Integer.class);  // 3
int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class);  // 1
// Operator precedence
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class);  // -21


String randomPhrase = parser.parseExpression(
        "random number is #{T(java.lang.Math).random()}",
        new TemplateParserContext()).getValue(String.class);
// evaluates to "random number is 0.7038186818312008"



ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
context.setVariable("reverseString", StringUtils.class.getDeclaredMethod("reverseString", String.class));
String helloWorldReversed = parser.parseExpression("#reverseString('hello')").getValue(context, String.class);

public abstract class StringUtils {
    public static String reverseString(String input) {
        StringBuilder backwards = new StringBuilder(input.length());
        for (int i = 0; i < input.length(); i++) {
            backwards.append(input.charAt(input.length() - 1 - i));
        }
        return backwards.toString();
    }
}

 

标签:parseExpression,String,Language,int,Spring,parser,getValue,SpringEL,class
From: https://www.cnblogs.com/gdjlc/p/17394467.html

相关文章

  • SpringBoot集成Jpa对数据进行排序、分页、条件查询和过滤
    之前介绍了SpringBoot集成Jpa的简单使用,接下来介绍一下使用Jpa连接数据库对数据进行排序、分页、条件查询和过滤操作。首先创建Springboot工程并已经继承JPA依赖,如果不知道可以查看我的另一篇文进行学习,这里不做介绍。文章地址(https://www.cnblogs.com/eternality/p/17391141.html......
  • Spring体系化笔记(韩顺平课程)
    SpringSpring核心学习内容IOC、AOP、JdbcTemplate、声明式事务1.Spring几个重要概念Spring可以整合其他的框架(Spring是管理框架的框架)Spring有两个核心的概念:IOC和AOPIOCInversionOfControl控制反转动态代理(学好了才能学好AOP)AOPAspect-ori......
  • [SpringCloud]Spring-Cloud-Gateway之启动过程(源码分析)
    1前言1.1环境信息Spring-Cloud-Gateway:2.2.9.RELEASEorg.springframework.boot:spring-boot:2.3.12.RELEASEio.projectreactor.netty:reactor-netty:0.9.20.RELEASEio.netty:netty-transport:4.1.65.FINAL2启动过程#与Netty的调用链路2.1简版(V1.0)cn.seres.b......
  • solon架构(spring-boot未来最有效的竞争力)
    一、现在基本WEB的开发都是用spring-boot来做开发了,但是springboot存在很多弊端。体量大,启动慢等。优点就是生态比较完整,文档说明也比较多。二、solon架构,是我学习其他框架兼容时了解的,说说其区别之处。1)solon目前已经有一定规模了,有自己的生态圈了吧2)sol......
  • spring出现依赖关系形成循环问题,The dependencies of some of the beans in the appli
    出现这个问题大多使用的springboot都是在2.6.x以上,springboot在2.6.x就将依赖循环禁用了,解决方式有以下几种:解决方式:1、第一种解决方式:可以优化自己程序的逻辑,优化bean的依赖关系,只要不形成一个环状就不会出该问题了 2、第二种解决方式:可以使用@Lazy注解(懒加载)和@Autowired注......
  • 【Spring 事务】【一】 Spring 事务简介
    1 前言本节我们开始来看看Spring事务哈,大家看之前首先要看过IOC、AOP、甚至代理哈,如果这些你不知道原理,你看任何东西都会很费劲,比如Bean的生命周期、AOP的切入时机、什么时候创建代理以及执行时机,这些不知道的话,你就看事务的话,会很懵,当然前提是大家是带着思考看的哈,单纯看不......
  • springboot 大文件切片上传
    1.前端(vueelementui&原生)初始变量声明: currentFile:{},//当前上传的文件bigFileSliceCount:20,//大文件切片后的子文件数量(也可使用其它限定方式,如按照文件大小,每10MB切一片,此处采用的是固定切片的子文件数量的方式倒推切片大小) 接口:切片上传图片&合并......
  • java基于springboot+html的学生就业管理系统的设计与实现,附源码+数据库+文档,包安装调
    1、项目介绍本系统是利用现代化的计算机网络技术将传统信息宣传方式整合,按照实践过程设计完成的。同时完善服务,初步设计一个学生就业管理系统平台以利于相关的事务操作。为了使系统在各项管理中发挥更大的作用,实现计算机信息化高效的管理,现将开发目标功能需求介绍如下:(1)管理员模......
  • 存下吧!Spring高频面试题总结
    Spring是什么?Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。Spring的优点通过控制反转和依赖注入实现松耦合。支持面向切面的编程,并且把应用业务逻辑和系统服务分开。通过切面和模板减少样板式代码。声明式事务的支持。可以从单调繁冗的事务管理代码中解脱......
  • SpringBoot中单元测试如何对包含AopContext.currentProxy()的方法进行测试
    今天在工作中遇到一个问题,一个Service类中有一个方法,其中使用了AopContext.currentProxy()去访问自身的函数,例如intresult=((OrderServiceImpl)AopContext.currentProxy()).save();单元测试方法如下:@InjectMocksprivateOrderServiceImplorderServiceUnderTest;@Tes......