ITeye博客

Spring3新特性之表达式语言SpEL

一日一博 · 2012-08-30 16:01 · Spring · 编程语言 · Spring · el表达式 · SPEL · Markdown

[size=medium][b]概述[/b] Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,类似于Struts2x中使用的OGNL表达式语言,能在运行时构建复杂表达式、存取对象图属性、对象方法调用等等,并且能与Spring功能完美整合,如能用来配置Bean定义。 表达式语言给静态Java语言增加了动态功能。 SpEL是单独模块,只依赖于core模块,不依赖于其他模块,可以单独使用。 表达式语言一般是用最简单的形式完成最主要的工作,减少我们的工作量。 [b]SpEL支持如下表达式:[/b] 一、[color=blue]基本表达式[/color]:字面量表达式、关系,逻辑与算数运算表达式、字符串连接及截取表达式、三目运算及Elivis表达式、正则表达式、括号优先级表达式; 二、[color=blue]类相关表达式[/color]:类类型表达式、类实例化、instanceof表达式、变量定义及引用、赋值表达式、自定义函数、对象属性存取及安全导航表达式、对象方法调用、Bean引用; 三、[color=blue]集合相关表达式[/color]:内联List、内联数组、集合,字典访问、列表,字典,数组修改、集合投影、集合选择;不支持多维内联数组初始化;不支持内联字典定义; 四、[color=blue]其他表达式[/color]:模板表达式。 [color=red][b]注:SpEL表达式中的关键字是不区分大小写的。[/b][/color] SpEL必需的Jar包:org.spring.expression.jar 关于SpEL的原理和接口,这篇博客有详细说明:[url]http://jinnianshilongnian.iteye.com/blog/1416581[/url] [b]SpEL语法[/b] [color=red][b]基本表达式[/b][/color] 一、[color=blue]字面量表达式[/color]: 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); 二、[color=blue]算数运算表达式[/color]: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)而外两个运算符,与“%”和“/”等价,不区分大小写。 三、[color=blue]关系表达式[/color]:等于(==)、不等于(!=)、大于(>)、大于等于(>=)、小于(<)、小于等于(<=),区间(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”来表示等于、不等于、大于、大于等于、小于、小于等于,不区分大小写。 四、[color=blue]逻辑表达式[/color]:且(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 [color=red]注:逻辑运算符不支持 Java中的 && 和 || [/color] 五、[color=blue]字符串连接及截取表达式[/color]:使用“+”进行字符串连接,使用“'String'[0] [index]”来截取一个字符,目前只支持截取一个,如“'Hello ' + 'World!'”得到“Hello World!”;而“'Hello World!'[0]”将返回“H”。 六、[color=blue]三目运算及Elivis运算表达式[/color]: 三目运算符 “表达式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; 七、[color=blue]正则表达式[/color]:使用“str matches regex,如“'123' matches '\\d{3}'”将返回true; 八、[color=blue]括号优先级表达式[/color]:使用“(表达式)”构造,括号里的具有高优先级。 [b][color=red]类相关表达式[/color][/b] 参考博客:[url]http://jinnianshilongnian.iteye.com/blog/1418309[/url] [b][color=red]集合相关表达式[/color][/b] 参考博客:[url]http://jinnianshilongnian.iteye.com/blog/1418309[/url] [align=center][b]在配置文件中灵活运用相关表达式:[/b][/align] SpEL支持在Bean定义时注入,默认使用“#{SpEL表达式}”表示,其中“#root”根对象默认可以认为是ApplicationContext,只有ApplicationContext实现默认支持SpEL,获取根对象属性其实是获取容器中的Bean,通过Bean中的set方法可以得到根对象中的属性值。 可以参考这个里面的例子:[url]http://jinnianshilongnian.iteye.com/blog/1418311[/url] [/size] [code="xml"] First #{'Garent'} Tomcat [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。