spring中的spel表达式语言
2012-05-29 23:15
411 查看
spring in action第三版读书笔记
spring3.0引入了spring expression language(spel)语言,通过spel我们可以实现
1.通过bean的id对bean进行引用
2.调用方法以及引用对象中的属性
3.计算表达式的值
4.正则表达式的匹配
5.集合的操作
spel最终的目标是得到表达式计算之后的值,这些表达式可能是列举的一些值,引用对象的某些属性,或者是类中的某些常量,复杂的spel表达式通常都是由一些简单的元素构成的
最简单的仅仅是得到一些给出元素的值例如:
<property name="count" value="the value is #{5}"/>。这种情况貌似很傻,根本就不需要用到spel,但是复杂的表达式都是由简单的构成的
对其他bean的引用
通过spel我们也可以对context中其他的bean进行引用
等同于
引用另外一个id为saxophone的bean作为instrument的值
对其他bean中某个属性的引用
对其他bean中某个方法的引用
<property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/>,表达式(?.)可以确保在selectSong()返回不为空的情况下调用toUpperCase()方法,如果返回空那么不继续调用后面的方法
对类进行引用
如果某个类是外部类,而不是spring中定义的bean,那么怎么进行引用呢?
使用表达式T(),例如:
spel计算表达式的值
spel表达式支持各种各样的运算符,我们可以可以运用这些运算符来计算表达式的值
使用spel从集合中筛选元素:
使用spring的util namespace中的元素<util:list>定义一个集合
<property name="chosenCity" value="#{cities[2]}"/>,
[]操作符也可以对Map进行筛选,假设citis是一个Map类型<property name="chosenCity" value="#{cities["keyName"]}"/>
[]对Properties类型进行操作
<util:properties id="settings"
location="classpath:settings.properties"/>使用<util:properties>标签读取一个properties文件
基于某个属性对集合中的元素进行过滤
<property name="bigCitis" value="#{cities.?[population gt 10000]}"/>选中人口大一10000的cities中的元素作为bigCitis的值,同操作符(.?[])类似, 操作符(.^[]选取满足要求的第一个元素, .$[]选取满足要求的最后一个)
选中已有集合中元素的某一个或几个属性作为新的集合
<property name="cityNames" value="#{cities.![name + ", " + state]}"/>
spring3.0引入了spring expression language(spel)语言,通过spel我们可以实现
1.通过bean的id对bean进行引用
2.调用方法以及引用对象中的属性
3.计算表达式的值
4.正则表达式的匹配
5.集合的操作
spel最终的目标是得到表达式计算之后的值,这些表达式可能是列举的一些值,引用对象的某些属性,或者是类中的某些常量,复杂的spel表达式通常都是由一些简单的元素构成的
最简单的仅仅是得到一些给出元素的值例如:
<property name="count" value="the value is #{5}"/>。这种情况貌似很傻,根本就不需要用到spel,但是复杂的表达式都是由简单的构成的
对其他bean的引用
通过spel我们也可以对context中其他的bean进行引用
<property name="instrument" value="#{saxophone}"/>
等同于
<property name="instrument" ref="saxophone"/>
引用另外一个id为saxophone的bean作为instrument的值
对其他bean中某个属性的引用
<bean id="carl" class="com.springinaction.Instrumentalist"> <property name="song" value="#{kenny.song}"/> </bean>取id为kenny的bean的song字段的作为song的value
对其他bean中某个方法的引用
<property name="song" value="#{songSelector.selectSong().toUpperCase()}"/>调用id为songSelector的bean的selectSong()方法,使用其返回值作为song的值,这也带来一个问如果selectSong()方法返回一个null,那么会抛出一个空指针异常
<property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/>,表达式(?.)可以确保在selectSong()返回不为空的情况下调用toUpperCase()方法,如果返回空那么不继续调用后面的方法
对类进行引用
如果某个类是外部类,而不是spring中定义的bean,那么怎么进行引用呢?
使用表达式T(),例如:
<property name="randomNumber" value="#{T(java.lang.Math).random()}"/>
spel计算表达式的值
spel表达式支持各种各样的运算符,我们可以可以运用这些运算符来计算表达式的值
使用spel从集合中筛选元素:
使用spring的util namespace中的元素<util:list>定义一个集合
<util:list id="cities"> <bean class="com.habuma.spel.cities.City" p:name="Chicago" p:state="IL" p:population="2853114"/> <bean class="com.habuma.spel.cities.City" p:name="Atlanta" p:state="GA" p:population="537958"/> <bean class="com.habuma.spel.cities.City" p:name="Dallas" p:state="TX" p:population="1279910"/> <bean class="com.habuma.spel.cities.City" p:name="Houston" p:state="TX" p:population="2242193"/> </util:list>使用spel对集合进行筛选
<property name="chosenCity" value="#{cities[2]}"/>,
[]操作符也可以对Map进行筛选,假设citis是一个Map类型<property name="chosenCity" value="#{cities["keyName"]}"/>
[]对Properties类型进行操作
<util:properties id="settings"
location="classpath:settings.properties"/>使用<util:properties>标签读取一个properties文件
<property name="accessToken" value="#{settings['twitter.accessToken']}"/>
基于某个属性对集合中的元素进行过滤
<property name="bigCitis" value="#{cities.?[population gt 10000]}"/>选中人口大一10000的cities中的元素作为bigCitis的值,同操作符(.?[])类似, 操作符(.^[]选取满足要求的第一个元素, .$[]选取满足要求的最后一个)
选中已有集合中元素的某一个或几个属性作为新的集合
<property name="cityNames" value="#{cities.![name + ", " + state]}"/>
相关文章推荐
- spring spel表达式语言
- Spring 表达式语言 (Spring Expression Language) SpEL
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
- Spring表达式语言 之 5.3 SpEL语法(拾肆)
- Spring-10 , Spring表达式语言(SpEL)
- Spring学习笔记--Spring表达式语言SpEL
- Spring学习记录(七)---表达式语言-SpEL
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
- spring3表达式语言(SpEL)
- Spring3表达式语言(SpEL)学习笔记
- 7 -- Spring的基本用法 -- 12... Spring 3.0 提供的表达式语言(SpEL)
- Spring表达式语言 之 5.3 SpEL语法
- Spring Expression Language(SpEL) spring表达式语言
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
- Spring表达式语言SpEL简单介绍
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3