天天看點

Spring學習第九天:Spring表達式語言Spel

Spring 表達式語言(簡稱SpEL):是一個支援運作時查詢和操作對象圖的強大的表達式語言。

文法類似于 EL:SpEL 使用 #{…} 作為定界符,所有在大框号中的字元都将被認為是 SpEL

SpEL 為 bean 的屬性進行動态指派提供了便利

通過 SpEL 可以實作:

- 通過 bean 的 id 對 bean 進行引用

- 調用方法以及引用對象中的屬性

- 計算表達式的值

- 正規表達式的比對

Spel字面值

字面量的表示:

- 整數:< property name=”count” value=”#{5}”/>

- 小數:< property name=”frequency” value=”#{89.7}”/>

- 科學計數法:< property name=”capacity” value=”#{1e4}”/>

- String可以使用單引号或者雙引号作為字元串的定界符号:< property name=“name” value=”#{‘Chuck’}”/> 或 < property name=’name’ value=’#{“Chuck”}’/>

- Boolean:< property name=”enabled” value=”#{false}”/>

引用 Bean、屬性和方法(1)

引用其他對象:

Spring學習第九天:Spring表達式語言Spel

引用其他對象的屬性

Spring學習第九天:Spring表達式語言Spel

調用其他方法,還可以鍊式操作

Spring學習第九天:Spring表達式語言Spel
Spring學習第九天:Spring表達式語言Spel

SpEL支援的運算符号

算數運算符:+, -, *, /, %, ^

Spring學習第九天:Spring表達式語言Spel

加号還可以用作字元串連接配接

Spring學習第九天:Spring表達式語言Spel

比較運算符: <, >, ==, <=, >=, lt, gt, eq, le, ge

Spring學習第九天:Spring表達式語言Spel
Spring學習第九天:Spring表達式語言Spel

邏輯運算符号: and, or, not, |

Spring學習第九天:Spring表達式語言Spel

if-else 運算符:?: (ternary), ?: (Elvis)

Spring學習第九天:Spring表達式語言Spel

if-else 的變體

Spring學習第九天:Spring表達式語言Spel

正規表達式:matches

Spring學習第九天:Spring表達式語言Spel

引用 Bean、屬性和方法(2)

調用靜态方法或靜态屬性:通過 T() 調用一個類的靜态方法,它将傳回一個 Class Object,然後再調用相應的方法或屬性:

Spring學習第九天:Spring表達式語言Spel

下面通過具體的代碼來解釋上面的内容

建立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 + "]";
    }
}
           

建立Car類

public class Car {
    private String brand;
    private double price;

    //輪胎周長
    private double tyerPerimeter;

    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 getTyerPerimeter() {
        return tyerPerimeter;
    }
    public void setTyerPerimeter(double tyerPerimeter) {
        this.tyerPerimeter = tyerPerimeter;
    }

    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price + ", tyerPerimeter=" + tyerPerimeter + "]";
    }
    public Car() {
        // TODO Auto-generated constructor stub
        System.out.println("Car's constructor...");
    }
}
           

建立Person類

package com.atguigu.spring.beans.spel;

public class Person {
    private String name;
    private Car car;

    // 引用address bean的city屬性
    private String city;

    // 根據car的price确定info, 如果price > 300000, 為金領
    // 否則為白領
    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 + "]";
    }
}
           

建立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">

</beans>
           

1: 使用spel為屬性賦一個字面值

<bean id="address" class="com.atguigu.spring.beans.spel.Address">
    <property name="city" value="#{'Beijing'}"></property>
    <property name="street" value="WuDaoKou"></property>
</bean>
           

調用該bean

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

輸出結果如下

2- 使用Spel引用類的靜态屬性

<bean id="car" class="com.atguigu.spring.beans.spel.Car">
    <property name="brand" value="Audi"></property>
    <property name="price" value="500000"></property>
    <property name="tyerPerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
</bean>
           

調用該bean

Car car = ctx.getBean(Car.class);
System.out.println(car);
           

輸出結果

3- 使用Spel來引用其他的bean,其他bean的屬性,以及如何在spel中使用運算符

<bean id="person" class="com.atguigu.spring.beans.spel.Person">
    <property name="car" value="#{car}"></property>
    <property name="city" value="#{address.city}"></property>
    <property name="info" value="#{car.price > 300000 ? '金領' : '白領'}"></property>
    <property name="name" value="Tom"></property>
</bean>
           

調用該bean

Person person = ctx.getBean(Person.class);
System.out.println(person);
           

輸出結果如下