本文内容来源于尚硅谷视频教程,仅作为本文学习笔记使用,请勿转载!
1.什么是SpEL?
Spring表达式语言,简称SpEL,是一个支持运行时查询和操作对象的强大的表达式语言。语法类似于EL,格式为#{...}。SpEL为Bean的属性进行动态赋值提供了便利。
2.可以做什么事?
1)通过Bean的id对bean进行引用
2)调用方法和引用对象中的属性
3)计算表达式的值
4)正则表达式的匹配
3.怎么用?
3.1字面量
--整数:<property name="num" value="#{10}"></property>
--小数:<property name="num" value="#{10.12}"></property>
--科学计数法:<property name="num" value="#{1e3}"></property>
--字符串:<property name="street" value="#{‘解放路’}"></property> //注意字符串需要加单或双引号
--Boolean:<property name="isTure" value="#{false}"></property>
3.2 引用bean 属性 和方法
--引用其他对象:<property name="car" value="#{car}"></property>等同于<property name="car" ref="car"></property>
--引用其他对象属性:<property name="car" value="#{car.price}"></property>
--引用其他对象方法,可以链式操作:<property name="car" value="#{car.toString().toUpperCase()}"></property>
3.3 支持的运算式
--逻辑运算符号:and,or,not,!<property name="car" value="#{car.price gt 200000 and car.color == 'red' or car.brand == 'bmw'}"></property>
--if-else 运算符:<property name="info" value="#{car.price > 300000 ? '金领':'白领' }"></property>
--正则表达式:<property name="check" value="#{admin.usernamematches '[a-zA-Z._%+-]'}"></property>
3.4 调用静态方法
--调用静态方法或者静态属性:通过T()来调用一个类的静态方法或者属性,示例如下:
<property name="length" value="#{T(java.lang.Math).PI*100}"></property>
4 示例代码
4.1 建一个Car类
public class Car {
private String brand;
private double price;
private double length;
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 getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", length=" + length + "]";
}
}
4.2建一个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 + "]"; } }
4.3 建一个Person类
4.4 建一个名为bean-SpEl的xml文档,配置如下public class Person { private String name; private Address address; private Car car; private String info; public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [name=" + name + ", address=" + address + ", car=" + car + ", info=" + info + "]"; } }
4.5 建一个测试类Main<?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.lg.spring.SpEL.Address"> <property name="city" value="#{'武汉'}"></property> 使用SpEL的字面量赋值方法 <property name="street" value="洪山区"></property> </bean> <bean id="car" class="com.lg.spring.SpEL.Car"> <property name="brand" value="bmw"></property> <property name="price" value="300000"></property> <property name="length" value="#{T(java.lang.Math).PI*100}"></property> 使用SpEL引用类的静态属性 </bean> <bean id="person" class="com.lg.spring.SpEL.Person"> <property name="name" value="lgzy"></property> <property name="address" value="#{address}"></property> 使用SpEL引用其他bean对象 <property name="car" ref="car"></property> <property name="info" value="#{car.price>280000?'金领':'白领'}"></property> 使用SpEL的条件判断语句 </bean> </beans>
package com.lg.spring.SpEL; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args){ ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-SpEL.xml"); Person person = (Person)ctx.getBean("person"); System.out.println(person); } }
4.6 输出结果