SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍
一、
1.SpEL expressions are framed with #{ ... }
2.SpEl的作用
Sp EL has a lot of tricks up its sleeves, including the following:
The ability to reference beans by their ID s
Invoking methods and accessing properties on objects
Mathematical, relational, and logical operations on values
Regular expression matching
Collection manipulation
As you’ll see later in this book, S p EL can also be used for purposes other than depen-
dency injection. Spring Security, for example, supports defining security constraints
using S p EL expressions. And if you’re using Thymeleaf templates as the views in your
Spring MVC application, those templates can use S p EL expressions to reference
model data.
3.基础例子
(1)解析原始类型数据 #{1}
(2)解析java类的静态方法#{T(System).currentTimeMillis()},"T()"会把被包括的内容解析为java类
(3)解析bean:#{sgtPeppers.artist},取出id为“sgtPeppers”的artist属性
(4)解析properties:#{systemProperties['disc.title']}
二、进一步了解SpEl
1.EXPRESSING LITERAL VALUES
(1)浮点型:#{3.14159}
(2)科学计数法:#{9.87E4} PS:=98,700
(3)字符串:#{'Hello'}
(4)布尔型:#{false}
2.REFERENCING BEANS , PROPERTIES , AND METHODS
(1)引用bean:#{sgtPeppers} (in this case, a bean whose ID is sgtPeppers )
(2)引用bean的属性:#{sgtPeppers.artist} 引用id为"sgtPeppers"的bean的artist属性
(3)调用bean的方法:#{artistSelector.selectArtist()}
(4)链式调用:#{artistSelector.selectArtist().toUpperCase()}
(5)防止空指针:#{artistSelector.selectArtist()?.toUpperCase()}
3.WORKING WITH TYPES IN EXPRESSIONS
The key to working with class-scoped methods and constants in S p EL is to use the T()
operator. For example, to express Java’s Math class in S p EL , you need to use the T()
operator like this:
T(java.lang.Math)
The result of the T() operator, as shown here, is a Class object that represents java.lang.Math .
(1)T(java.lang.Math).PI
(2)T(java.lang.Math).random() random value between 0 and 1:

(1)计算一个代表圆的bean的周长:#{2 * T(java.lang.Math).PI * circle.radius}
(2)计算面积:#{T(java.lang.Math).PI * circle.radius ^ 2}
(3)用"+"连接字符串:#{disc.title + ' by ' + disc.artist}
(4)符号逻辑和文本逻辑操作符是等效的:#{counter.total == 100} 或者 #{counter.total eq 100}
(5)三目运算符:#{scoreboard.score > 1000 ? "Winner!" : "Loser"}
设置默认值:#{disc.title ?: 'Rattle and Hum'}
4.EVALUATING REGULAR EXPRESSIONS
(1)验证邮箱(不全) :#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}
5.EVALUATING COLLECTIONS
(1)#{jukebox.songs[4].title}
(2)随机播放歌曲:#{jukebox.songs[T(java.lang.Math).random() *jukebox.songs.size()].title}
(3)"[]"也可用来从字符串中截取字符:#{'This is a test'[3]} ,返回's'
(4)筛选:#{jukebox.songs.?[artist eq 'Aerosmith']},会返回所有Aerosmith的歌曲的新集合
(5)SpEL also offers two other selection operations: .^[] for selecting the first matching entry and .$[] for selecting the last matching entry.
返回第一个符合条件的条目:#{jukebox.songs.^[artist eq 'Aerosmith']}
(6)投影:SpEL offers a projection operator ( .![] ) to project properties from the elements in the collection onto a new collection.
抽取所有title:#{jukebox.songs.![title]}
组合操作,获取Aerosmith的所有歌名:#{jukebox.songs.?[artist eq 'Aerosmith'].![title]}
SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍的更多相关文章
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value
一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)
一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode
一. Spring的bean默认是单例的 But sometimes you may find yourself working with a mutable class that does main ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary
一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles
一. Spring honors two separate properties when determining which profiles are active:spring.profiles. ...
- SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile
一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值
1.When injecting properties and constructor arguments on beans that are created via component-scanni ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解
一. The @Qualifier annotation is the main way to work with qualifiers. It can beapplied alongside @Au ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile
一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...
随机推荐
- openvswitch安装和使用 --修订通用教程的一些错误
1.下载openvswitch源文件,注意版本要适合操作系统内核. 推荐openvswitch2.0及其以上版本. 2.开始安装openvswitch cd openvswitch sudo ./bo ...
- HW--漂亮度
描述 给出一个名字,该名字有26个字符串组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和.每个字母都有一个“漂亮度”,范围在1到26之间.没有任何两个字母拥有相同的“漂亮度”.字母忽略大小 ...
- 批量杀进程——xargs用途
ps -ef|grep ora|grep “LOCAL=YES”|awk ‘{print $2}’|xargs –I {} kill -9 {} ps -ef|grep ora|grep “LOCAL ...
- 395. Longest Substring with At Least K Repeating Characters
395. Longest Substring with At Least K Repeating Characters 我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为 ...
- C++对象模型与内存位对齐的简单分析(GNU GCC&VS2015编译器)
以Fruit和Apple为例进行分析: Fruit和Apple的定义如下: 通过在两种编译环境下的测试(GNU GCC & VS2015),可以发现这两种编译器的对象模型是一样的,如下图所示: ...
- OpenJudge 2753 菲波那契数列
1.链接地址: http://bailian.openjudge.cn/practice/2753 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 菲波那契数列是指这样的数列 ...
- Openstack安装
作者:陈沙克 Openstack发展很猛,很多朋友都很认同,2013年,会很好的解决OpenStack部署的问题,让安装,配置变得更加简单易用. 很多公司都投入人力去做这个,新浪也计划做一个Opens ...
- Photoshop快捷键
ctrl+del :铺后景色alt+del:铺前景色ctrl+d:取消选框ctrl+t:拉伸(挡住文字)TAB:显示(隐藏)工具栏ctrl+alt+i:反选ctrl+r:辅佐线ctrl+j:复制并添加 ...
- IE兼容性问题
1.H5标签兼容.解决:js:document.createElement("footer");css:display: block;或者直接使用 html5shiv.js ...
- 尚学堂JavaEE项目备选
偶然得知:记下待练 微博 软件人才网 论坛 博客系统 京东网上商城 赶集网 拉手网 优酷视频 百度知道(问答) 生产管理系统 房屋租赁网 金融股票