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._%+-][email protected][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]}

时间: 2024-10-27 05:15:26

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

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

一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's Lonely Hearts Club Band", "The Beatles"); } < bean id = "sgtPeppers" class = "soundsystem.BlankDisc" c: _title = &quo

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-tain some state and therefore isn’t safe for reuse. In that case, declaring the class as asingleton bean probably isn’t a good idea because that obje

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法

一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automatically create a proxy that delegates calls to either the proxied bean or to the introduction implementation, depending on whether the method called be

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY简介及用&lt;s:message&gt;和ReloadableResourceBundleMessageSource实现国际化

一. SPRING支持的GENERAL TAG LIBRARY 1. 二.用<s:message>和ReloadableResourceBundleMessageSource实现国际化 1.配置ReloadableResourceBundleMessageSource,它能it has the ability to reload message properties without recompiling or restarting the application. 1 package spi

SPRING IN ACTION 第4版笔记-第六章Rendering web views-001- Spring支持的View Resolver、InternalResourceViewResolver、JstlView

一.Spring支持的View Resolver 二.InternalResourceViewResolver Spring supports JSP views in two ways:? InternalResourceViewResolver ? Spring provides two JSP tag libraries, one for form-to-model binding and one providing general utility features. 1.在java中定义

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、&lt;jpa:repositories&gt;)

一.JpaRepository 1.要使Spring自动生成实现类的步骤 (1)配置文件xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xm

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-006- 使用thymeleaf(TemplateResolver、SpringTemplateEngine、ThymeleafViewResolver、th:include、th:object、th:field=&quot;*{firstName}&quot;)

一.在Spring中使用thymeleaf的步骤 1.配置 In order to use Thymeleaf with Spring, you’ll need to configure three beans that enable Thymeleaf-Spring integration:? A ThymeleafViewResolver that resolves Thymeleaf template views from logical view names? A SpringTempl

SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMapping、model

一.RequestMapping 1.可以写在方法上或类上,且值可以是数组 1 package spittr.web; 2 3 import static org.springframework.web.bind.annotation.RequestMethod.*; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.ui.Model; 7 import org.springfra

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-002设置JPA的EntityManagerFactory(&lt;persistence-unit&gt;、&lt;jee:jndi-lookup&gt;)

一.EntityManagerFactory的种类 1.The JPA specification defines two kinds of entity managers: ? Application-managed—Entity managers are created when an application directly requests one from an entity manager factory. With application-managed entity manage