---
title: "Spring笔记 - Spring Expression Language (SpEL表达式)"
author: "txidol"
date: "2013-06-19T16:17:00Z"
tags: []
slug: "spring-spring-expression-language-spel"
draft: false
lastmod: "2013-11-21T14:06:04Z"
id: 1978420
url: "https://txidol.iteye.com/blog/1978420"
---

# 基本表达式

数字5 &lt;property name="count" value="#{5}"/&gt;

联合方式 &lt;property name="message" value="The value is #{5}"/&gt;

浮点型 &lt;property name="frequency" value="#{89.7}"/&gt;

科学表达式10000.0 &lt;property name="capacity" value="#{1e4}"/&gt;

String &lt;property name="name" value="#{'Chuck'}"/&gt; &lt;property name='name' value='#{"Chuck"}'/&gt;

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

引用Bean &lt;property name="instrument" value="#{saxophone}"/&gt; 等价 &lt;property name="instrument" ref="saxophone"/&gt;

引用Bean属性 &lt;property name="song" value="#{kenny.song}"/&gt;

引用Bean方法 &lt;property name="song" value="#{songSelector.selectSong()}"/&gt;

进阶一 &lt;property name="song" value="#{songSelector.selectSong().toUpperCase()}"/&gt;

进阶二 防空指针 &lt;property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/&gt; 防空指针

使用静态类 T(java.lang.Math)

使用静态属性 &lt;property name="multiplier" value="#{T(java.lang.Math).PI}"/&gt;

使用静态方法 &lt;property name="randomNumber" value="#{T(java.lang.Math).random()}"/&gt;

# 运算表达式

+&lt;property name="adjustedAmount" value="#{counter.total+42}"/&gt;

\- &lt;property name="adjustedAmount" value="#{counter.total-20}"/&gt;

\* &lt;property name="circumference" value="#{2 \*T(java.lang.Math).PI\*circle.radius}"/&gt;

/ &lt;property name="average" value="#{counter.total/counter.count}"/&gt;

% &lt;property name="remainder" value="#{counter.total%counter.count}"/&gt;

^ &lt;property name="area" value="#{T(java.lang.Math).PI\*circle.radius^2}"/&gt;

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

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

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

Equal== eq  
Less than &lt; lt  
Less than or equals &lt;= le  
Greater than &gt; gt  
Greater than or equals &gt;= ge

逻辑表达式

andA logical AND operation; both sides must evaluate true for the expression to be true  
or A logical OR operation; either side must evaluate true for the expression to be true  
not or ! A logical NOT operation; negates the target of the operation

&lt;property name="largeCircle" value="#{shape.kind =='circle'andshape.perimeter gt 10000}"/&gt;

&lt;property name="outOfStock" value="#{!product.available}"/&gt;

条件表达式 ?: &lt;property name="instrument" value="#{songSelector.selectSong()=='JingleBells'?piano:saxophone}"/&gt;

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

等价 Elvis operator &lt;propertyname="song"value="#{kenny.song?:'Greensleeves'}"/&gt;

正则表达式 &lt;property name="validEmail"value= "#{admin.email matches '\[a-zA-Z0-9.\_%+-]+@\[a-zA-Z0-9.-]+\\\\.com'}"/&gt;

# 集合处理

&lt;util:listid="cities"&gt;  
&lt;bean class="com.habuma.spel.cities.City"  
p:name="Chicago"p:state="IL"p:population="2853114"/&gt;  
&lt;bean class="com.habuma.spel.cities.City"  
p:name="LasCruces"p:state="NM"p:population="91865"/&gt;  
&lt;/util:list&gt;

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

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

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

Properties &lt;util:properties id="settings" location="classpath:settings.properties"/&gt;

Properties &lt;property name="accessToken" value="#{settings\['twitter.accessToken']}"/&gt;

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

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

String s 'This is a test'\[3]

.?\[] 条件选择成员 &lt;property name="bigCities" value="#{cities.?\[population gt 100000]}"/&gt;

.^\[] and .$\[], for 第一个和最后一个匹配成员 &lt;property name="aBigCity" value="#{cities.^\[population gt 100000]}"/&gt;

.!\[] 抽取集合 List &lt;property name="cityNames" value="#{cities.!\[name]}"/&gt;

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

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