spring表達式語言(SpEL)簡述及Hello World示例

作爲spring的基礎模塊之一,spring表達式語言模塊(Spring Expression Language,簡稱SpEL)在運行時提供了查詢和操作一個對象圖的強大的表達式語言。

Spring EL既可以通過XML被配置,也可以通過註解來進行配置。下面通過簡單的例子來示範如何通過兩種不同的方式配置SpEL從而注入String、整型和javabean數據。


  1. Spring EL依賴

    項目通過maven進行管理,在maven的pom.xml配置文件中聲明spring的核心依賴,maven將自動下載依賴包。(若不採用maven進行項目管理,可直接下載並添加jar包至buildpath)

pom.xml:

	<properties>
		<spring.version>3.1.4.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

定義兩個簡單的bean,後面將分別通過XML方式和註解方式用SpEL爲bean的屬性注入值

public class Customer {

	private Item item;

	private String itemName;
}

public class Item {

	private String name;

	private int qty;
}


3. 通過XML方式配置Spring EL

SpEL表達式的格式如下#{SpEL表達式},XML bean定義文件中配置SpEL,示例如下

<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}-將itemBean注入到customerBean的item屬性中

#{itemBean.name}-將itemBean的name屬性注入到customerBean的itemName屬性中


4. 通過註解配置Spring EL

注意:要通過註解配置SpEL,必須將bean通過註解方式註冊。如果在XML配置文件中註冊bean,同時在java類中定義@Value,@Value將會失效。

註解方式配置bean如下:

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;

	//...}
	
	

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;
	}

	//...}

在配置文件中允許自動掃描:

<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/>
</beans>

註解方式中,通過@Value定義springEL。通過上述的配置,string值和整型值直接被注入到itemBean的屬性中,然後itemBean被注入到customerBean的屬性中。


5. 輸出

運行下述代碼,無論是XML方式還是註解方式都會輸出同樣的結果

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);
	}}

輸出:

Customer [item=Item [name=itemA, qty=10], itemName=itemA]

類似JSF EL,除了直接設置值之外,SpEL還支持其他多種表達式,包括關係表達式、正則表達式、數組、列表等等。


參考

http://www.mkyong.com/spring3/spring-el-hello-world-example/

http://examples.javacodegeeks.com/enterprise-java/spring/spel/spring-expression-language-example/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章