Spring 表达式语言 SpEL
1、Spring 表达式语言 (简称SpEL):是一个支持运行时查询和操作对象图的强大表达式语言
2、语法类似于EL表达式 #{..}作为定界符,所有在大括号中的字符被认为是SpEL
3、SpEL 为bean 的属性进行动态赋值提供了便利
4、通过SpEL可以实现以下功能:
①:–通过bean 的id 对bean 进行引用
②:–调用方法以及引用对象中的属性
③:–计算表达式的值
Person
application-spel.xml 配置文件
1、Spring 表达式语言 (简称SpEL):是一个支持运行时查询和操作对象图的强大表达式语言
2、语法类似于EL表达式 #{..}作为定界符,所有在大括号中的字符被认为是SpEL
3、SpEL 为bean 的属性进行动态赋值提供了便利
4、通过SpEL可以实现以下功能:
①:–通过bean 的id 对bean 进行引用
②:–调用方法以及引用对象中的属性
③:–计算表达式的值
④:–正则表达式的匹配
用于测试的两个类
Car
public class Car {
private String name;
private double pace;
private double perimeter;
private Boolean isable;
public Car() {
super();
}
public Car(String name, double pace, double perimeter, Boolean isable) {
super();
this.name = name;
this.pace = pace;
this.perimeter = perimeter;
this.isable = isable;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPace() {
return pace;
}
public void setPace(double pace) {
this.pace = pace;
}
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
public Boolean getIsable() {
return isable;
}
public void setIsable(Boolean isable) {
this.isable = isable;
}
@Override
public String toString() {
return "Car [name=" + name + ", pace=" + pace + ", perimeter=" + perimeter + ", isable=" + isable + "]";
}
}
Person
package com.dxf.spel;
import java.util.Date;
public class Person {
private String name;
private String nick;
private Date brithday;
private Car car;
private float sal;
private boolean isemail;
public Person() {
super();
}
public Person(String name, String nick, Date brithday, Car car, float sal, boolean isemail) {
super();
this.name = name;
this.nick = nick;
this.brithday = brithday;
this.car = car;
this.sal = sal;
this.isemail = isemail;
}
public boolean isIsmatch() {
return isemail;
}
public void setIsmatch(boolean isemail) {
this.isemail = isemail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
@Override
public String toString() {
return "Person [name=" + name + ", nick=" + nick + ", brithday=" + brithday + ", car=" + car + ", sal=" + sal
+ ", isemail=" + isemail + "]";
}
}
application-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">
<!-- 注意: 导入的外部文件使用 :$ el 使用 #
以下没有测试
比较运算符:<, >, ==, <=, >=, lt, gt, eq, le, ge
逻辑运算符号:and, or, not, | -->
<bean id="car" class="com.dxf.spel.Car">
<!-- 也可以赋值字面值 字符串 -->
<property name="name" value="#{'BMW'}"></property>
<!-- 简单的数学表达式 -->
<property name="pace" value="#{2*120}"></property>
<!-- 调用静态方法 和属性 T(..) -->
<property name="perimeter" value="#{T(java.lang.Math).PI*4.5^2}"></property>
<!-- if else 变体 -->
<property name="isable" value="#{1>2? false:true}"></property>
</bean>
<bean id="" class="com.dxf.spel.Person">
<!-- 调用其他bean 的属性、还支持字符串拼接,使用 + -->
<property name="name" value="#{car.name+' Tom'}"></property>
<!-- 调用其他bean的方法 及字符拼接 -->
<property name="nick" value="#{car.getName()+'Tom_nick'}"></property>
<!-- 调用静态方法、支持练市操作 -->
<property name="brithday"
value="#{T(java.util.Calendar).getInstance().getTime() }"></property>
<!-- 通过bean 的id 对bean 进行引用 -->
<property name="car" value="#{car}"></property>
<!-- 支持 + - * / % ,java.lang.Math.rint(double d)返回最接近参数并等于某一整数的 double
值 -->
<property name="sal"
value="#{((T(java.lang.Math).rint(car.perimeter)+200.0)/100)%1*2-1}"></property>
<!-- [0-9A-Za-z]{1,10}@(qq|126|136).com 匹配 以1-10位字母数字开头 @ qq或者126或者136
以.com结尾的字符串 -->
<property name="ismatch"
value="#{'1122280166@qq.com' matches '^[0-9A-Za-z]{1,10}@(qq|126|136)(.com)$'}">
</property>
</bean>
</beans>
package com.dxf.spel;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
ClassPathXmlApplicationContext cac;
@Before
public void init() {
cac = new ClassPathXmlApplicationContext("application-spel.xml");
}
@Test
public void testCar() {
Car car = cac.getBean(Car.class);
System.out.println(car);
}
@Test
public void testPerson() {
Person person = cac.getBean(Person.class);
System.out.println(person);
}
}