1、SpEL非常實用,基本特性:
a.使用Bean的ID來引用Bean
b.調用方法和訪問對象的屬性
c.對值進行算術、關系和邏輯運算
d.正則表達式匹配
e.集合操作
2、案例
<property name = "count" value = "#{5}" />
<property name = "message" value = "the value is #{5}" />
<property name = "capacity" value = "#{1e4}" />
<property name = "name" vaue = "#{'Chuck'}" />//注意單雙引號的使用
<property name = "name" value = '#{"Chuck"}' />
<property name = "enabled" value = "#{false}" />
<property name = "instrument" value = "#{saxophone}" />
<==>
<property name = "instrument" ref= "saxophone" />
<property name = "song" value = "#{kenny.song}" />
<property name = "song" value = "#{kenny.getSong()}" />
<property name = "song" value = "#{kenny.getSong().toUpperCase()}" />
<property name = "song" value="#{kenny.getSong()?.toUpperCase()}"http://防止空指針異常:?.
<property name = "pi" value = "#{T(java.lang.Math).PI}" /> //T運算符會調用類作用域的方法和常量
<property name = "randomNumber" value = "#{T(java.lang.Math).random()}" />
<property name = "adjust" value = "#{counter.total + 42}" /> //加法運算
<property name = "area" value = "#{T(java.lang.Math).PI * circle.radius ^ 2}" /> //乘方運算
<property name = "fullName" value = "#{performer.firstName + ' ' + performer.lastName}" />字符串連接
<property name = "equal" value = "#{counter.total == 100}" />
<property name = "hasCa" value = "#{counter.total le 10000}" /> //== eq,< lt,<= le,> gt,>= ge
<property name = "aaa" value = "#{shape.kind == 'circle' and shapeperimter gt 1000}" /> //and,or,not或!
<property name = "validEmail" value = "#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+//.com'}" /> //匹配正則表達式
<property name = "choseCity" value = "#{cities[2]}" />//list集合
<property name = "choeCity" value = "#{cities[T(java.lang.Math).random()*cities.size()]}" />//list集合
<property name = "choseCity" value = "#{cities['Dallas']}" /> //Map集合
<util:properties id = "settings" location = "classpath:settings.properties" />
<property name = "accessToken" value = "#{settings['twitter.accessToken']}" />
<property name = "homePath" value = "#{systemEnvironment['HOME']}" />//systemEnvironment包含了應用程序所在機器上的所有環境變量
<property name = "homePath" value = "#{systemProperties['application.home']}" />//systemProperties包含了Java應用程序啟動時所設置的所有屬性
<property name = "bigCities" value = "#{cities.?[population gt 1000]}" />//.?[]查詢運算符,創建一個新的集合;.^[]和.$[]從集合中查詢出第一個匹配項和最后一個匹配項
<property name = "cityNames" value = "#{cities.![name]}" />//.![]投影運算符:從集合的每一個成員中選擇特定的屬性放入一個新的集合中
<property name = "cityNames" value = "#{cities.![name+' , '+state]}" />
<property name = "cityNames" value = "#{cities.?[population gt 10000].![name + ','+state]}" />
新聞熱點
疑難解答