Spring學習筆記之--SpEL
本文內容來源於尚矽谷視訊教程,僅作為本文學習筆記使用,請勿轉載!
1.什麼是SpEL?
Spring表示式語言,簡稱SpEL,是一個支援執行時查詢和操作物件的強大的表示式語言。語法類似於EL,格式為#{...}。SpEL為Bean的屬性進行動態賦值提供了便利。
2.可以做什麼事?
1)通過Bean的id對bean進行引用
2)呼叫方法和引用物件中的屬性
3)計算表示式的值
4)正則表示式的匹配
3.怎麼用?
3.1字面量
--整數:<property name="num" value="#{10}"></property>
--小數:<property name="num" value="#{10.12}"></property>
--科學計數法:<property name="num" value="#{1e3}"></property>
--字串:<property name="street" value="#{‘解放路’}"></property> //注意字串需要加單或雙引號
--Boolean:<property name="isTure" value="#{false}"></property>
3.2 引用bean 屬性 和方法
--引用其他物件:<property name="car" value="#{car}"></property>等同於<property name="car" ref="car"></property>
--引用其他物件屬性:<property name="car" value="#{car.price}"></property>
--引用其他物件方法,可以鏈式操作:<property name="car" value="#{car.toString().toUpperCase()}"></property>
3.3 支援的運算式
--邏輯運算子號:and,or,not,!<property name="car" value="#{car.price gt 200000 and car.color == 'red' or car.brand == 'bmw'}"></property>
--if-else 運算子:<property name="info" value="#{car.price > 300000 ? '金領':'白領' }"></property>
--正則表示式:<property name="check" value="#{admin.usernamematches '[a-zA-Z._%+-]'}"></property>
3.4 呼叫靜態方法
--呼叫靜態方法或者靜態屬性:通過T()來呼叫一個類的靜態方法或者屬性,示例如下:
<property name="length" value="#{T(java.lang.Math).PI*100}"></property>
4 示例程式碼
4.1 建一個Car類
public class Car {
private String brand;
private double price;
private double length;
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 getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", length=" + length + "]";
}
}
4.2建一個Address類
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 + "]"; } }
4.3 建一個Person類
public class Person { private String name; private Address address; private Car car; private String info; public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [name=" + name + ", address=" + address + ", car=" + car + ", info=" + info + "]"; } }4.4 建一個名為bean-SpEl的xml文件,配置如下
<?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.lg.spring.SpEL.Address">
<property name="city" value="#{'武漢'}"></property> 使用SpEL的字面量賦值方法
<property name="street" value="洪山區"></property>
</bean>
<bean id="car" class="com.lg.spring.SpEL.Car">
<property name="brand" value="bmw"></property>
<property name="price" value="300000"></property>
<property name="length" value="#{T(java.lang.Math).PI*100}"></property> 使用SpEL引用類的靜態屬性
</bean>
<bean id="person" class="com.lg.spring.SpEL.Person">
<property name="name" value="lgzy"></property>
<property name="address" value="#{address}"></property> 使用SpEL引用其他bean物件
<property name="car" ref="car"></property>
<property name="info" value="#{car.price>280000?'金領':'白領'}"></property> 使用SpEL的條件判斷語句
</bean>
</beans>
4.5 建一個測試類Main
package com.lg.spring.SpEL;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-SpEL.xml");
Person person = (Person)ctx.getBean("person");
System.out.println(person);
}
}
4.6 輸出結果