Spring SpEL表達式語言

Spring SpEL表達式語言

一、說在前面

Spring  表達式語言(簡稱SpEL):是一個支持運行時查詢和操作對象圖的強大的表達式語言。語法類似於 EL:SpEL 使用 #{…} 作爲定界符,所有在大框號中的字符都將被認爲是 SpEL。SpEL 爲 bean 的屬性進行動態賦值提供了便利。
通過 SpEL 可以實現:
(1)通過 bean 的 id 對 bean 進行引用;
(2)調用方法以及引用對象中的屬性;
(3)計算表達式的值;
(4)正則表達式的匹配。


二、舉例說明

1、Address、Car和Person類

Address類:

package com.at.beans.spel;

public class Address {

	private String city;
	private String street;
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	@Override
	public String toString() {
		return "Address [city=" + city + ", street=" + street + "]";
	}
	
	
}
Car類

package com.at.beans.spel;

public class Car {

	private String brand;
	private double price;
	//輪胎周長
	private double ltzhouchang;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public double getLtzhouchang() {
		return ltzhouchang;
	}
	public void setLtzhouchang(double ltzhouchang) {
		this.ltzhouchang = ltzhouchang;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + ", ltzhouchang="
				+ ltzhouchang + "]";
	}
}
Person類

package com.at.beans.spel;

public class Person {

	private String name;
	private Car car;

	//引用address bean的city屬性
	private String city;
	
	//根據car的price屬性來確定info:car的price>400000爲金領,否則爲白領
	private String info;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", car=" + car + ", city=" + city
				+ ", info=" + info + "]";
	}
}


2、配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

	<bean id="address" class="com.at.beans.spel.Address">
		<!-- 使用SpEL爲屬性賦一個字面值 -->
		<property name="city" value="#{'BeiJing'}"></property>
		
		<property name="street" value="huoying"></property>
	</bean>

	<bean id="car" class="com.at.beans.spel.Car">
		<property name="brand" value="Audi"></property>
		<property name="price" value="300000"></property>
		
		<!-- 使用SpEL引用類的靜態屬性 -->
		<property name="ltzhouchang" value="#{T(java.lang.Math).PI * 50}"></property>
	</bean>
	
	<bean id="person" class="com.at.beans.spel.Person">
		<!-- 使用SpEL來應用其他的bean -->
		<property name="car" value="#{car}"></property>
		
		<!-- 使用SpEL來應用其他的bean的屬性 -->
		<property name="city" value="#{address.city}"></property>
		
		<!-- 使用SpEL中使用運算符 -->
		<property name="info" value="#{car.price > 400000 ? '金領':'白領'}"></property>
		
		<property name="name" value="luoyepiaoxue2014"></property>
	</bean>
</beans>


3、測試函數

package com.at.beans.spel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpel {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml");
		Address address = (Address) ctx.getBean("address");
		System.out.println(address);
		
		Car car = (Car) ctx.getBean("car");
		System.out.println(car);
		
		Person person = (Person) ctx.getBean("person");
		System.out.println(person);
	}
}


4、測試結果

五月 24, 2017 9:40:45 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@427a39ea: startup date [Wed May 24 21:40:45 CST 2017]; root of context hierarchy
五月 24, 2017 9:40:45 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-spel.xml]
Address [city=BeiJing, street=huoying]
Car [brand=Audi, price=300000.0, ltzhouchang=157.07963267948966]
Person [name=luoyepiaoxue2014, car=Car [brand=Audi, price=300000.0, ltzhouchang=157.07963267948966], city=BeiJing, info=白領]


By luoyepiaoxue2014

微博地址:http://weibo.com/luoyepiaoxue2014 點擊打開鏈接

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