使用Spring Expression Language (SpEL)全面解析表达式

2022-07-18,,,,

目录
  • spring expression language (spel)
    • 1.环境准备
    • 2.spel示例应用
    • 3.小结
  • spring表达式语言spel
    • spel:字面量
    • spel:引用 bean、属性和方法
    • spel支持的运算符号
    • 示例-基于xml的方式

spring expression language (spel)

是强大的表达式语言,支持查询、操作运行时对象图,以及解析逻辑、算术表达式。spel可以独立使用,无论你是否使用spring框架。

本文尝试通过多个示例使用spel,探索其强大能力。

1.环境准备

引入依赖:

compile group: 'org.springframework', name: 'spring-expression', version: '5.2.4.release'

读者可以选择最新版本或合适的版本。当然也可以下载相应jar文件。在调用下面的函数之前,按如下方式初始化一个类级属性spelexpression解析器:

import org.springframework.expression.expression;
import org.springframework.expression.expressionparser;
import org.springframework.expression.spel.standard.spelexpressionparser;
public class elmain {
    private expressionparser parser;
    elmain(){
        parser =  new spelexpressionparser();
    }
    public static void main(string[] args) {
        elmain elhelper = new elmain();
        elhelper.evaluateliteralexpresssions();
    }
    private static void print(object message){
        system.out.println(message);
    }

2.spel示例应用

2.1. 解析直接文本

    private void evaluateliteralexpresssions() {
        expression exp = parser.parseexpression("'hello world'");
        string message = (string) exp.getvalue();
        print(message);
        exp = parser.parseexpression("6");
        integer value = exp.getvalue(integer.class);
        print(value*2);
    }

这里直接解决字符串及数字文本。

2.2. 直接文本上调用方法

    /**
     * a function that tests method invocation on literals
     */
    private void methodinvocationonliterals() {
        expression exp = parser.parseexpression("'hello world'.concat('!')");
        string message = (string) exp.getvalue();
        println(message);
        exp = parser.parseexpression("'hello world'.length()");
        integer size = exp.getvalue(integer.class);
        println(size);
        exp = parser.parseexpression("'hello world'.split(' ')[0]");
        message = (string)exp.getvalue();
        println(message);
    }

示例展示了在字符串上直接调用java string类的public方法。

2.3.访问对象属性和方法

    /**a function that tests accessing properties of objects**/
    private void accessingobjectproperties() {
        user user = new user("john", "doe",  true, "john.doe@acme.com",30);
        expression exp = parser.parseexpression("firstname");
        println((string)exp.getvalue(user));
        exp = parser.parseexpression("isadmin()==false");
        boolean isadmin = exp.getvalue(user, boolean.class);
        println(isadmin);
        exp = parser.parseexpression("email.split('@')[0]");
        string emailid = exp.getvalue(user, string.class);
        println(emailid);
        exp = parser.parseexpression("age");
        integer age = exp.getvalue(user, integer.class);
        println(age);
    }

表达式可以直接使用对象的属性与方法。我们看到方法与属性使用一样,只是多了调用括号。

2.4.执行各种操作(比较、逻辑、算术)

spel支持下面几种操作:

  • 关系比较操作:==, !=, <, <=, >, >=
  • 逻辑操作: and, or, not
  • 算术操作: +, -, /, *, %, ^
    private void operators() {
        user user = new user("john", "doe", true,"john.doe@acme.com",  30);
        expression exp = parser.parseexpression("age > 18");
        println(exp.getvalue(user,boolean.class));
        exp = parser.parseexpression("age < 18 and isadmin()");
        println(exp.getvalue(user,boolean.class));
    }

2.5.使用多个对象和变量

表达式不仅需要引用对象,而且可能需要引用多个不同类型的对象。我们可以把所有使用的对象都加入至上下文中。使用键值对的方式加入并引用。

    private void variables() {
        user user = new user("john", "doe",  true, "john.doe@acme.com",30);
        application app = new application("facebook", false);
        standardevaluationcontext context = new standardevaluationcontext();
        context.setvariable("user", user);
        context.setvariable("app", app);
        expression exp = parser.parseexpression("#user.isadmin() and #app.isactive()");
        boolean result = exp.getvalue(context,boolean.class);
        println(result);
    }

2.6.调用自定义函数

spel也可以调用自定义的函数,用户可以扩展业务逻辑。下面首先定义一个函数:

public class stringhelper {
    public static boolean isvalid(string url){
        return true;
    }
}

下面在spel中调用isvalid方法:

