---
title: "Spring 表达式语言 （Spring Expression Language） SpEL"
author: "cmm"
date: "2012-03-15T11:21:43Z"
tags: []
categories: ["编程语言"]
slug: "spring-spring-expression-language-spel"
draft: false
lastmod: "2012-03-15T11:21:43Z"
id: 1454061
url: "https://cmm.iteye.com/blog/1454061"
---

Spring 3.0 创建了一种新的方式用以配置对象的注入（set注入或者构造参数注入），它便是SpEL (Spring Expression Language)下面我们一一做一介绍。

▲基础特性

——SpEL使用#{…}作为定界符，所有在大框号中的字符都将被认为是SpEL.

——1、 字面量的表示 

  1&gt;整数

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="count" value="#{5}"/&gt;

 2&gt;小数

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="frequency" value="#{89.7}"/&gt;

  3&gt;科学计数法

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="capacity" value="#{1e4}"/&gt;

 4&gt;String可以使用单引号或者双引号作为字符串的定界符号。

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="name" value="#{'Chuck'}"/&gt;
2. &lt;property name='name' value='#{"Chuck"}'/&gt;

 5&gt;Boolean

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="enabled" value="#{false}"/&gt;

——2、 引用Bean，属性和方法

 1&gt;引用其他对象

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;bean id=”saxophone” value=”com.xxx.xxx.Xxx”/&gt;
2. &lt;bean ..&gt;
3. .
4. &lt;property name="instrument" value="#{saxophone}"/&gt;
5. .
6. &lt;bean/&gt;

通过id：“saxophone”将对象注入到instrument属性中，这与下面的配置是一样的:

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="instrument" ref="saxophone"/&gt;

  2&gt; 引用其他对象的属性

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;bean id="carl"
2. class="com.springinaction.springidol.Instrumentalist"&gt;
3. &lt;property name="song" value="#{kenny.song}" /&gt;
4. &lt;/bean&gt;

kenny是Bean Id 而 song是属性的名字，这样配置就如同我们写了如下的代码

**\[java]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. Instrumentalist carl = new Instrumentalist();
2. carl.setSong(kenny.getSong());

 3&gt;调用其他方法

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="song" value="songSelector.selectSong()"/&gt;

调用了BeanId为“songSelector”的对象的selectSong()方法，并将返回值注入到song属性中。或者还可以链式操作。如下:

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="song" value="songSelector.selectSong().toUpperCase()"/&gt;

如果songSelector.selectSong()返回null的还会抛出异常，为了避免我们要使用?.表达式。这样如果songSelector.selectSong()为null就不会再调用后面的方法了。如下

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="song" value="songSelector.selectSong()?.toUpperCase()"/&gt;

 4&gt;调用静态方法

我们已经知道如何通过一个对象调用它的方法了，但是如何调用一个静态方法呢？用T()。它将返回一个 Class object  
然后我们再调用相应的方法即可：

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="multiplier" value="T(java.lang.Math).PI"/&gt;

▲SpEL支持的运算符号  
——1、算数运算符：+, -, \*, /, %, ^

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="adjustedAmount" value="#{counter.total + 42}"/&gt;
2. &lt;property name="adjustedAmount" value="#{counter.total - 20}"/&gt;
3. &lt;property name="circumference" value="#{2 * T(java.lang.Math).PI * circle.radius}"/&gt;
4. &lt;property name="average" value="#{counter.total / counter.count}"/&gt;
5. &lt;property name="remainder" value="#{counter.total % counter.count}"/&gt;
6. &lt;property name="area" value="#{T(java.lang.Math).PI * circle.radius ^ 2}"/&gt;

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

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="fullName" value="#{performer.firstName + ' ' + performer.lastName}"/&gt;

——2、比较运算符： &lt;, &gt;, ==, &lt;=, &gt;=, lt, gt, eq, le, ge

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="equal" value="#{counter.total == 100}"/&gt;

不可以使用&lt;和&gt;号，应为在xml中它有特殊的含义，我们使用lt和gt代替

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="hasCapacity" value="#{counter.total le 100000}"/&gt;

——3、 逻辑运算符号： and, or, not, |

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="largeCircle" value="#{shape.kind == 'circle' and shape.perimeter gt 10000}"/&gt;
2. &lt;property name="outOfStock" value="#{!product.available}"/&gt;
3. &lt;property name="outOfStock" value="#{not product.available}"/&gt;

——4、 If-else运算符：?: (ternary), ?: (Elvis)

〇最基本的 ?:（这如同我们在使用EL表达式语言）：

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="instrument" value="#{songSelector.selectSong() == 'Jingle Bells' ? piano : ' Jingle Bells '}"/&gt;

