【Spring4.0】spEL(Spring Expression Language)表达式入门

##一、什么是spEL

The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.

While there are several other Java expression languages available, OGNL, MVEL, and JBoss EL, to name a few, the Spring Expression Language was created to provide the Spring community with a single well supported expression language that can be used across all the products in the Spring portfolio. Its language features are driven by the requirements of the projects in the Spring portfolio, including tooling requirements for code completion support within the eclipse based Spring Tool Suite. That said, SpEL is based on a technology agnostic API allowing other expression language implementations to be integrated should the need arise.
出自Spring Expression Language

Spring Expression Language(简称SpEL)是一种强大的表达式语言,支持在运行时查询和操作对象图。语言语法类似于Unified EL,但提供了其他功能,最着名的是方法调用和基本字符串模板功能。

虽然还有其他几种Java表达式语言,例如OGNL,MVEL和JBoss EL,但是创建Spring表达语言是为了向Spring社区提供一种支持良好的表达式语言,可以在所有产品中使用。春季组合。其语言功能受Spring组合项目要求的驱动,包括基于eclipse的Spring Tool Suite中代码完成支持的工具要求。也就是说,SpEL基于技术不可知的API,允许在需要时集成其他表达式语言实现。



##二、spEL能够实现的功能

  • 文字表达
  • 布尔和关系运算符
  • 常用表达
  • 类表达式
  • 访问属性,数组,列表,映射
  • 方法调用
  • 关系运算符
  • 分配
  • 调用构造函数
  • Bean引用
  • 阵列构造
  • 内联列表
  • 内联地图
  • 三元运算符
  • 变量
  • 用户定义的功能
  • 收集投影
  • 收藏品选择
  • 模板化的表达



    ##三、spEL实现某些功能的示例代码

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

###1.字面量表示
整数:<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}"/>
###2.引用其他对象
这里写图片描述
###3.引用其他对象属性
这里写图片描述
###4.引用其他方法
这里写图片描述
###5.链式引用其他方法
这里写图片描述
###6.算术运算符+、-、*、/、%、^
这里写图片描述
###7.加号还能够用作字符串连接
这里写图片描述
###8.比较运算符 <, >, ==, <=, >=, lt, gt, eq, le, ge
这里写图片描述
###9.逻辑运算符号 and, or, not, |

这里写图片描述
###10.if-else 运算符?: (ternary), ?: (Elvis)(即三目运算符)

这里写图片描述
###11.正则表达式matches

这里写图片描述
###12.调用静态方法或静态属性:通过T() 调用一个类的静态方法,它将返回一个 Class Object,然后再调用相应的方法或属性
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_33596978/article/details/81169047