Spring笔记 - Spring Expression Language (SpEL表达式)
基本表达式
数字5 <property name="count" value="#{5}"/>
联合方式 <property name="message" value="The value is #{5}"/>
浮点型 <property name="frequency" value="#{89.7}"/>
科学表达式10000.0 <property name="capacity" value="#{1e4}"/>
String <property name="name" value="#{'Chuck'}"/> <property name='name' value='#{"Chuck"}'/>
Boolean <property name="enabled" value="#{false}"/>
引用Bean <property name="instrument" value="#{saxophone}"/> 等价 <property name="instrument" ref="saxophone"/>
引用Bean属性 <property name="song" value="#{kenny.song}"/>
引用Bean方法 <property name="song" value="#{songSelector.selectSong()}"/>
进阶一 <property name="song" value="#{songSelector.selectSong().toUpperCase()}"/>
进阶二 防空指针 <property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/> 防空指针
使用静态类 T(java.lang.Math)
使用静态属性 <property name="multiplier" value="#{T(java.lang.Math).PI}"/>
使用静态方法 <property name="randomNumber" value="#{T(java.lang.Math).random()}"/>
运算表达式
+ <property name="adjustedAmount" value="#{counter.total+42}"/>
- <property name="adjustedAmount" value="#{counter.total-20}"/>
* <property name="circumference" value="#{2 *T(java.lang.Math).PI*circle.radius}"/>
/ <property name="average" value="#{counter.total/counter.count}"/>
% <property name="remainder" value="#{counter.total%counter.count}"/>
^ <property name="area" value="#{T(java.lang.Math).PI*circle.radius^2}"/>
+ overloaded <property name="fullName value="#{performer.firstName + ' ' + performer.lastName}"/>
== eq equal <property name="equal" value="#{counter.total==100}"/>
<= <propertyname="hasCapacity"value="#{counter.total le 100000}"/>
Equal == eq
Less than < lt
Less than or equals <= le
Greater than > gt
Greater than or equals >= ge
逻辑表达式
and A 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
<property name="largeCircle" value="#{shape.kind =='circle'andshape.perimeter gt 10000}"/>
<property name="outOfStock" value="#{!product.available}"/>
条件表达式 ?: <property name="instrument" value="#{songSelector.selectSong()=='JingleBells'?piano:saxophone}"/>
<propertyname="song" value="#{kenny.song !=null?kenny.song:'Greensleeves'}"/>
等价 Elvis operator <propertyname="song"value="#{kenny.song?:'Greensleeves'}"/>
正则表达式 <property name="validEmail"value= "#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}"/>
集合处理
<util:listid="cities">
<bean class="com.habuma.spel.cities.City"
p:name="Chicago"p:state="IL"p:population="2853114"/>
<bean class="com.habuma.spel.cities.City"
p:name="LasCruces"p:state="NM"p:population="91865"/>
</util:list>
List <property name="chosenCity" value="#{cities[2]}"/>
进阶 <property name="chosenCity" value="#{cities[T(java.lang.Math).random()*cities.size()]}"/>
Map <property name="chosenCity" value="#{cities['Dallas']}"/>
Properties <util:properties id="settings" location="classpath:settings.properties"/>
Properties <property name="accessToken" value="#{settings['twitter.accessToken']}"/>
systemEnvironment <property name="homePath" value="#{systemEnvironment['HOME']}"/>
systemProperties <property name="homePath" value="#{systemProperties['application.home']}"/>
String s 'This is a test'[3]
.?[] 条件选择成员 <property name="bigCities" value="#{cities.?[population gt 100000]}"/>
.^[] and .$[], for 第一个和最后一个匹配成员 <property name="aBigCity" value="#{cities.^[population gt 100000]}"/>
.![] 抽取集合 List <property name="cityNames" value="#{cities.![name]}"/>
进阶 <property name="cityNames" value="#{cities.![name+' , '+state]}"/>
进阶 <property name="cityNames" value="#{cities.?[population gt 100000].![name+' ,'+state]}"/>