〇变体的 ?:

 &lt;property name="song" value="#{kenny.song != null ? kenny.song : 'Greensleeves'}"/&gt;

上下两种是同一语义，但下面的明显简洁

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="song" value="#{kenny.song ?: 'Greensleeves'}"/&gt;

——5、 正则表达式：matches

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="validEmail" value="#{admin.email matches '\[a-zA-Z0-9.\_%+-]+@\[a-zA-Z0-9.-]+\\\\.\[a-zA-Z]{2,4}'}"/&gt;

表达式返回逻辑值，如果匹配返回true,否则返回false

▲SpEL对集合的支持

——环境

有实体City定义如下：

**\[java]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. package com.habuma.spel.cities;
2. public class City {
3. private String name;
4. private String state;
5. private int population;
6. }

Xml中有如下定义

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

01. &lt;util:list id="cities"&gt;
02. &lt;bean class="com.habuma.spel.cities.City"
03. p:name="Chicago" p:state="IL" p:population="2853114"/&gt;
04. &lt;bean class="com.habuma.spel.cities.City"
05. p:name="Atlanta" p:state="GA" p:population="537958"/&gt;
06. &lt;bean class="com.habuma.spel.cities.City"
07. p:name="Dallas" p:state="TX" p:population="1279910"/&gt;
08. &lt;bean class="com.habuma.spel.cities.City"
09. p:name="Houston" p:state="TX" p:population="2242193"/&gt;
10. &lt;bean class="com.habuma.spel.cities.City"
11. p:name="Odessa" p:state="TX" p:population="90943"/&gt;
12. &lt;bean class="com.habuma.spel.cities.City"
13. p:name="El Paso" p:state="TX" p:population="613190"/&gt;
14. &lt;bean class="com.habuma.spel.cities.City"
15. p:name="Jal" p:state="NM" p:population="1996"/&gt;
16. &lt;bean class="com.habuma.spel.cities.City"
17. p:name="Las Cruces" p:state="NM" p:population="91865"/&gt;
18. &lt;/util:list&gt;

——1、 获取Collection中的某个对象

〇通过下标访问，如下：

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="chosenCity" value="#{cities\[2]}"/&gt;

我们就会获得population为"1279910"的city（记住下标从0开始）

〇下标可以通过变量指定，如下：

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="chosenCity" value="#{cities\[T(java.lang.Math).random() * cities.size()]}"/&gt;

〇如果是从Map中获得，可指定key值，如下

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="chosenCity" value="#{cities\['Dallas']}"/&gt;

〇也可以通过key访问properties的值，如下

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;util:properties id="settings" location="classpath:settings.properties"/&gt;
2. &lt;property name="accessToken" value="#{settings\['twitter.accessToken']}"/&gt;

〇可以通过下标访问systemEnvironment和SystemProperties中的值

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="homePath" value="#{systemEnvironment\['HOME']}"/&gt;

〇如果在jre运行时配置了-Dapplication.home=/etc/myapp，我们可以通过如下方式访问

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="homePath" value="#{systemProperties\['application.home']}"/&gt;

〇通过下标获取String串中的某个字符

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. 'This is a test'\[3]

——2、获取Collection中的子集-通过条件筛选（注意新对象是一个新的Collection）

   1&gt;筛选子集(.?\[])

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="bigCities" value="#{cities.?\[population gt 100000]}"/&gt;

   2&gt;获取第一个（.^\[]）

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="aBigCity" value="#{cities.^\[population gt 100000]}"/&gt;

   3&gt;获取最后一个（.$\[]）

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="aBigCity" value="#{cities.$\[population gt 100000]}"/&gt;

——3、集合的投影（.!\[]）

 如果想获得所有城市的名称组成的列表，可用如下操作

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="cityNames" value="#{cities.!\[name]}"/&gt;

将返回"Chicago", "Atlanta", "Dallas"

也可以组合两个列，如下：

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="cityNames" value="#{cities.!\[name + ', ' + state]}"/&gt;

将返回"Chicago, IL", "Atlanta, GA", and "Dallas, TX".

—— 4、将投影和筛选结合

**\[html]** [view plain](http://blog.csdn.net/gudong2945/article/details/7330642 "view plain")[copy](http://blog.csdn.net/gudong2945/article/details/7330642 "copy")

1. &lt;property name="cityNames" value="#{cities.?\[population gt 100000].!\[name + ', ' + state]}"/&gt;

 

来源于：

[http://blog.csdn.net/gudong2945/article/details/7330642](http://blog.csdn.net/gudong2945/article/details/7330642)