Spring EL hello world example

The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation time. In addition, all Spring expressions are available via XML or annotation.
In this tutorial, we show you how to use Spring Expression Language(SpEL), to inject String, integer and bean into property, both in XML and annotation.

1. Spring EL Dependency

Declares the core Spring jars in Maven pom.xml file, it will download the Spring EL dependencies automatically.
File : pom.xml

<?xml version="1.0"?>
<properties>
    <spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
    <!-- Spring 3 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

2. Spring Beans

Two simple beans, later use SpEL to inject values into property, in XML and annotation.

package com.mkyong.core;
public class Customer { 
    private Item item; 
    private String itemName;
}
package com.mkyong.core;
public class Item { 
    private String name; 
    private int qty;
}

3. Spring EL in XML

The SpEL are enclosed with #{ SpEL expression }, see following example in XML bean definition file.

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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">
    <bean id="itemBean" class="com.mkyong.core.Item">
        <property name="name" value="itemA" />
        <property name="qty" value="10" />
    </bean>
    <bean id="customerBean" class="com.mkyong.core.Customer">
        <property name="item" value="#{itemBean}" />
        <property name="itemName" value="#{itemBean.name}" />
    </bean>
</beans>

#{itemBean} – inject “itemBean” into “customerBean” bean’s “item” property.
#{itemBean.name} – inject “itemBean”‘s “name” property into “customerBean” bean’s “itemName” property.

4. Spring EL in Annotation

See equivalent version in annotation mode.

Note To use SpEL in annotation, you must register your component via annotation. If you register your bean in XML and define @Value in Java class, the @Value will failed to execute.

package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer { 
    @Value("#{itemBean}") 
    private Item item; 
    @Value("#{itemBean.name}") 
    private String itemName; 
    //...
}
package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("itemBean")
public class Item { 
    @Value("itemA") 
    //inject String directly 
    private String name; 
    @Value("10") 
    //inject interger directly 
    private int qty; 
    public String getName() { 
        return name; 
    } 
    //...
}

Enable auto component scanning.

<beans xmlns="http://www.springframework.org/schema/beans" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns:context="http://www.springframework.org/schema/context" 
             xsi:schemaLocation="http://www.springframework.org/schema/beans    
                                               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                                               http://www.springframework.org/schema/context    
                                               http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <context:component-scan base-package="com.mkyong.core" />
</beans>

In annotation mode, you use @Value to define Spring EL. In this case, you inject a String and Integer value directly into the “itemBean“, and later inject the “itemBean” into “customerBean” property.

5. Output

Run it, both SpEL in XML and annotation are display the same result :

package com.mkyong.core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App { 
    public static void main(String[] args) { 
         ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); 
        Customer obj = (Customer) context.getBean("customerBean"); 
        System.out.println(obj); 
    }
}

Output

Customer [item=Item [name=itemA, qty=10], itemName=itemA]
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子诲侮,更是在濱河造成了極大的恐慌,老刑警劉巖突照,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異氧吐,居然都是意外死亡讹蘑,警方通過查閱死者的電腦和手機末盔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來座慰,“玉大人陨舱,你說我怎么就攤上這事“孀校” “怎么了游盲?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蛮粮。 經常有香客問我益缎,道長,這世上最難降的妖魔是什么然想? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任莺奔,我火速辦了婚禮,結果婚禮上变泄,老公的妹妹穿的比我還像新娘令哟。我一直安慰自己,他們只是感情好妨蛹,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布屏富。 她就那樣靜靜地躺著,像睡著了一般蛙卤。 火紅的嫁衣襯著肌膚如雪役听。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天表窘,我揣著相機與錄音,去河邊找鬼甜滨。 笑死乐严,一個胖子當著我的面吹牛,可吹牛的內容都是我干的衣摩。 我是一名探鬼主播昂验,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼艾扮!你這毒婦竟也來了既琴?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤泡嘴,失蹤者是張志新(化名)和其女友劉穎甫恩,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體酌予,經...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡磺箕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年奖慌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片松靡。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡简僧,死狀恐怖,靈堂內的尸體忽然破棺而出雕欺,到底是詐尸還是另有隱情岛马,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布屠列,位于F島的核電站啦逆,受9級特大地震影響,放射性物質發(fā)生泄漏脸哀。R本人自食惡果不足惜蹦浦,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望撞蜂。 院中可真熱鬧盲镶,春花似錦、人聲如沸蝌诡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浦旱。三九已至宇色,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間颁湖,已是汗流浹背宣蠕。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留甥捺,地道東北人抢蚀。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像镰禾,于是被迫代替她去往敵國和親皿曲。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內容