Spring SpEL 表达式语言

 一、文本表达式

文本表达式支持字符串、 数字(正数 、 实数及十六进制数) 、 布尔类型及 null。其中的字符表达式可使用单引号来表示,形如:'Deniro'。如果表达式中包含单引号或者双引号字符,那么可以使用转义字符 \。

数字支持负数 、小数、科学记数法、八进制数和十六进制数 。 默认情况下,实数使用 Double.parseDouble() 进行表达式类型转换 。

ExpressionParser parser = new SpelExpressionParser();

//字符串解析, 写成“Hello World”会报错。
String str = (String) parser.parseExpression("'Hello World'").getValue();
System.out.println(str);

//整型解析
int intVal = (Integer) parser.parseExpression("0x2F").getValue();
System.out.println(intVal);

//双精度浮点型解析
double doubleVal = (Double) parser.parseExpression("4329759E+22").getValue();
System.out.println(doubleVal);

//布尔型解析
boolean booleanVal = (boolean) parser.parseExpression("true").getValue();
System.out.println(booleanVal);

二、对象属性表达式

在 SpEL 中,我们可以使用对象属性路径(形如类名.属性名.属性名)来访问对象属性的值。

假设有一个账号类,Account.java:

public class Account {

    private String name;
    private int footballCount;

    private Friend friend;

    private List<Friend> friends;

    public Account(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setFootballCount(int footballCount) {
        this.footballCount = footballCount;
    }

    public int getFootballCount() {
        return footballCount;
    }

    public void setFriends(List<Friend> friends) {
        this.friends = friends;
    }

    public List<Friend> getFriends() {
        return friends;
    }

    public void setFriend(Friend friend) {
        this.friend = friend;
    }

    public Friend getFriend() {
        return friend;
    }
}

它包含姓名 name、足球数 footballCount 和一个朋友 friend 属性。friend 属性是一个 Friend 类:

public class Friend {
    private String name;

