- Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。
- 语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL
- SpEL 为 bean 的属性进行动态赋值提供了便利
- 通过 bean 的 id 对 bean 进行引用
- 调用方法以及引用对象中的属性
- 计算表达式的值
- 正则表达式的匹配
字面量的表示:
- 整数:<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}"/>
- 引用其他对象:
- 引用其他对象的属性
- 调用其他方法,还可以链式操作
- 算数运算符:+, -, *, /, %, ^:
- 加号还可以用作字符串连接:
- 比较运算符: <, >, ==, <=, >=, lt, gt, eq, le, ge
代码:
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 + "]"; } }
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 getTyrePerimeter() { return tyrePerimeter; } public void setTyrePerimeter(double tyrePerimeter) { this.tyrePerimeter = tyrePerimeter; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter=" + tyrePerimeter + "]"; } public Car() { System.out.println("car的构造器"); } }
public class Person { private String name; private Car car; 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 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; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [name=" + name + ", car=" + car + ", city=" + city + ", info=" + info + "]"; } }
<?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.atguigu.spring.beans.spel.Address"> <property name="city" value="#{'BeiJing'}"></property> <property name="street" value="朝阳路"></property> </bean> <bean id="car" class="com.atguigu.spring.beans.spel.Car"> <property name="brand" value="jili"></property> <property name="price" value="300000"></property> <property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property> </bean> <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> </bean> </beans>
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext apc = new ClassPathXmlApplicationContext("beans-sqel.xml"); Address address = (Address) apc.getBean("address"); System.out.println(address); Car car = (Car) apc.getBean("car"); System.out.println(car); Person person = (Person) apc.getBean("person"); System.out.println(person); } }
结果:
car的构造器 Address [city=BeiJing, street=朝阳路] Car [brand=jili, price=300000.0, tyrePerimeter=251.32741228718345] Person [name=null, car=Car [brand=jili, price=300000.0, tyrePerimeter=251.32741228718345], city=BeiJing, info=白领]