你的位置:首页 > Java教程

[Java教程]spring学习笔记——SpEL


1、SpEL非常实用,基本特性:

a.使用Bean的ID来引用Bean

b.调用方法和访问对象的属性

c.对值进行算术、关系和逻辑运算

d.正则表达式匹配

e.集合操作

2、案例

<property name = "count" value = "#{5}" />

<property name = "message" value = "the value is #{5}" />

<property name = "capacity" value = "#{1e4}" />

<property name = "name" vaue = "#{'Chuck'}" />//注意单双引号的使用

<property name = "name" value = '#{"Chuck"}' />

<property name = "enabled" value = "#{false}" />

<property name = "instrument" value = "#{saxophone}" />

<==>

<property name = "instrument" ref= "saxophone" />

<property name = "song" value = "#{kenny.song}" />

<property name = "song" value = "#{kenny.getSong()}" />

<property name = "song" value = "#{kenny.getSong().toUpperCase()}" />

<property name = "song" value="#{kenny.getSong()?.toUpperCase()}"//防止空指针异常:?.

<property name = "pi" value = "#{T(java.lang.Math).PI}" /> //T运算符会调用类作用域的方法和常量

<property name = "randomNumber" value = "#{T(java.lang.Math).random()}" />

<property name = "adjust" value = "#{counter.total + 42}" /> //加法运算

<property name = "area" value = "#{T(java.lang.Math).PI * circle.radius ^ 2}" /> //乘方运算

<property name = "fullName" value = "#{performer.firstName + ' ' + performer.lastName}" />字符串连接

<property name = "equal" value = "#{counter.total == 100}" />

<property name = "hasCa" value = "#{counter.total le 10000}" /> //== eq,< lt,<= le,> gt,>= ge

<property name = "aaa" value = "#{shape.kind == 'circle' and shapeperimter gt 1000}" /> //and,or,not或!

<property name = "validEmail" value = "#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}" /> //匹配正则表达式

<property name = "choseCity" value = "#{cities[2]}" />//list集合

<property name = "choeCity" value = "#{cities[T(java.lang.Math).random()*cities.size()]}" />//list集合

<property name = "choseCity" value = "#{cities['Dallas']}" /> //Map集合

<util:properties id = "settings" location = "classpath:settings.properties" />

<property name = "accessToken" value = "#{settings['twitter.accessToken']}" />

<property name = "homePath" value = "#{systemEnvironment['HOME']}" />//systemEnvironment包含了应用程序所在机器上的所有环境变量

<property name = "homePath" value = "#{systemProperties['application.home']}" />//systemProperties包含了Java应用程序启动时所设置的所有属性

<property name = "bigCities" value = "#{cities.?[population gt 1000]}" />//.?[]查询运算符,创建一个新的集合;.^[]和.$[]从集合中查询出第一个匹配项和最后一个匹配项

<property name = "cityNames" value = "#{cities.![name]}" />//.![]投影运算符:从集合的每一个成员中选择特定的属性放入一个新的集合中

<property name = "cityNames" value = "#{cities.![name+' , '+state]}" />

<property name = "cityNames" value = "#{cities.?[population gt 10000].![name + ','+state]}" />