简介
Spring表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。语法类似EL:SpEL使用#{…}作为定界符,所有在大括号中的字符都将被认为SpEL。SpEL为bean属性的动态赋值提供了便利。
通过 SpEL 可以实现:
通过 bean 的 id 对 bean 进行引用
调用方法以及引用对象中的属性
计算表达式的值
正则表达式的匹配
1.字面量的赋值
<!--整数-->
<property name="count" value="#{5}"/>
<!--小数-->
<property name="frequency" value="#{1.2}">
<!--科学计数法-->
<property name="capacity" value="#{le4}"/>
<!--String 可以使用单引号或者双引号作为字符串的定界符号-->
<property name="name" value="#{'Tom'}"/>或者是
<property name="name" value='#{"Tom"}'/>
<!--Boolean-->
<property name="flag" value="#{false}"/>
2.引用其他对象
3.SpEL支持的运算符号
算数运算符:+,-,*,/,%,^
加号还可以作为字符串连接
<property name="infor" value="#{'name= '+'Tome'}"/>
比较运算符
<property name="equal" value="#{couter.total == 100}" />
逻辑运算符:and,or,not,|
<property name="aaa" value="#{shape.kind =='circle' and shape.price ==1000}"/>
<property name="bb" value="!shape.isAvailable"/>
<property name="cc" value="not shape.isAvailable"/>
if-else运算符: ?…:…
<property name="infor" value="car.price > 300000 ? '金领' : '白领' "/>
调用静态方法或静态属性:通过 T() 调用一个类的静态方法,它将返回一个 Class Object,然后再调用相应的方法或属性:
<property name="initValue" value="#{T(java.lang.Math).PI }"/>
示例代码:
//准备一个Car类
public class Car {
private String brand;
private double price;
private double perimeter;
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 getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", perimeter=" + perimeter + "]";
}
public Car(String brand, double price) {
super();
this.brand = brand;
this.price = price;
}
public Car() {}
//准备一个Address类
public class Address {
private String city;
private String stree;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStree() {
return stree;
}
public void setStree(String stree) {
this.stree = stree;
}
@Override
public String toString() {
return "Address [city=" + city + ", stree=" + stree + "]";
}
public Address(String city, String stree) {
super();
this.city = city;
this.stree = stree;
}
public Address() {}
//准备一个Perosn类
public class Person {
private String name;
private Car car;
private String info;
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
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 getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Person [name=" + name + ", car=" + car + ", info=" + info + ", city=" + city + "]";
}
public Person(String name, Car car, String info) {
super();
this.name = name;
this.car = car;
this.info = info;
}
public Person() {}
准备xml文件,配置bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id="car" class="Car的全类名">
<property name="brand" value="#{'audi'}"></property>
<property name="price" value="#{600000}"></property>
<property name="perimeter" value="#{T(java.lang.Math).PI * 80}"></property>
</bean>
<bean id="address" class="Address的全类名">
<property name="city" value="beijing">
<property name="stree" vlaue="wudaokou"></property>
</bean>
<property id="person" class="Person的全类名">
<property name="name" value="Tom"></property>
<property name="car" value="#{car}"></property>
<property name="city" value="#{address.city}"></property>
<property name="info" value="#{car.price > 300000 ? '金领' : '白领'}"></property>
</property>
</beans>