Spring spel表达式使用方法示例
作者:yangnianbing110 时间:2023-08-25 00:43:32
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进行引用
<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]}"/>
来源:http://blog.csdn.net/yangnianbing110/article/details/7614521
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
聊聊Springboot2.x的session和cookie有效期
![](https://img.aspxhome.com/file/2023/9/113209_0s.jpg)
Android 使用Picasso加载网络图片等比例缩放的实现方法
Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用详解
关于Java实现HttpServer模拟前端接口调用
![](https://img.aspxhome.com/file/2023/4/88054_0s.png)
将项目上传到Maven中央仓库(2023最新版)
![](https://img.aspxhome.com/file/2023/0/103970_0s.png)
C#如何动态创建lambda表达式
Java基础之重载(Overload)与重写(Override)详解
Android支付宝支付封装代码
android 实现类似微信缓存和即时更新好友头像示例
详解如何在C#中接受或拒绝Excel中的修订
![](https://img.aspxhome.com/file/2023/1/106401_0s.jpg)
Spring Boot Thymeleaf实现国际化的方法详解
![](https://img.aspxhome.com/file/2023/8/59008_0s.png)
Spring-Security实现登录接口流程
![](https://img.aspxhome.com/file/2023/4/70334_0s.png)
Java的Struts框架中<results>标签的使用方法
![](https://img.aspxhome.com/file/2023/2/132222_0s.jpg)
使用C#获取远程图片 Form用户名与密码Authorization认证的实现
springboot整合websocket最基础入门使用教程详解
![](https://img.aspxhome.com/file/2023/6/78036_0s.png)
Java基于TCP方式的二进制文件传输
![](https://img.aspxhome.com/file/2023/9/68219_0s.png)
Java 线程同步详解
Intellij IDEA如何去掉@Autowired 注入警告的方法
![](https://img.aspxhome.com/file/2023/5/96055_0s.png)