一、

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介绍的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value

    一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...

  2. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

    一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...

  3. 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 ...

  4. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary

    一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...

  5. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles

    一. Spring honors two separate properties when determining which profiles are active:spring.profiles. ...

  6. SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile

    一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...

  7. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值

    1.When injecting properties and constructor arguments on beans that are created via component-scanni ...

  8. 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 ...

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

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

随机推荐

  1. ASP.NET MVC中的统一化自定义异常处理

    当ASP.NET MVC程序出现了异常,怎么处理更加规范? 1. 最简单的方法是设置<customErrors/>节点 <customErrors>节点用于定义一些自定义错误信 ...

  2. asp.net 备份和恢复数据库

    观看了Insus的视频写下来的,代码可能有点冗长,如有好的想法的,可以多交流. 前台: <form id="form1" runat="server"&g ...

  3. 利用绝对定位与margin实现元素居中

    例: 要让一个width:100px ; height: 100px;的div,相对body居中. div{ width:100px; height:100px; border: 1px solid ...

  4. 306573704 Char型和string型字符串比较整理(转)

    1.赋值 char赋值: char ch1[] = "give me"; char ch2[] = "a cup"; strcpy(ch1,ch2); cout ...

  5. C++学习之路,漫长而遥远

    一.C/C++语言 如果你的基础很差, 建议不要一开始就学C++语言,从C开始学起,对程序有个初步的认识,循序渐进.C语言的书嘛,先买一本 300 页以内的,把书中的每一个例子都通过键盘敲打进去到 V ...

  6. 百度地图LBS应用开发代码

    最近因为工作需要,领导要我将51地图API开发的一个应用迁移到百度地图,或者说用百度地图API进行重写,实现同样的功能.我先是把现有的这个51地图的应用了解了一下,然后就试着用百度地图做一些demo, ...

  7. C# 链表操作

    关于链表操作,在C#当中微软已经提供了一个LinkedList<T>的数据结构,通过这个类提供的一系列方法就能够实现链表操作. 这里我提供一段代码,这是在论坛里面有人提问时给出的代码,它实 ...

  8. [C#]将千分位字符串转换成数字

    关键代码: /// <summary> /// 将千分位字符串转换成数字 /// 说明:将诸如"–111,222,333的千分位"转换成-111222333数字 /// ...

  9. js 框架都有哪几种(转载)

    目前来看,js框架以及一些开发包和库类有如下几个,Dojo .Scriptaculous .Prototype .yui-ext .Jquery .Mochikit.mootools .moo.fxD ...

  10. stop() 是用于停止动画 :animated 用于判断动画是否在进行中

    stop() 是用于停止动画 if($("element").is(":animated"))  用于判断动画是否在进行中