SpEL: Spring Expression Language是Spring的一套表达式,主要应用在IOC容器进行对象属性的注入。
格式为:#{表达式}
The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.
Spring表达式语言(简称SpEL)是一种强大的表达式语言,支持在运行时查询和操作对象图。语言语法类似于Unified EL,但提供了额外的功能,最明显的是方法调用和基本字符串模板功能
简单来说,SpEL表达式可以实现调用对象的属性和方法(静态的也可以),还支持表达式的编写。
SpEL表达式的格式:#{表达式}
<?xml version="1.0" encoding="UTF-8"?>
<!--约束文件,切记先有约束后有本地变量 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="bd2" class="cn.code404.domain.BasicDate"></bean>
<!--1、SpEL表达式的数据类型 支持 -->
<bean id="bn1" class="cn.code404.domain.BasicNum">
<!--SpEL表达式 -->
<property name="num1" value="#{1}"></property>
<property name="num2" value="#{10*23.3}"></property>
<property name="num3" value="#{10/23.3}"></property>
<property name="num4" value="#{'abc'}"></property>
<property name="date" value="#{bd2}"></property>
</bean>
<!--2、SpEL表达式运算符的支持 -->
<bean id="bo1" class="cn.code404.domain.BasicOp">
<!--算术运算符 -->
<property name="num" value="#{2/3}"></property>
<!--字符串连接运算符 -->
<property name="msg" value="#{bd2.name+user1.id}"></property>
<!-- 比较运算符 -->
<property name="res" value="#{3 gt 4}"></property>
<!--三目运算符的支持 -->
<property name="sex" value="#{user1.id gt 3?'男人':'女孩'}"></property>
<!-- <property name="name" value="#{bd2.name!=null?bd2.name:'默认值'}"></property> -->
<!--简写的三目 -->
<property name="name" value="#{bd2.name?:'默认值'}"></property>
</bean>
<!--3、SpEL表达式的方法的调用 -->
<bean id="bm1" class="cn.code404.domain.BasicMethod">
<property name="msg" value="#{'abcdefg'}"></property>
<!--长度 -->
<property name="size" value="#{'abcdefg'.length()}"></property>
<!--转换为大写 -->
<property name="msgUpper" value="#{'abcdefg'.toUpperCase()}"></property>
<!--支持方法的链式调用 -->
<property name="index" value="#{'abcdefg'.toUpperCase().indexOf('E')}"></property>
<!--调用实例方法 -->
<property name="content" value="#{bo1.getTime()}"></property>
<!--调用静态属性 -->
<property name="pi" value="#{T(java.lang.Math).PI}"></property>
<!--调用静态方法 -->
<property name="isStudy" value="#{T(java.lang.Math).random()*2}"></property></property>
<!--静态方法的传值 -->
<property name="food" value="#{T(org.qf.domain.BasicOp).food(bo1.name)}"></property>
</bean>
<!--4、使用正则表达式 -->
<bean id="stu" class="cn.code404.domain.Student">
<property name="isMatch" value="#{'123a5678' matches '^[0-9]{8}$'}"></property>
</bean>
<!--5、对集合的查询操作 -->
<util:list id="emp_2">
<bean class="cn.code404.collection.Emp" p:name="小王" p:zw="CEO" p:age="30"></bean>
<bean class="cn.code404.collection.Emp" p:name="大张" p:zw="CEO" p:age="40"></bean>
<bean class="cn.code404.collection.Emp" p:name="小白" p:zw="CEO" p:age="50"></bean>
<bean class="cn.code404.collection.Emp" p:name="老高" p:zw="CEO" p:age="60"></bean>
</util:list>
<bean class="cn.code404.collection.ElModel" id="e1">
<!--1、返回符合条件的第一个元素 -->
<!-- <property name="emp" value="#{emp_2.^[age lt 50]}"></property> -->
<!--2、返回符合条件的最后一个元素 -->
<property name="emp" value="#{emp_2.$[age lt 50]}"></property>
<!--3、返回符合条件的多个元素(组成集合返回) -->
<property name="emps" value="#{emp_2.?[age gt 30 ]}"></property>
</bean>
</beans>
单元测试类:
public class SpELTest {
//SpEL表达式的数据类型的支持
@Test
public void test1(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext1.xml");
System.out.println(context.getBean("bn1"));
}
//SpEL表达式的运算符的支持
@Test
public void test3(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext1.xml");
System.out.println(context.getBean("bo1"));
}
//SpEL表达式对方法的支持
@Test
public void test4(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext1.xml");
BasicMethod bMethod=(BasicMethod) context.getBean("bm1");
System.out.println(bMethod.getSize());
System.out.println(bMethod.getMsgUpper());
System.out.println(bMethod.getIndex());
//调用实例方法
System.out.println(bMethod.getContent());
//调用静态属性
System.out.println(bMethod.getPi());
//调用静态方法
System.out.println(bMethod.getIsStudy());
System.out.println(bMethod.getFood());
}
//SpEL表达式对正则的支持
@Test
public void test5(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext1.xml");
Student student=(Student) context.getBean("stu");
System.out.println(student.getIsMatch());
}
//SpEL的集合支持
@Test
public void test7(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext1.xml");
ElModel model=(ElModel) context.getBean("e1");
System.out.println(model.getEmp().getName());
System.out.println(model.getEmps().size());
}
}
测试的实体类就不贴了。