详情参考Spring 官网
- SpEl:Spring Expression Language,是一个支持运行时查询和操作对象图的强大语言。
- 语法类似于EL,SpEL使用#{…}作为定界符,所有在大括号里的字符都被认为是SpEl。
- SpEl 为Bean的属性进行动态复赋值。
- SpEl 可以实现:
- 通过bean的id对bean进行引用
- 调用方法以及引用对象中的属性
- 计算表达式的值
- 正则表达式的匹配
SpEl 用法
字面量的表示:
整数:<property name="count" value="#{23}"></property>
小数:<property name="price" value="#{23.3}"></property>
科学计数法:<property name="capacity" value="#{1e4}"></property>
String(用单引号或者双引号引起来)<property name="name" value="#{'crystal'}"></property>
或者<property name='name' value='#{"crystal"}'></property>
Boolean:<property name="count" value="#{false}"></property>
引用Bean、属性和方法
* 引用其他对象
<property name="prefix" value="#{prefixGenerator}"></property>
* 引用其他对象的属性
<property name="suffix" value="#{sequenceGenerator.suffix}"></property>
* 调用其他方法,可以链式操作
<property name="suffix" value="#{sequenceGenerator.toString().toUpperCase()}"></property>
运算符操作 +、-、*、/、%、^
<property name="count" value="#{num.total + 2}"></property>
<property name="area" value="#{T(java.lang.Math).PI * circle.redius ^ 2}"></property>
字符串连接
<property name="name" value="#{performer.firstName + ' ' + performer.lastName}"></property>
比较运算符 >、<、==、>=、<=、lt、gt、eq、le、ge
<property name="equal" value="#{counter.total == 100}"></property>
逻辑运算符 and、or、not、!
<property name="judge" value="#{shape.kind == 'circle' and shape.perimeter gt 100}"></property>
if-else运算符 ?:
<property name="type" value="#{shape.kind == 'circle'?'圆形':'方形'}"></property>
正则表达式 matches
<property name="match" value="#{email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}'}"></property>
调用静态函数和静态方法:通过T()调用一个类的静态方法,它将返回一个Class Object,然后再调用相应的属性和方法。
<property name="initValue" value="#{T(java.lang.Math).PI}"></property>
例子
beans-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.spring.test.spel.Address">
<!-- 使用 SpEL 设置String属性值 -->
<property name="city" value="#{'Suzhou'}"></property>
<property name="street" value="#{'Shilu'}"></property>
</bean>
<bean id="car" class="com.spring.test.spel.Car">
<property name="brand" value="#{'Audi'}"></property>
<property name="price" value="#{200000}"></property>
</bean>
<bean id="person" class="com.spring.test.spel.Person">
<property name="name" value="#{'crystal'}"></property>
<!-- 引用bean对象属性值 -->
<property name="city" value="#{address.city}"></property>
<property name="car" value="#{car}"></property>
</bean>
</beans>
Address.java
package com.spring.test.spel;
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.spring.test.spel;
public class Car {
private String brand;
private double price;
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;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
Person.java
package com.spring.test.spel;
public class Person {
private String name;
private Car car;
private String 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 getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Person [name=" + name + ", car=" + car + ", city=" + city + "]";
}
}
Main.java
package com.spring.test.spel;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
@Test
public void testAutoWire() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans-spel.xml");
Person person = (Person) context.getBean("person");
System.out.println(person);
}
}