SpEL简介
Spring Expression Language,Spring表达式语言,简称SpEL。支持运行时查询并可以操作对象图。
和JSP页面上的EL表达式、Struts2中用到的OGNL表达式一样,SpEL根据JavaBean风格的getXxx()、setXxx()方法定义的属性访问对象图,完全符合我们熟悉的操作习惯。
基本语法
SpEL使用#{…}作为定界符,所有在大框号中的字符都将被认为是SpEL表达式。
使用字面量
●整数:
●小数:
●科学计数法:
●String类型的字面量可以使用单引号或者双引号作为字符串的定界符号
●Boolean:
引用其他bean
引用其他bean的属性值作为自己某个属性的值
调用非静态方法
调用静态方法
运算符
①算术运算符:+、-、*、/、%、^
②字符串连接:+
③比较运算符:、==、<=、>=、lt、gt、eq、le、ge
④逻辑运算符:and, or, not, |
⑤三目运算符:判断条件?判断结果为true时的取值:判断结果为false时的取值
⑥正则表达式:matches
创建java实体Bean对象
public class Person {
private int id;
private String name;
private String phone;
private double salary;
private Car car;
public class Car {
private String name;
private String carNo;
实验26:[SpEL测试I]在SpEL中使用字面量
使用格式:#{数值} #{“字符串” || ‘字符串’}
实验27:[SpEL测试II]在SpEL中引用其他bean
使用格式:#{bean的id}
实验28:[SpEL测试III]在SpEL中引用其他bean的某个属性值
使用格式: #{bean.属性名}
实验29:[SpEL测试IV]在SpEL中调用非静态方法
使用格式: #{bean.方法名(参数)}
实验30:[SpEL测试V]在SpEL中调用静态方法
使用格式:#{T(全名类).方法名(参数)}
实验31:[SpEL测试VI]在SpEL中使用运算符
使用格式:#{表达式}
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">
测试类:
public class SpringTest {
@Test
public void test1() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) applicationContext.getBean("p1");
System.out.println( person );
}
}