Spring中Spel的使用
注:本文用到的jar请到Spring(1-1、基于xml装配Bean)中查找
Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。
语法类似于 EL:SpEL 使用#{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL
SpEL 为 bean 的属性进行动态赋值提供了便利。
通过 SpEL 可以实现:
通过 bean 的 id 对 bean 进行引用。
调用方法以及引用对象中的属性。
计算表达式的值。
正则表达式的匹配。
例如:
字面量的表示:
整数:<propertyname="count" value="#{5}"/>
小数:<propertyname="frequency" value="#{89.7}"/>
科学计数法:<propertyname="capacity" value="#{1e4}"/>
String可以使用单引号或者双引号作为字符串的定界符号:
<propertyname=“name” value="#{'Chuck'}"/> 或
<propertyname='name' value='#{"Chuck"}'/>
Boolean:<property name="enabled" value="#{false}"/>
javaBean:
public class Address {
private String city;
private String street;
public class Person {
private String name;
private Address address;
private Car car;
// 引用address bean的city属性
private String city;
// 根据car的price确定info:car的price>=300000:金额.否则为白领
private String info;
public class Car {
private String brand;
private double price;
// 周长
private double tyrePerimeter;
xml的配置:
<!-- 使用spel为属性配置一个字面值 -->
<bean id="address" class="org.mybatis.bean.spel.Address">
<property name="city" value="#{'BeiJing'}"></property>
<property name="street" value="wudaokou"></property>
</bean>
<bean id="car" class="org.mybatis.bean.spel.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="500000"></property>
<!-- 使用SPEL引用类的静态属性 -->
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property>
</bean>
<bean id="person" class="org.mybatis.bean.spel.Person">
<!-- 使用spel来引用其他的bean -->
<property name="car" value="#{car}"></property>
<!-- 使用spel来引用其他的bean 的属性 -->
<property name="city" value="#{address.city}"></property>
<!-- 在spel中使用运算符 -->
<property name="info" value="#{car.price>300000?'金领':'白领'}"></property>
<property name="name" value="Tom"></property>
</bean>
算数运算符:+, -, *, /, %, ^
<property name="adjustedAmount" value="#{counter.total+42}"></property>
<property name="adjustedAmount" value="#{counter.total-20}"></property>
<property name="cirumference" value="#{2 * T(java.lang.Math).PI*circle.radius}"></property>
<property name="average" value="#{counter.total%counter.count}"></property>
<property name="remainder" value="#{counter.total%counter.count}"></property>
<property name="area" value="#{T(java.lang.Math).PI*circle.radius^2}"></property>
加号还可以作为字符串连接:
<constructor-arg value="#{performer.firstName+''+performer.lastName}">
</constructor-arg>
逻辑运算符:and, or, not, |
<property name="largeCircle"
value="shape.kind=='circle' and shape.perimeter gt 10000"></property>
<property name="outOfStock" value="#{!product.available}"></property>
<property name="outOfStock" value="#{not product.available}"></property>
if-else变体:
<property name="info" value="#{car.price>300000?'金领':'白领'}"></property>
正则表达式:
<constructor-arg
value="#{admin.email matches'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9._%-]+\\.[a-zA-z]{2,4}'}">
</constructor-arg>
测试:
@Test
public void test1() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"beans-spel.xml");
Address address = (Address) ctx.getBean("address");
System.out.println(address);
Car Car = (Car) ctx.getBean("car");
System.out.println(Car);
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}