Spring3新特性之表达式语言SpEL

概述
       Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,类似于Struts2x中使用的OGNL表达式语言,能在运行时构建复杂表达式、存取对象图属性、对象方法调用 等等,并且能与Spring功能完美整合,如能用来配置Bean定义。
       表达式语言给静态Java语言增加了动态功能。
       SpEL是单独模块,只依赖于core模块,不依赖于其他模块,可以单独使用。
       表达式语言一般是用最简单的形式完成最主要的工作,减少我们的工作量。

SpEL支持如下表达式:
一、基本表达式:字面量表达式、关系,逻辑与算数运算表达式、字符串连接及截取表达式、三目运算及Elivis表达式、正则表达式、括号优先级表达式;
二、类相关表达式:类类型表达式、类实例化、instanceof表达式、变量定义及引用、赋值表达式、自定义函数、对象属性存取及安全导航表达式、对象方法调用、Bean引用;
三、集合相关表达式:内联List、内联数组、集合,字典访问、列表,字典,数组修改、集合投影、集合选择;不支持多维内联数组初始化;不支持内联字典定义;
四、其他表达式:模板表达式。
注:SpEL表达式中的关键字是不区分大小写的。
SpEL必需的Jar包:org.spring.expression.jar
关于SpEL的原理和接口,这篇博客有详细说明:http://jinnianshilongnian.iteye.com/blog/1416581

SpEL语法
基本表达式
一、字面量表达式
SpEL支持的字面量包括:字符串、数字类型(int、long、float、double)、布尔类型、null类型:
●字符串
String str1 = parser.parseExpression("'Hello World!'").getValue(String.class);
String str2 = parser.parseExpression("\"Hello World!\"").getValue(String.class);
●数字类型
int int1 = parser.parseExpression("1").getValue(Integer.class);
long long1 = parser.parseExpression("-1L").getValue(long.class);
float float1 = parser.parseExpression("1.1").getValue(Float.class);
double double1 = parser.parseExpression("1.1E+2").getValue(double.class);
int hex1 = parser.parseExpression("0xa").getValue(Integer.class);
long hex2 = parser.parseExpression("0xaL").getValue(long.class);
●布尔类型
boolean true1 = parser.parseExpression("true").getValue(boolean.class);
boolean false1 = parser.parseExpression("false").getValue(boolean.class);
●null类型
Object null1 = parser.parseExpression("null").getValue(Object.class);

二、算数运算表达式:SpEL支持加(+)、减(-)、乘(*)、除(/)、求余(%)、幂(^)运算:
●加减乘除
int result1 = parser.parseExpression("1+2-3*4/2").getValue(Integer.class);//-3
●求余
int result2 = parser.parseExpression("4%3").getValue(Integer.class);//1
●幂运算
int result3 = parser.parseExpression("2^3").getValue(Integer.class);//8
SpEL还提供求余(MOD)和除(DIV)而外两个运算符,与“%”和“/”等价,不区分大小写。

三、关系表达式:等于(==)、不等于(!=)、大于(>)、大于等于(>=)、小于(<)、小于等于(<=),区间(between)运算
●parser.parseExpression("1>2").getValue(boolean.class)将返回 false;●parser.parseExpression("1 between {1, 2}").getValue(boolean.class)将返回true
between运算符右边操作数必须是列表类型,且只能包含2个元素。第一个元素为开始,第二个元素为结束,区间运算是包含边界值的,即 xxx>=list.get(0) && xxx<=list.get(1)。
SpEL同样提供了等价的“EQ” 、“NE”、 “GT”、“GE”、 “LT” 、“LE”来表示等于、不等于、大于、大于等于、小于、小于等于,不区分大小写。

四、逻辑表达式:且(and)、或(or)、非(!或NOT)
●parser.parseExpression("2>1 and (!true or !false)").getValue(boolean.class)返回true
●parser.parseExpression("2>1 and (NOT true or NOT false)").getValue(boolean.class)返回true
注:逻辑运算符不支持 Java中的 && 和 ||

五、字符串连接及截取表达式:使用“+”进行字符串连接,使用“'String'[0] [index]”来截取一个字符,目前只支持截取一个,如“'Hello ' + 'World!'”得到“Hello World!”;而“'Hello World!'[0]”将返回“H”。

六、三目运算及Elivis运算表达式
三目运算符 “表达式1?表达式2:表达式3”用于构造三目运算表达式,如“2>1?true:false”将返回true;
Elivis运算符“表达式1?:表达式2”从Groovy语言引入用于简化三目运算符的,当表达式1为非null时则返回表达式1,当表达式1 为null时则返回表达式2,简化了三目运算符方式“表达式1? 表达式1:表达式2”,如“null?:false”将返回false,而“true?:false”将返回true;

七、正则表达式:使用“str matches regex,如“'123' matches '\\d{3}'”将返回true;

八、括号优先级表达式:使用“(表达式)”构造,括号里的具有高优先级。

类相关表达式
参考博客:http://jinnianshilongnian.iteye.com/blog/1418309
集合相关表达式
参考博客:http://jinnianshilongnian.iteye.com/blog/1418309

在配置文件中灵活运用相关表达式:


SpEL支持在Bean定义时注入,默认使用“#{SpEL表达式}”表示,其中“#root”根对象默认可以认为是 ApplicationContext,只有ApplicationContext实现默认支持SpEL,获取根对象属性其实是获取容器中的Bean,通 过Bean中的set方法可以得到根对象中的属性值。
可以参考这个里面的例子:http://jinnianshilongnian.iteye.com/blog/1418311

Xml代码   收藏代码
  1. <bean id="world" class="java.lang.String">    
  2.        <constructor-arg index="0" value="#{'Hello Spring!!!'[0]}"/>   
  3.        <property name="pros">  
  4.         <props value-type="java.lang.Long">  
  5.             <prop key="1">First</prop>  
  6.             <prop key="2">#{'Garent'}</prop>  
  7.             <prop key="3">Tomcat</prop>  
  8.         </props>  
  9.        </property>   
  10. </bean>  
  11. <!-- 构造了一个String类型的属性 其他Bean直接引用aaa这个Bean就可以-->  
  12. <bean id="aaa" class="java.lang.String">    
  13.        <constructor-arg value="%{' World!'}"/>    
  14. </bean>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值