Spring3引入了Spring表達式語言(Spring Expression Language,SpEL)。SpEL是一種強大的、簡潔的裝配Bean的方式,它經過運行期執行的表達式將值裝配到Bean的屬性或構造器參數中。
字面值
咱們能夠在<property>元素的value屬性中使用#{}界定符將值裝配到Bean的屬性中。java
<property name="count" value="#{5}" />
浮點型數字同樣能夠出如今SpEL表達式中。正則表達式
<property name="frequency" value="#{89.7}" />
表達式中的數字也能夠實用科學計數法。spring
<property name="capacity" value="#{1e4}" />
這裏將capacity屬性設爲了10000.0。
String類型的字面值能夠使用單引號或者雙引號做爲字符串界定符。dom
<property name="name" value="#{'moonlit'}" />
或者ide
<property name="name" value='#{"moonlit"}' />
還能夠使用布爾值true和false。this
<property name="enabled" value="#{true}" />
引用Bean
新建Poem類。spa
package com.moonlit.myspring; public class Poem { private static String[] LINES = { "牀前明月光,", "疑是地上霜。", "舉頭望明月,", "低頭思故鄉。", }; public void perform() { for (String line : LINES) System.out.println(line); } }
新建Poet類。3d
package com.moonlit.myspring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Poet { private Poem poem; public void recite() { System.out.println("the poet begin to recite..."); poem.perform(); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "spring-idol.xml"); Poet poet = (Poet) context.getBean("poet"); poet.recite(); } public Poem getPoem() { return poem; } public void setPoem(Poem poem) { this.poem = poem; } }
並在xml文件中聲明他們對應的Bean。指針
<bean id="poem" class="com.moonlit.myspring.Poem" /> <bean id="poet" class="com.moonlit.myspring.Poet"> <property name="poem" value="#{poem}"> </bean>
poet經過SpEL得到了poem這個Bean。
此時運行Poet程序獲得結果以下:code
the poet begin to recite... 牀前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。
還能夠經過SpEL得到bean的對象。
<property name="poem" value="#{poet.poem}" />
經過這句話就得到了poet的poem。
還能夠經過SpEL得到bean的對象。
<property name="poem" value="#{poet.getPoem()}" />
經過這句話經過poet.getPoem()得到poet的poem。
在SpEL中避免拋出空指針異常(NullPointException)的方法是使用null-safe存取器:
<property name="song" value="#{songSelector.selectSong()?.toUpperCase()}" />
這裏咱們使用 ?. 運算符代替點(.)來訪問toUpperCase()方法。在訪問郵編方法以前,該運算符會確保左邊項的值不爲null。因此,若是selectorSong返回null,SpEL就再也不嘗試調用toUpperCase()方法。
操做類
在SpEL中,使用T()運算符會調用類做用域的方法和常量。例如,在SpEL中使用Java的Math類,咱們能夠像下面的示例這樣使用T()運算符:
T(java.lang.Math)
T()運算符的結果會返回一個java.lang.Math類對象。
裝配PI或者一個隨機值的配置方法以下:
<property name="multiplier" value="#{T(java.lang.Math).PI}" /> <property name="randomNumber" value="#{T(java.lang.Math).random()}" />
SpEL上還能夠執行bean值和數值之間的多種運算。
這裏我定義一個Circle類:
package com.moonlit.myspring; public class Circle { private double radius; public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } }
它的Bean聲明以下:
<bean id="circle" class="com.moonlit.myspring.Circle"> <property name="radius" value="2.1" /> </bean>
使用以下語句進行SpEL的數值運算示例:
<property name="total" value="#{circle.radius + 100.0}" />
「+」還能夠進行字符串的鏈接。
比較值
<property name="hasCapacity" value="#{circle.radius lt 3.3}" />
操做一個表達式的值:
eq(==),lt(<),le(<=),gt(>),ge(>=)。
邏輯表達式:
and,or,not或!。
條件運算符:使用三元運算符
<property name="instrument" value="#{sonSelector.selecSOng()=='Jingle Bells'?piano:saxophone}" />
一個常見的三元運算符的使用場景是檢查一個值是否爲null。
<property name="song" value="#{kenny.song != null ? kenny.song : 'Greensleeves'}" />
雖然以上配置能夠正常工做,但這裏kenny.song的引用重複了兩次。SpEL提供了三元運算符的變體來簡化表達式:
<property name="song" value="#{kenny.song ?: 'Greensleeves'}" />
在以上示例中,若是kenny.song不爲null,那麼表達式的求值結果是kenny.song,不然就是"Greensleeves"。當咱們以這種方式使用時,「?:」一般被稱爲elvis運算符。這個名字的來歷是,使用這個運算符來表示微笑表情(?:) ,頭左轉90度看)時,問號看起來像貓王(Elvis Presley)的頭髮。
SpEL支持正則表達式匹配
<property name="validEmail" value="#{admin.email matches '[a-zA-Z0-9._%+_]+@[a-zA-Z0-9.-]+\\.com'}" />
Hero類用於演示上述的大部分效果:
package com.moonlit.myspring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Hero { private int count; private double frequency; private double capacity; private String name; private boolean enabled; private Poem poem; private double multiplier; private double randomNumber; private double total; private boolean hasCapacity; public void perform() { System.out.println("count = " + count); System.out.println("frequency = " + frequency); System.out.println("capacity = " + capacity); System.out.println("name = " + name); System.out.println("enabled = " + enabled); System.out.println("hero begin to recite..."); poem.perform(); System.out.println("pi = " + multiplier); System.out.println("random number = " + randomNumber); System.out.println("total = " + total); System.out.println("hasCapacity = " + hasCapacity); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "spring-idol.xml"); Hero hero = (Hero) context.getBean("hero"); hero.perform(); } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public double getFrequency() { return frequency; } public void setFrequency(double frequency) { this.frequency = frequency; } public double getCapacity() { return capacity; } public void setCapacity(double capacity) { this.capacity = capacity; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public Poem getPoem() { return poem; } public void setPoem(Poem poem) { this.poem = poem; } public double getMultiplier() { return multiplier; } public void setMultiplier(double multiplier) { this.multiplier = multiplier; } public double getRandomNumber() { return randomNumber; } public void setRandomNumber(double randomNumber) { this.randomNumber = randomNumber; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } public boolean isHasCapacity() { return hasCapacity; } public void setHasCapacity(boolean hasCapacity) { this.hasCapacity = hasCapacity; } }
spring-idol.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-init-method="defaultBorn" default-destroy-method="defaultDead" > <bean id="poem" class="com.moonlit.myspring.Poem" /> <bean id="poet" class="com.moonlit.myspring.Poet"> <property name="poem" value="#{poem}" /> </bean> <bean id="circle" class="com.moonlit.myspring.Circle"> <property name="radius" value="2.1" /> </bean> <bean id="hero" class="com.moonlit.myspring.Hero"> <property name="count" value="#{5}" /> <property name="frequency" value="#{89.7}" /> <property name="capacity" value="#{1e4}" /> <!-- <property name="name" value="#{'moonlit'}" /> --> <property name="name" value='#{"moonlit"}' /> <property name="enabled" value="#{true}" /> <!-- <property name="poem" value="#{poet.poem}" /> --> <property name="poem" value="#{poet.getPoem()}" /> <property name="multiplier" value="#{T(java.lang.Math).PI}" /> <property name="randomNumber" value="#{T(java.lang.Math).random()}" /> <property name="total" value="#{circle.radius + 100.0}" /> <property name="hasCapacity" value="#{circle.radius lt 3.3}" /> </bean> </beans>