    public Friend(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

解析属性对象表达式

//初始化对象
Account account=new Account("Deniro");
account.setFootballCount(10);
account.addFriend(new Friend("Jack"));

//解析器
ExpressionParser parser
        = new SpelExpressionParser();
//解析上下文
EvaluationContext context=new StandardEvaluationContext(account);

//获取不同类型的属性
String name= (String) parser.parseExpression("Name").getValue(context);
System.out.println(name);
int count= (Integer) parser.parseExpression("footballCount+1").getValue(context);
System.out.println(count);

//获取嵌套类中的属性
String friend= (String) parser.parseExpression("friend.name").getValue(context);
System.out.println(friend);

总结:

  • SpEL 解析器适应力强,属性名首字母大小写均可。
  • 解析对象表达式时,需要传入 EvaluationContext 上下文参数。

三、数组、List 和 Map 表达式

数组表达式支持 Java 创建数组的语法,形如 new int[]{3,4,5},数组项之间以逗号作为分隔符。

注意:目前还不支持多维数组。Map 表达式以键值对的方式来定义,形如 {name:'deniro',footballCount:10}。

//解析器
ExpressionParser parser = new SpelExpressionParser();

//解析一维数组
int[] oneArray = (int[]) parser.parseExpression("new int[]{3,4,5}").getValue();
System.out.println("一维数组开始:");
for (int i : oneArray) {
    System.out.println(i);
}
System.out.println("一维数组结束");

//这里会抛出 SpelParseException
//int[][] twoArray = (int[][]) parser.parseExpression("new int[][]{3,4,5}{3,4,5}")
//        .getValue();

//解析 list
List list = (List) parser.parseExpression("{3,4,5}").getValue();
System.out.println("list:" + list);

//解析 Map
Map map = (Map) parser.parseExpression("{account:'deniro',footballCount:10}")
        .getValue();
System.out.println("map:" + map);

//解析对象中的 list
final Account account = new Account("Deniro");
Friend friend1 = new Friend("Jack");
Friend friend2 = new Friend("Rose");
List<Friend> friends = new ArrayList<>();
friends.add(friend1);
friends.add(friend2);
account.setFriends(friends);
EvaluationContext context = new StandardEvaluationContext(account);
String friendName = (String) parser.parseExpression("friends[0].name")
        .getValue(context);
System.out.println("friendName:" + friendName);

从数组与 List 获取值,可以在括号内指定索引来获取,形如上例中的 friends[0]。Map 中可通过键名来获取,形如 xxx['xxx']

四、方法表达式

SpEL 支持调用有访问权限的方法,这些方法包括对象方法、静态方法,而且支持可变方法参数。除此之外,还可以调用 String 类型中的所有可访问方法,比如 String.contains('xxx')

//解析器
ExpressionParser parser = new SpelExpressionParser();

//调用 String 方法
boolean isEmpty = parser.parseExpression("'Hi,everybody'.contains('Hi')").getValue(Boolean.class);
System.out.println("isEmpty:" + isEmpty);

/**
 * 调用对象相关方法
 */
final Account account = new Account("Deniro");
EvaluationContext context = new StandardEvaluationContext(account);

//调用公开方法
parser.parseExpression("setFootballCount(11)").getValue(context, Boolean.class);
System.out.println("getFootballCount:" + account.getFootballCount());

//调用私有方法,抛出 SpelEvaluationException: EL1004E: Method call: Method write() cannot be found on net.deniro.spring4.spel.Account type
//parser.parseExpression("write()").getValue(context,Boolean.class);

//调用静态方法
parser.parseExpression("read()").getValue(context, Boolean.class);

//调用待可变参数的方法
parser.parseExpression("addFriendNames('Jack','Rose')").getValue(context, Boolean.class);

注意:调用对象的私有方法会抛出异常。

五、操作符表达式

关系操作符

SpEL 支持 Java 标准操作符:等于、不等于、小于、小等于、大于、大等于、正则表达式和 instanceof 操作符。

//解析器
ExpressionParser parser = new SpelExpressionParser();

//数值比较
boolean result = parser.parseExpression("2>1").getValue(Boolean.class);
System.out.println("2>1:" + result);

//字符串比较
result = parser.parseExpression("'z'>'a'").getValue(Boolean.class);
System.out.println("'z'>'a':" + result);

//instanceof 运算符
result = parser.parseExpression("'str' instanceof T(String)").getValue(Boolean.class);
System.out.println("'str' 是否为字符串 :" + result);

result = parser.parseExpression("1 instanceof T(Integer)").getValue(Boolean.class);
System.out.println("1 是否为整型 :" + result);

//正则表达式
result = parser.parseExpression("22 matches '\\d{2}'").getValue(Boolean.class);
System.out.println("22 是否为两位数字 :" + result);

instanceof 操作符后面是类型表达式,格式为 T(Java 包装器类型),如整型 T(Integer)。注意:不能使用原生类型,如果这样 T(int) 会返回错误的判断结果。

matches 用于定义正则表达式,之后跟着单引号包裹着的正则表达式。

逻辑操作符

逻辑操作符支持以下操作:

逻辑操作符说明
and 或 &&与操作
or 或 ||或操作
!非操作

注意: 在 SpEL 中,不仅支持 Java 标准的逻辑操作符,还支持 and 与 or 关键字。

//解析器
ExpressionParser parser = new SpelExpressionParser();

//与操作
boolean result = parser.parseExpression("true && true").getValue(Boolean.class);
System.out.println("与操作:" + result);

//或操作
result = parser.parseExpression("true || false").getValue(Boolean.class);
System.out.println("或操作:" + result);

parser.parseExpression("true or false").getValue(Boolean.class);
System.out.println("或操作(or 关键字):" + result);

//非操作
result = parser.parseExpression("!false").getValue(Boolean.class);
System.out.println("非操作:" + result);

//抛出 SpelEvaluationException: EL1001E: Type conversion problem, cannot convert from java.lang.Integer to java.lang.Boolean
//parser.parseExpression("!0").getValue(Boolean.class);

注意:逻辑操作符前后运算结果必须是布尔类型,否则会抛出 SpelEvaluationException。

运算操作符

SpEL 支持 Java 运算操作符,并遵守运算符优先级规则:

运算操作符说明支持的操作数类型
+加法数字、字符串或日期
-减法数字或日期
*乘法数字
/除法数字
%取模数字
^指数幂数字
//解析器
ExpressionParser parser = new SpelExpressionParser();

//加法运算
Integer iResult = parser.parseExpression("2+3").getValue(Integer.class);
System.out.println("加法运算:" + iResult);

String sResult = parser.parseExpression("'Hi,'+'everybody'").getValue(String.class);
System.out.println("字符串拼接运算:" + sResult);

//减法运算
iResult = parser.parseExpression("2-3").getValue(Integer.class);
System.out.println("减法运算:" + iResult);

//乘法运算
iResult = parser.parseExpression("2*3").getValue(Integer.class);
System.out.println("乘法运算:" + iResult);

//除法运算
iResult = parser.parseExpression("4/2").getValue(Integer.class);
System.out.println("除法运算:" + iResult);

Double dResult = parser.parseExpression("4/2.5").getValue(Double.class);
System.out.println("除法运算:" + dResult);

//求余运算
iResult = parser.parseExpression("5%2").getValue(Integer.class);
System.out.println("求余运算:" + iResult);

六、安全导航操作符

安全导航操作符来源于 Groovy 语言,使用它能够避免空指针异常。一般在访问对象时,需要验证该对象是否为空,使用安全导航操作符就能避免繁琐的空对象验证方法。它的格式是在获取对象属性操作符“.” 之前加一个 “?”。

final Account account = new Account("Deniro");
account.setFriend(new Friend("Jack"));

//解析器
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext(account);

String friendName = parser.parseExpression("friend?.name").getValue(context, String.class);
System.out.println("friendName:" + friendName);

//设置为 null
account.setFriend(null);
friendName = parser.parseExpression("friend?.name").getValue(context, String.class);
//打印出 null
System.out.println("friendName:" + friendName);

这里会先判断 friend 对象是否为空;如果为空,则返回 "null" 字符串;否则返回需要的属性值。

七、三元运算符

SpEL 支持标准的 Java 三元操作符:<表达式 1>?<表达式 2>:<表达式 3>

ExpressionParser parser = new SpelExpressionParser();

boolean result = parser.parseExpression("(1+2) == 3?true:false").getValue(Boolean.class);
System.out.println("result:" + result);

八、Elvis 操作符

Elvis 操作符是在 Groovy 中使用的三元操作符简化版。

在三元操作符中,我们一般需要写两次变量名,比如下面代码段中的 title:

String title="News";
String actualTitle=(title!=null)?title:"tip";

使用 Elvis 操作符后,可以将上述代码段简写为:

title?:"tip"

SpEL 支持的 Elvis 操作符格式是:<var>?:<value>,如果 var 变量为 null,那就取 value 值,否则就取自身的值。所以 Elvis 操作符很适合用于设置默认值。

final Account account = new Account("Deniro");
//解析器
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext(account);

String friendName = parser.parseExpression("name?:'无名'").getValue(context, String.class);
System.out.println("friendName:" + friendName);

//设置名字为 null
account.setName(null);
friendName = parser.parseExpression("name?:'无名'").getValue(context, String.class);
System.out.println("friendName:" + friendName);

九、赋值表达式

可以通过赋值表达式来设置属性的值,效果等同于调用 setValue() 方法。

final Account account = new Account("Deniro");

//解析器
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext(account);

String name=parser.parseExpression("name='Jack'").getValue(context,String.class);
System.out.println("name:"+name);

十、类型操作符

类型操作符 T 可以从类路径加载指定类名称(全限定名)所对应的 Class 的实例,格式为:T(全限定类名),效果等同于 ClassLoader#loadClass()

ExpressionParser parser = new SpelExpressionParser();

//加载 java.lang.Integer
Class integerClass = parser.parseExpression("T(Integer)").getValue(Class.class);
System.out.println(integerClass == java.lang.Integer.class);

//加载 net.deniro.spring4.spel.Account
Class accountClass = parser.parseExpression("T(com.codedot.spel.Account)").getValue(Class.class);
System.out.println(accountClass == Account.class);

//调用类静态方法
double result = (double) parser.parseExpression("T(Math).abs(-2.5)").getValue();
System.out.println("result:" + result);

我们还可以直接通过 T 操作符调用类的静态方法,格式为 T(全限定类名).静态方法名,比如上面例子中求某数的绝对值 T(Math).abs(-2.5)

SpEL 中会使用 StandardTypeLocator#findType() 方法来加载类。 findType 方法定义如下:

public Class<?> findType(String typeName) throws EvaluationException {
	String nameToLookup = typeName;
	try {
		return ClassUtils.forName(nameToLookup, this.classLoader);
	}
	catch (ClassNotFoundException ey) {
		// try any registered prefixes before giving up
	}
	for (String prefix : this.knownPackagePrefixes) {
		try {
			nameToLookup = prefix + '.' + typeName;
			return ClassUtils.forName(nameToLookup, this.classLoader);
		}
		catch (ClassNotFoundException ex) {
			// might be a different prefix
		}
	}
	throw new SpelEvaluationException(SpelMessage.TYPE_NOT_FOUND, typeName);
}
  • 尝试直接加载类。
  • 如果找不到,则尝试从已注册的包前缀(java.lang)下加载类。所以如果需要加载的类在 java.lang 下,那么可以直接写类名。
  • 如果都找不到,则抛出 SpelEvaluationException 异常。

十一、创建对象操作符

可以使用 new 操作符来创建一个新对象 。 除了基本类型(如整型、布尔型等)和字符串之外,创建其它类需要指明全限定类名( 包括包路径 ) 。

ExpressionParser parser = new SpelExpressionParser();

Account account = parser.parseExpression("new com.codedot.spel.Account" + "('Deniro')").getValue(Account.class);
System.out.println("name:"+account.getName());

十二、变量表达式

可以通过 #变量名 来引用在 EvaluationContext 中定义的变量。通过 EvaluationContext#setVariable(name, val) 即可定义新的变量;name 表示变量名,val 表示变量值。

如果变量是集合,比如 list,那么可以通过 #scores.[#this] 来引用集合中的元素。

Account account = new Account("Deniro");

ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext(account);

//定义一个新变量,名为 newVal
context.setVariable("newVal", "Jack");

//获取变量 newVal 的值,并赋值给 User 的 name 属性
parser.parseExpression("name=#newVal").getValue(context);
System.out.println("getName:" + account.getName());

//this 操作符表示集合中的某个元素
List<Double> scores = new ArrayList<>();
scores.addAll(Arrays.asList(23.1, 82.3, 55.9));
context.setVariable("scores", scores);//在上下文中定义 scores 变量
List<Double> scoresGreat80 = (List<Double>) parser.parseExpression("#scores.?[#this>80]")
        .getValue(context);
System.out.println("scoresGreat80:" + scoresGreat80);

十三、集合选择表达式

可以使用选择表达式来过滤集合,从而生成一个新的符合选择条件的集合 。它的语法是 ?[selectionExpression]。选择符合条件的结果集的第一个元素的语法为 ^ [selectionExpression] ,选择最后一个元素的语法为 $[selectionExpression]。选择表达式也可应用于 Map 。

List<Integer> list = new ArrayList();
list.add(10);
list.add(21);
list.add(8);
list.add(33);

ExpressionParser parser = new SpelExpressionParser();
//过滤 list 集合中的元素
StandardEvaluationContext listContext = new StandardEvaluationContext(list);
List<Integer> great4List = (List<Integer>) parser.parseExpression("?[#this>4]").getValue(listContext);
System.out.println("great4List:" + great4List);

//获取匹配元素中的第一个值
Integer first = (Integer) parser.parseExpression("^[#this>2]").getValue(listContext);
System.out.println("first:" + first);

//获取匹配元素中的最后一个值
Integer end = (Integer) parser.parseExpression("$[#this>2]").getValue(listContext);
System.out.println("end:" + end);

对于 List 和 Set ,是针对集合中的每一个元素进行比较的;而对于 Map,则可以指定是元素的键(key)还是元素的值进行比较的。

ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
//过滤 Map
Map<String, Double> rank = new HashMap<>();
rank.put("Deniro", 96.5);
rank.put("Jack", 85.3);
rank.put("Lily", 91.1);
context.setVariable("Rank", rank);

//value 大于 90
Map<String, Double> rankGreat95 = (Map<String, Double>) parser.parseExpression
        ("#Rank.?[value>90]").getValue(context);
System.out.println("rankGreat95:" + rankGreat95);

//key 按字母顺序,排在 L 后面
Map<String, Double> afterL = (Map<String, Double>) parser.parseExpression("#Rank.?[key>'L']").getValue(context);
System.out.println("afterL:" + afterL);

十四、集合元素布尔判断

通过表达式 ![projectionExpression],我们可以判断集合中每一个元素是否符合表达式规则。

ExpressionParser parser = new SpelExpressionParser();
List list = (List) parser.parseExpression("{3,4,5}").getValue();
System.out.println("list:" + list);
List<Boolean> isgreat4=(List<Boolean>)parser.parseExpression("![#this>3]").getValue(list);
System.out.println("isgreat4:" + isgreat4);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring表达式语言SpEL)是一种强大的表达式语言,用于在运行时计算表达式的值。SpEL可以用于配置文件、注解和代码中,支持对Bean的属性、方法和构造函数进行访问和操作,提供了很多常用的运算符和函数,是Spring框架中非常重要的一部分。 SpEL支持以下特性: 1. 引用Bean的属性和方法。可以使用“#”符号引用Bean的属性和方法,如“#user.name”表示引用名为user的Bean的name属性。 2. 调用静态方法和常量。可以使用“T()”关键字调用静态方法和常量,如“T(java.lang.Math).PI”表示引用Math类的PI常量。 3. 访问数组和集合。可以使用“[]”符号访问数组和集合,如“list[0]”表示访问名为list的集合的第一个元素。 4. 进行算术运算和比较运算。SpEL支持常见的算术运算和比较运算符,如“+”、“-”、“*”、“/”、“%”、“==”、“!=”、“<”、“>”等。 5. 定义变量和使用占位符。可以使用“#{}”定义变量和使用占位符,如“#{T(System).currentTimeMillis()}”表示定义一个名为currentTimeMillis的变量并赋值为当前时间的毫秒数。 6. 调用Bean的构造函数。可以使用“new”关键字调用Bean的构造函数,如“new java.util.Date()”表示调用java.util.Date类的无参构造函数。 SpEL可以在Spring的XML配置文件、@Value注解以及SpEL表达式注解中使用,可以方便地实现复杂的条件判断和动态计算。例如,可以使用SpEL表达式注解来实现@Scheduled注解的cron表达式的动态计算,或者在XML配置文件中使用SpEL表达式来实现Bean之间的依赖注入和条件配置等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值