一、

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]}

转载于:https://www.cnblogs.com/shamgod/p/5238071.html

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍相关推荐

  1. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile...

    一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun;public class MagicBean {} 2. package co ...

  2. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-008-带参数的ADVICE

    一. 假设有情形如:cd里有很多轨,当播放音乐时,要统计每个音轨的播放次数,这些统计操作不应放在播放方法里,因为统计不是播放音乐的主要职责,这种情况适合应用AOP. 二. 1. package sou ...

  3. SPRING IN ACTION 第4版笔记-第四章Aspect-oriented Spring-001-什么是AOP

    一. Aspect就是把会在应用中的不同地方重复出现的非业务功能的模块化,比如日志.事务.安全.缓存 In software development, functions that span mult ...

  4. 计算机网络第七版笔记--第三章

    计算机网络第七版学习笔记 第三章数据链路层 3.1使用点对点信道的数据链路层 3.1.1数据链路和帧 1.链路(link)就是从一个结点到相邻结点的一段物理线路(有线或无线),而中间没有任何其他的交换 ...

  5. SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例

    spring的bean默认是单例,加载容器是会被化,spring会拦截其他再次请求bean的操作,返回spring已经创建好的bean. It appears that the CompactDisc ...

  6. SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法

    一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPla ...

  7. SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean...

    1. 1 package soundsystem; 2 import org.springframework.context.annotation.ComponentScan; 3 import or ...

  8. (第二版)零基础入门Python小甲鱼-笔记-第三章-p5

    (第二版)零基础入门Python小甲鱼-笔记-第三章-p5 变量和字符串(下) 上节课讲了有些字符比如换行符.TAB制表符还有单引号.双引号等等...可以通过转义字符来实现,今天来谈谈原始字符串 1. ...

  9. 机器人导论(第四版)学习笔记——第三章

    机器人导论(第四版)学习笔记--第三章 3 操作臂运动学 3.1 引言 3.2 连杆的描述 3.3 连杆连接的描述 3.4 连杆坐标系的定义 3.5 操作臂运动学 3.6 驱动空间.关节空间和笛卡尔空 ...

最新文章

  1. 继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错? springboot 两种方式稳定解决跨域问题
  2. getbytes方法_关于C#中识别和查找中英文字符串的解决方法和字符串处理方式的笔记...
  3. UI Component in CRM WebUI and Hybris
  4. 【python】利用python的tkinter-canvas函数绘制哆啦A梦过程详解(附源码)
  5. 使用java理解程序逻辑试卷_《使用Java理解程序逻辑》试题分析
  6. 数的计数【Noip2001】
  7. Java8 Lambda表达式教程
  8. 传统项目管理 VS 敏捷项目管理
  9. Jsonp解决跨域问题原理
  10. Ftp站点访问及FileZilla客户端使用
  11. Elasticsearch7.17 二:mapping映射和高级语法查询DSL
  12. c++通讯录管理系统(函数,指针,结构体)
  13. 上半年亏损之下,卫龙第三次冲刺港股IPO
  14. 【PAT甲级A1003 】Emergency (25分)(c++)
  15. 计算机自考考研学校有哪些专业,自考生考研9大热门专业,你知道是哪些吗?...
  16. matlab加速度转化为位移,matlab数值积分实现加速度、速度、位移的转换(时域频域积分)...
  17. Python爬虫抓取b站排行榜
  18. 一键批量导出好友联系方式
  19. 有孚网络副总裁吕鑫:合纵连横,云领未来—如何打造低成本混合云架构
  20. matlab实现ofdm调制解调,基于ofdm的调制解调原理,ofdm调制解调fpga代码 - 全文

热门文章

  1. Mr.J---重拾Ajax(一)--XMLHttpRequest
  2. oracle 11g12c(pdbcdb)系统巡检
  3. win 10 VMware与Hyper-v共存
  4. ThreadLocal剧集(一)
  5. 2016年度最受欢迎中国开源软件评选
  6. SPI、I2C、UART三种串行总线协议的区别和SPI接口介绍(转)
  7. 4.UiCollection API 详细介绍
  8. QT_StepByStep(2)--滑动杆的实现
  9. jquery ajax返回html乱码解决
  10. SQL 必知必会·笔记7汇总数据——使用聚合函数