• 4.Spring EL运算符


    Spring EL运算符

    介绍

    Spring Expression Language (SpEL) 是 Spring 框架中用于处理表达式的语言,它支持从 Java 5 开始的 lambda 表达式,并具有类似 XPath 和 SQL 的语法。SpEL 提供了丰富的运算符,可以用于处理和操作对象图

    以下是 SpEL 的一些主要运算符:

    1. #root: 它代表当前表达式正在处理的根对象。在复杂的表达式中,可以用它来导航到根对象。
    2. 导航 .: 用于访问对象的属性或方法。例如,"person.name"表示访问 person 对象的 name 属性。如果需要访问嵌套的对象,可以使用连续的.,例如 "person.address.city"
    3. []: 用于访问集合和数组的元素。例如,"persons[0]" 表示访问 persons 集合的第一个元素。它还可以配合 #root 使用,例如 "#root[0]"
    4. (): 用于调用方法。例如,"person.getName()" 表示调用 person 对象的 getName() 方法。
    5. +-: 用于加法和减法。
    6. */: 用于乘法和除法。
    7. %: 用于求模。
    8. ^: 用于计算幂。
    9. !!!: 用于逻辑非和严格非。第一个 ! 将转化为 Java 的 ! 运算符,而 !! 将创建一个新的空对象或 null 值,取决于被操作的上下文。
    10. ?:??: 用于空值处理和空值合并。例如,"persons?.name ?? 'unknown'" 表示如果 persons 不为 null,则返回 persons 的 name,否则返回 “unknown”。
    11. ==!=: 用于比较两个值是否相等或不相等。
    12. <>: 用于比较两个值的大小关系。
    13. <=>=: 用于比较两个值的大小关系是否满足条件。
    14. andor: 用于逻辑与和逻辑或。
    15. not: 用于逻辑非。
    16. matches: 用于检查一个字符串是否满足某个正则表达式,或者一个对象是否匹配某个条件。例如,"person.name matches 'John.*'" 表示检查 person 的 name 是否以 John 开头。

    下面是一个 SpEL 的简单示例:

    假设我们有一个 person 对象,包含 name 和 age 两个属性和一个 getName() 方法:

    class Person {
        private String name;
        private int age;
        
        public String getName() {
            return name;
        }
        
        // getters and setters...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    那么我们可以这样使用 SpEL:

    @Autowired
    private ExpressionParser parser;
    
    public void evaluatePerson() {
        Person person = new Person();
        person.setName("John");
        person.setAge(30);
        
        Expression exp = parser.parseExpression("name"); // 获取 name 属性
        String name = (String) exp.getValue(person); // 在 person 对象上求值并获取结果
        System.out.println(name); // 输出 "John"
        
        exp = parser.parseExpression("age + 10"); // 计算 age+10 的值
        int newAge = (int) exp.getValue(person); // 在 person 对象上求值并获取结果
        System.out.println(newAge); // 输出 "40"
        
        exp = parser.parseExpression("getName()"); // 调用 getName() 方法
        String personName = (String) exp.getValue(person); // 在 person 对象上求值并获取结果
        System.out.println(personName); // 输出 "John"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Spring EL以注解的形式

    package com.yiibai.core;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("customerBean")
    public class Customer {
    
    	//Relational operators
    	
    	@Value("#{1 == 1}") //true
    	private boolean testEqual;
    	
    	@Value("#{1 != 1}") //false
    	private boolean testNotEqual;
    	
    	@Value("#{1 < 1}") //false
    	private boolean testLessThan;
    	
    	@Value("#{1 <= 1}") //true
    	private boolean testLessThanOrEqual;
    	
    	@Value("#{1 > 1}") //false
    	private boolean testGreaterThan;
    	
    	@Value("#{1 >= 1}") //true
    	private boolean testGreaterThanOrEqual;
    
    	//Logical operators , numberBean.no == 999
    	
    	@Value("#{numberBean.no == 999 and numberBean.no < 900}") //false
    	private boolean testAnd;
    	
    	@Value("#{numberBean.no == 999 or numberBean.no < 900}") //true
    	private boolean testOr;
    	
    	@Value("#{!(numberBean.no == 999)}") //false
    	private boolean testNot;
    
    	//Mathematical operators
    	
    	@Value("#{1 + 1}") //2.0
    	private double testAdd;
    	
    	@Value("#{'1' + '@' + '1'}") //1@1
    	private String testAddString;
    	
    	@Value("#{1 - 1}") //0.0
    	private double testSubtraction;
    
    	@Value("#{1 * 1}") //1.0
    	private double testMultiplication;
    	
    	@Value("#{10 / 2}") //5.0
    	private double testDivision;
    	
    	@Value("#{10 % 10}") //0.0
    	private double testModulus ;
    	
    	@Value("#{2 ^ 2}") //4.0
    	private double testExponentialPower;
    
    	@Override
    	public String toString() {
    		return "Customer [testEqual=" + testEqual + ", testNotEqual="
    				+ testNotEqual + ", testLessThan=" + testLessThan
    				+ ", testLessThanOrEqual=" + testLessThanOrEqual
    				+ ", testGreaterThan=" + testGreaterThan
    				+ ", testGreaterThanOrEqual=" + testGreaterThanOrEqual
    				+ ", testAnd=" + testAnd + ", testOr=" + testOr + ", testNot="
    				+ testNot + ", testAdd=" + testAdd + ", testAddString="
    				+ testAddString + ", testSubtraction=" + testSubtraction
    				+ ", testMultiplication=" + testMultiplication
    				+ ", testDivision=" + testDivision + ", testModulus="
    				+ testModulus + ", testExponentialPower="
    				+ testExponentialPower + "]";
    	}
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    package com.yiibai.core;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("numberBean")
    public class Number {
    
    	@Value("999")
    	private int no;
    
    	public int getNo() {
    		return no;
    	}
    
    	public void setNo(int no) {
    		this.no = no;
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    执行

    Customer obj = (Customer) context.getBean("customerBean");
    System.out.println(obj);
    
    • 1
    • 2

    输出结果

    Customer [testEqual=true, testNotEqual=false, testLessThan=false, testLessThanOrEqual=true, testGreaterThan=false, testGreaterThanOrEqual=true, testAnd=false, testOr=true, testNot=false, testAdd=2.0, testAddString=1@1, testSubtraction=0.0, testMultiplication=1.0, testDivision=5.0, testModulus=0.0, testExponentialPower=4.0]
    
    • 1

    Spring EL以XML形式

    请参阅在XML文件定义bean的等效版本。在XML中,类似 < 小于符号不支持,应该使用下面所示文本形式,例如文本等值, (‘<’ = ‘lt’) 和 (‘<=’ = ‘le’).

    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    	<bean id="customerBean" class="com.yiibai.core.Customer">
    	
    	  <property name="testEqual" value="#{1 == 1}" />
    	  <property name="testNotEqual" value="#{1 != 1}" />
    	  <property name="testLessThan" value="#{1 lt 1}" />
    	  <property name="testLessThanOrEqual" value="#{1 le 1}" />
    	  <property name="testGreaterThan" value="#{1 > 1}" />
    	  <property name="testGreaterThanOrEqual" value="#{1 >= 1}" />
    		
    	  <property name="testAnd" value="#{numberBean.no == 999 and numberBean.no lt 900}" />
    	  <property name="testOr" value="#{numberBean.no == 999 or numberBean.no lt 900}" />
    	  <property name="testNot" value="#{!(numberBean.no == 999)}" />
    		
    	  <property name="testAdd" value="#{1 + 1}" />
    	  <property name="testAddString" value="#{'1' + '@' + '1'}" />
    	  <property name="testSubtraction" value="#{1 - 1}" />
    	  <property name="testMultiplication" value="#{1 * 1}" />
    	  <property name="testDivision" value="#{10 / 2}" />
    	  <property name="testModulus" value="#{10 % 10}" />
    	  <property name="testExponentialPower" value="#{2 ^ 2}" />
    		
    	bean>
    	
    	<bean id="numberBean" class="com.yiibai.core.Number">
    		<property name="no" value="999" />
    	bean>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    JSP页面中page指令contentPage/pageEncoding具有什么功能呢?
    《天天数学》连载54:二月二十三日
    在conda构造的虚拟环境中运行MNIST测试用例
    【考研】操作系统——同步互斥问题(P、V操作)2
    除gRPC之外的另一个选择,IceRPC-支持QUIC
    Selenium 前世今生
    靶场练习——SDcms文件上传漏洞靶场
    搭建Docker私有镜像仓库
    【Java知识体系】Redis实用教程,深入原理
    SQLlabs46关
  • 原文地址:https://blog.csdn.net/LuckFairyLuckBaby/article/details/132910056