天天看點

【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.引用其他對象

【Spring4.0】spEL(Spring Expression Language)表達式入門

3.引用其他對象屬性

【Spring4.0】spEL(Spring Expression Language)表達式入門

4.引用其他方法

【Spring4.0】spEL(Spring Expression Language)表達式入門

5.鍊式引用其他方法

【Spring4.0】spEL(Spring Expression Language)表達式入門

6.算術運算符+、-、*、/、%、^

【Spring4.0】spEL(Spring Expression Language)表達式入門

7.加号還能夠用作字元串連接配接

【Spring4.0】spEL(Spring Expression Language)表達式入門

8.比較運算符 <, >, ==, <=, >=, lt, gt, eq, le, ge

【Spring4.0】spEL(Spring Expression Language)表達式入門

9.邏輯運算符号 and, or, not, |

【Spring4.0】spEL(Spring Expression Language)表達式入門

10.if-else 運算符?: (ternary), ?: (Elvis)(即三目運算符)

【Spring4.0】spEL(Spring Expression Language)表達式入門

11.正規表達式matches

【Spring4.0】spEL(Spring Expression Language)表達式入門

12.調用靜态方法或靜态屬性:通過

T()

調用一個類的靜态方法,它将傳回一個 Class Object,然後再調用相應的方法或屬性

【Spring4.0】spEL(Spring Expression Language)表達式入門

繼續閱讀