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}"/>
•
SpEL:引用 Bean、属性和方法(1)
•引用其他对象:
•
•
•引用其他对象的属性
•
•
•调用其他方法,还可以链式操作
–
•算数运算符:+, -, *, /, %, ^:
•
•
•
•
•
•加号还可以用作字符串连接:
•
•比较运算符: <, >, ==, <=, >=, lt, gt, eq, le, ge
•逻辑运算符号: and, or, not, |
•
•
•if-else 运算符:?: (ternary), ?: (Elvis)
•
•
•if-else 的变体
•
•
•正则表达式:matches
•
•
SpEL:引用 Bean、属性和方法(2)
•调用静态方法或静态属性:通过 T() 调用一个类的静态方法,它将返回一个 Class Object,然后再调用相应的方法或属性:
address.java
package com.hzyc.spring.bean.spel;
/**
* @author xuehj2016
* @Title: Address
* @ProjectName spring01
* @Description: TODO
* @date 2018/12/1710:30
*/
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.java
package com.hzyc.spring.bean.spel;
/**
* @author xuehj2016
* @Title: Car
* @ProjectName spring01
* @Description: TODO
* @date 2018/12/1710:30
*/
public class Car {
private String brand;
private double price;
/**
* 轮胎的周长
*/
private double tyrePerimeter;
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 getTyrePerimeter() {
return tyrePerimeter;
}
public void setTyrePerimeter(double tyrePerimeter) {
this.tyrePerimeter = tyrePerimeter;
}
public Car() {
System.out.println("Car's Constructor...");
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
", tyrePerimeter=" + tyrePerimeter +
'}';
}
}
person.java
package com.hzyc.spring.bean.spel;
/**
* @author xuehj2016
* @Title: Person
* @ProjectName spring01
* @Description: TODO
* @date 2018/12/1710:30
*/
public class Person {
private String name;
private Car car;
/**
* 引用 address bean 的 city 属性
*/
private String city;
/**
* 根据 car 的 price 确定 info : car 的 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">
<bean id="address" class="com.hzyc.spring.bean.spel.Address">
<!--使用 spel 为属性赋一个字面值-->
<property name="city" value="#{'北京'}"/>
<property name="street" value="五道口"/>
</bean>
<bean id="car" class="com.hzyc.spring.bean.spel.Car">
<property name="brand" value="宝马"/>
<property name="price" value="300000"/>
<!--使用 spel 引用类的静态属性-->
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"/>
</bean>
<bean id="person" class="com.hzyc.spring.bean.spel.Person">
<property name="name" value="薛恒杰"/>
<!--使用 spel 来引用其他的 bean-->
<property name="car" value="#{car}"/>
<!--使用 spel 来引用其他 bean 的属性-->
<property name="city" value="#{address.city}"/>
<!--在 spel 中使用运算符-->
<property name="info" value="#{car.price>300000 ?'金领':'白领'}"/>
</bean>
</beans>
Main.java
package com.hzyc.spring.bean.spel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author xuehj2016
* @Title: Main
* @ProjectName spring01
* @Description: TODO
* @date 2018/12/1710:44
*/
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-spel.xml");
Address address = (Address) applicationContext.getBean("address");
System.out.println(address);
Car car = (Car) applicationContext.getBean("car");
System.out.println(car);
Person person = (Person) applicationContext.getBean("person");
System.out.println(person);
}
}