    private void customfunctions() {
        try {
            standardevaluationcontext context = new standardevaluationcontext();
            context.registerfunction("isurlvalid",
                    stringhelper.class.getdeclaredmethod("isvalid", new class[] { string.class }));
            string expression = "#isurlvalid('http://google.com')";
            boolean isvalid = parser.parseexpression(expression).getvalue(context, boolean.class);
            println(isvalid);
        } catch (exception e) {
            e.printstacktrace();
        }
    }

3.小结

通过示例介绍了spel中多种应用场景。读者可以利用这些功能实现更加灵活的功能应用。

spring表达式语言spel

spring 表达式语言(简称spel):是一个支持运行时查询和操作对象图的强大的表达式语言。

语法类似于 el:spel 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 spel

spel 为 bean 的属性进行动态赋值提供了便利.

通过 spel 可以实现:

  • 通过 bean 的 id 对 bean 进行引用
  • 调用方法以及引用对象中的属性
  • 计算表达式的值
  • 正则表达式的匹配

spel:字面量

字面量的表示:

整数:
<property name="count" value="#{5}"/>
小数:
<property name="frequency" value="#{89.7}"/>
科学计数法:
<property name="capacity" value="#{1e4}"/>
string可以使用单引号或者双引号作为字符串的定界符号:
<property name=“name” value="#{'chuck'}"/> 
或
<property name='name' value='#{"chuck"}'/>
boolean:
<property name="enabled" value="#{false}"/>

如果仅仅是表示字面量,其实是没有必要使用spring el表达式的,这里仅仅演示一下而已,日常的开发中很少使用。

spel:引用 bean、属性和方法

引用其他对象

但是我们更常用ref 来实现其他对象的引用

引用其他对象的属性

调用其他方法,还可以链式操作

调用静态方法或静态属性

通过 t() 调用一个类的静态方法,它将返回一个 class object,然后再调用相应的方法或属性:

spel支持的运算符号

算数运算符:+, -, *, /, %, ^

加号还可以用作字符串连接

比较运算符: <, >, ==, <=, >=, lt, gt, eq, le, ge

逻辑运算符号: and, or, not, |

if-else 运算符:?: (ternary), ?: (elvis)

if-else 的变体

正则表达式:matches

示例-基于xml的方式

package com.xgj.spel;
/**
 * 
 * 
 * @classname: address
 * 
 * @description: 地址信息
 * 
 * @author: mr.yang
 * 
 * @date: 2018年4月7日 下午8:29:12
 */
public class address {
    private string city;
    private string street;
    public string getcity() {
        return city;
    }
    public void setcity(string city) {
        this.city = city;
    }
    public string getstreet() {
        return street;
    }
    public void setstreet(string street) {
        this.street = street;
    }
    @override
    public string tostring() {
        return "address [city=" + city + ", street=" + street + ", getclass()=" + getclass() + ", hashcode()=" + hashcode() + ", tostring()=" + super.tostring() + "]";
    }
}
package com.xgj.spel;
/**
 * 
 * 
 * @classname: car
 * 
 * @description: 车辆
 * 
 * @author: mr.yang
 * 
 * @date: 2018年4月7日 下午8:30:01
 */
public class car {
    private string brand;
    private double price;
    // 调用静态方法或静态属性:通过 t() 调用一个类的静态方法,它将返回一个 class object,然后再调用相应的方法或属性
    private long weight;
    public long getweight() {
        return weight;
    }
    public void setweight(long weight) {
        this.weight = weight;
    }
    public string getbrand() {
        return brand;
    }
    public void setbrand(string brand) {
        this.brand = brand;
    }
    public double getprice() {
        return price;
    }
    public void setprice(double price) {
        this.price = price;
    }
    @override
    public string tostring() {
        return "car [brand=" + brand + ", price=" + price + ", weight=" + weight + "]";
    }
}
package com.xgj.spel;
public class boss {
    private string name;
    private car car;
    // 通过 spring el 引用 address的city
    private string city;
    // 通过 car的price属性,确定info ,如果car.price>=500000 ,info 为ceo,否则为 staff
    private string info;
    public string getname() {
        return name;
    }
    public void setname(string name) {
        this.name = name;
    }
    public car getcar() {
        return car;
    }
    public void setcar(car car) {
        this.car = car;
    }
    public string getcity() {
        return city;
    }
    public void setcity(string city) {
        this.city = city;
    }
    public string getinfo() {
        return info;
    }
    public void setinfo(string info) {
        this.info = info;
    }
    @override
    public string tostring() {
        return "boss [name=" + name + ", car=" + car + ", city=" + city + ", info=" + info + "]";
    }
}

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="car" class="com.xgj.spel.car" 
        p:brand="bench" 
        p:price="700000"
        p:weight="#{t(java.lang.math).pi * 4567}" />
    <!-- 通过spring el表达式为属性赋值一个字面值 ,
           当然了,如果是字面值就没有必要使用spring el表达式了,这里仅仅是演示该用法 -->
    <bean id="address" class="com.xgj.spel.address" 
        p:city="#{'nanjing'}"
        p:street="ruanjiandadao" />
    <bean id="boss" class="com.xgj.spel.boss" 
        p:name="artisan" 
        p:city="#{address.city}"
        p:car-ref="car"
        p:info="#{car.price > 500000 ? 'ceo' : 'staff'}" />
</beans>

测试类:

package com.xgj.spel;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
public class speltest {
    public static void main(string[] args) {
        string configlocation = "com/xgj/spel/beans_spel.xml";
        applicationcontext ctx = new classpathxmlapplicationcontext(configlocation);
        car car = (car) ctx.getbean("car");
        system.out.println(car);
        boss boss = (boss) ctx.getbean("boss");
        system.out.println(boss);
    }
}

结果:

2018-04-07 21:21:30,804  info [main] (abstractapplicationcontext.java:583) - refreshing org.springframework.context.support.classpathxmlapplicationcontext@4af6178d: startup date [sat apr 07 21:21:30 bot 2018]; root of context hierarchy
2018-04-07 21:21:30,907  info [main] (xmlbeandefinitionreader.java:317) - loading xml bean definitions from class path resource [com/xgj/spel/beans_spel.xml]
car [brand=bench, price=700000.0, weight=14347]
boss [name=artisan, car=car [brand=bench, price=700000.0, weight=14347], city=nanjing, info=ceo]

示例-基于注解的方式

我们通过一个数据库的例子来演示。虽然可以通过spring el 表达式从配置文件中加载一个参数值,比如

@value("#{properties['jdbc.driverclassname']}")

是不是容易出错…. spring提供了更好的方式 context:property-placeholder。

package com.xgj.spel.annotation;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
/**
 * 
 * 
 * @classname: mydatasource
 * 
 * @description: 数据源 @component标注
 * 
 * @author: mr.yang
 * 
 * @date: 2018年4月7日 下午9:26:32
 */
@component
public class mydatasource {
    private string driverclass;
    private string url;
    private string username;
    private string password;
    public string getdriverclass() {
        return driverclass;
    }
    /**
     * 
     * 
     * @title: setdriverclass
     * 
     * @description: @value注解自动注入属性配置文件中对应属性的值
     * 
     * @param driverclass
     * 
     * @return: void
     */
    @value("${jdbc.driverclassname}")
    public void setdriverclass(string driverclass) {
        this.driverclass = driverclass;
    }
    public string geturl() {
        return url;
    }
    @value("${jdbc.url}")
    public void seturl(string url) {
        this.url = url;
    }
    public string getusername() {
        return username;
    }
    // @value("$(jdbc.username)")
    @value("${jdbc.username}")
    public void setusername(string username) {
        this.username = username;
    }
    public string getpassword() {
        return password;
    }
    @value("${jdbc.password}")
    public void setpassword(string password) {
        this.password = password;
    }
    @override
    public string tostring() {
        return "mydatasource [driverclass=" + driverclass + ", url=" + url + ", username=" + username + ", password=" + password + "]";
    }
}
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- 扫描的基包 -->
    <context:component-scan base-package="com.xgj.spel.annotation"/>
    <!-- 加载外部properties文件 -->
    <context:property-placeholder location="classpath:mysql/db_mysql.properties"/>  
</beans>

db_mysql.properties

jdbc.driverclassname=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/artisan
jdbc.username=artisan
jdbc.password=artisan
package com.xgj.spel.annotation;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
public class testcase {
    @test
    public void test() {
        string configurationlocation = "com/xgj/spel/annotation/beans_anno.xml";
        applicationcontext ctx = new classpathxmlapplicationcontext(configurationlocation);
        mydatasource mydatasource = (mydatasource) ctx.getbean("mydatasource");
        system.out.println(mydatasource);
        system.out.println("driverclassname:" + mydatasource.getdriverclass());
        system.out.println("url:" + mydatasource.geturl());
        system.out.println("username:" + mydatasource.getusername());
        system.out.println("password:" + mydatasource.getpassword());
    }
}

运行结果

2018-04-07 23:37:11,409  info [main] (abstractapplicationcontext.java:583) - refreshing org.springframework.context.support.classpathxmlapplicationcontext@761df304: startup date [sat apr 07 23:37:11 bot 2018]; root of context hierarchy
2018-04-07 23:37:11,552  info [main] (xmlbeandefinitionreader.java:317) - loading xml bean definitions from class path resource [com/xgj/spel/annotation/beans_anno.xml]
mydatasource [driverclass=com.mysql.jdbc.driver, url=jdbc:mysql://localhost:3306/artisan, username=artisan, password=artisan]
driverclassname:com.mysql.jdbc.driver
url:jdbc:mysql://localhost:3306/artisan
username:artisan
password:artisan

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

《使用Spring Expression Language (SpEL)全面解析表达式.doc》

下载本文的Word格式文档,以方便收藏与打印。