http://sishuok.com/forum/blogPost/list/2463.html
http://docs.spring.io/spring/docs/3.0.x/reference/expressions.html
http://cnicwsh.iteye.com/blog/504937
Spring 3.0 RC1发布,一些新特性很吸引人,看了一下Reference,顺便翻译了SpEL这节,水平有限,还望指教。
Spring 3.0 Reference:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/index.html
排版过的见:http://blog.csdn.net/wsh622827/archive/2009/10/27/4733524.aspx
Part III 核心技术
6.Spring 表达式语言(SpEL)
- 6.1 简介
- 6.2 特性概览
- 6.3 使用Spring Expression接口进行表达式求值
- 6.4 bean定义的表达式支持
- 6.5 语言参考
- 6.6 示例类
6.1 简介
Spring表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言。其语法类似于统一EL,但提供了额外特性,显式方法调用和基本字符串模板函数。
同很多可用的Java 表达式语言相比,例如OGNL,MVEL和JBoss EL,SpEL的诞生是为了给Spring社区提供一个可以给Spring目录中所有产品提供单一良好支持的表达式语言。其语言特性由Spring目录中的项目需求驱动,包括基于eclipse的SpringSource套件中的代码补全工具需求。那就是说,SpEL是一个基于技术中立的API允许需要时与其他表达式语言集成。
SpEL作为Spring目录中表达式求值的基础,它并不是直接依赖于Spring而是可以被独立使用。为了能够自包含,本章中的许多示例把SpEL作为一个独立的表达式语言来使用。这就需要创建一些如解析器的引导基础组件类。大多数Spring用户只需要为求值编写表达式字符串而不需要关心这些基础组件。一个典型的使用例子是集成SpEL和创建基于XML或注解的bean定义,见6.4 bean定义的表达式支持一节。
本章涵盖了表达式语言的特性,API和语法。在很多地方,一个Inventor类和Invertor的Society类作为表达式求值的目标对象。这些类声明和操作它们使用的数据列在本章的末尾。
6.2 特性概览
表达式语言支持下列功能:
ž 字符表达式
ž 布尔和关系操作符
ž 正则表达式
ž 类表达式
ž 访问properties,arrays,lists,maps
ž 方法调用
ž 关系操作符
ž 赋值
ž 调用构造器
ž 三元操作符
ž 变量
ž 用户自定义函数
ž 集合投影
ž 集合选择
ž 模板表达式
6.3 使用Spring Expression接口进行表达式求值
这一节介绍SpEL接口和其表达式语言的简单实用。完整的语言参考参见语言参考一节。
下面的代码使用SpEL API求字符表达式‘Hello World’的值。
ExpressionParserparser= new SpelExpressionParser();
Expressionexp=parser.parseExpression("'HelloWorld'");
Stringmessage=(String)exp.getValue();
message变量的值是最简单的‘Hello World’。
最常使用的SpEL类和接口在包org.springframework.expression和其子包以及spel.support中。
ExpressionParser接口用来解析一个表达式字符串。在这个例子中,表达式串是一个被单引号包括标注的字符串。Expression接口用来求前面定义的表达式串的值。当调用parser.parseExpression和exp.getValue时分别可能抛出ParseException和EvaluationException异常。
SpEL支持一系列特性,例如方法调用,访问属性和调用构造器。
下面调用字符串文字的concat方法作为方法调用的一个例子。
ExpressionParserparser= new SpelExpressionParser();
Expressionexp=parser.parseExpression("'HelloWorld'.concat('!')");
Stringmessage=(String)exp.getValue();
message的值为‘Hello World!’。
下面的String属性Bytes可以被调用作为调用JavaBean属性的一个例子。
ExpressionParserparser= new SpelExpressionParser();
//invokes'getBytes()'
Expressionexp=parser.parseExpression("'HelloWorld'.bytes");
byte[]bytes=(byte[])exp.getValue();
SpEL使用标准的‘.’符号支持属性嵌套和属性设值,例如:prop1.prop2.prop3.
公共属性也可以被访问。
ExpressionParserparser= new SpelExpressionParser();
//invokes'getBytes().length'
Expressionexp=parser.parseExpression("'HelloWorld'.bytes.length");
int length=(Integer)exp.getValue();
使用字符串构造器而不是字符串文字。
ExpressionParserparser= new SpelExpressionParser();
Expressionexp=parser.parseExpression("newString('helloworld').toUpperCase()");
Stringmessage=exp.getValue(String.class);
记住使用泛型方法public <T> T getValue(Class<T> desiredResultType)。使用这个方法省去了需要时对表达式的值显式类型转换。如果该值不能被转换为T或者使用已注册的类型转换器转换则会抛出EvaluationException.
SpEL中更常见的用途是提供一个针对特定对象实例求值的表达式字符串。在下面的例子中,我们检索一个Inventor类的实例的name属性。
//Createandsetacalendar
GregorianCalendarc= new GregorianCalendar();
c.set(1856,7,9);
//Theconstructorargumentsarename,birthday,andnationality.
Inventortesla= new Inventor("NikolaTesla",c.getTime(), "Serbian");
ExpressionParserparser= new SpelExpressionParser();
Expressionexp=parser.parseExpression("name");
EvaluationContextcontext= new StandardEvaluationContext();
context.setRootObject(tesla);
Stringname=(String)exp.getValue(context);
在最后一行,该字符串变量'name'将被设置为“Nikola Tesla”。类StandardEvaluationContext是您可以指定对象的将被求值的“name”属性。你可以重复使用相同的表达式,在求值上下文内设定一个新的根对象。表达式是使用反射求值。
Note:在单独使用SpEL时,你需要创建一个解析器并提供一个求值上下文。但是更为广泛的应用仅仅提供一个SpEL表达式字符串作为配置文件的一部分。例如为Spring bean或Spring Web Flow定义。这种情况下,解析器,求值上下文,根对象和其他预定义变量都会被隐含创建。
作为最后一个例子,使用前面例子中的Inventor对象中使用布尔操作符。
Expressionexp=parser.parseExpression("name=='NikolaTesla'");
boolean result=exp.getValue(context,Boolean.class); //evaluatestotrue
EvaluationContext接口
EvaluationContext接口用来求一个解析属性,方法,域的表达式值以及帮助类型转换。其即插即用实现StandardEvaluationContext使用反射机制来操作对象。为获得好的性能缓存java.lang.reflect的Method,Field,和Constructor实例。
该StandardEvaluationContext是你用来通过方法setRootObject指定求值根对象或传递根对象到构造器的接口。你还可以指定将在表达式中使用的方法setVariable和registerFunction的变量和函数。变量和函数使用的描述见语言参考部分的变量和函数。StandardEvaluationContext还是你可以注册自定义ConstructorResolvers,MethodResolvers,和PropertyAccessors来扩展SpEL如何计算表达式。请参阅这些类的JavaDoc了解详情
类型转换
默认情况下,SpEL使用的转换服务可在Spring的核心(org.springframework.core.convert.ConversionService)找到。这种转换的服务由很多内建的为通常转换的转换器构成,但也是可扩展的,这样可以添加自定义类型之间的转换。此外它的主要支持是它是泛型敏感的。这意味着,当处理表达式中的泛型时,SpEL将尝试转换来维持对所遇到的任何对象类型的正确性。
这是什么在实践中意味着什么呢?比如赋值,用setValue()来设置List类型,实际是List<Boolean>。SpEL将会认识到,在List的元素需要在使用之前转换为布尔类型。一个简单的例子:
class Simple{
public List<Boolean>booleanList= new ArrayList<Boolean>();
}
Simplesimple= new Simple();
simple.booleanList.add(true);
StandardEvaluationContextsimpleContext= new StandardEvaluationContext(simple);
//falseispassedinhereasastring.SpELandtheconversionservicewill
//correctlyrecognizethatitneedstobeaBooleanandconvertit
parser.parseExpression("booleanList[0]").setValue(simpleContext, "false");
//bwillbefalse
Booleanb=simple.booleanList.get(0);
6.4 bean定义的表达式支持
SpEL可以和Bean定义中基于XML或注解的元数据配置一起使用。两种情况下的语法都是如#{ <expression string> }的形式。
基于XML的配置
下面一个属性或者构造参数值可以使用表达式设值。
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
<property name="randomNumber" value="#{T(java.lang.Math).random()*100.0}"/>
<!--otherproperties-->
</bean>
变量‘systemProperties’是预先定义的,所以你可以在表达式中如下使用,记住在这个上下文中你不必在预定义的变量前加#号。
<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
<property name="defaultLocale" value="#{systemProperties['user.region']}"/>
<!--otherproperties-->
</bean>
你可以通过名字引用其他bean的属性,如下:
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
<property name="randomNumber" value="#{T(java.lang.Math).random()*100.0}"/>
<!--otherproperties-->
</bean>
<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
<property name="initialShapeSeed" value="#{numberGuess.randomNumber}"/>
<!--otherproperties-->
</bean>
基于注解的配置
@Value注解可以在域,方法和方法/构造器参数中使用来指定一个默认值。
这是一个设置域变量默认值的例子。
publicstaticclass FieldValueTestBean
@Value("#{systemProperties['user.region']}")
private StringdefaultLocale;
publicvoid setDefaultLocale(StringdefaultLocale)
{
this.defaultLocale=defaultLocale;
}
public StringgetDefaultLocale()
{
returnthis.defaultLocale;
}
}
和下面在属性的setter方法中使用等价。
publicstaticclass PropertyValueTestBean
private StringdefaultLocale;
@Value("#{systemProperties['user.region']}")
publicvoid setDefaultLocale(StringdefaultLocale)
{
this.defaultLocale=defaultLocale;
}
public StringgetDefaultLocale()
{
returnthis.defaultLocale;
}
}
自动装配的方法和构造器同样可以使用@Value注解。
publicclass SimpleMovieLister{
private MovieFindermovieFinder;
private StringdefaultLocale;
@Autowired
publicvoid configure(MovieFindermovieFinder,
@Value("#{systemProperties['user.region']}"}StringdefaultLocale){
this.movieFinder=movieFinder;
this.defaultLocale=defaultLocale;
}
//...
}
publicclass MovieRecommender{
private StringdefaultLocale;
private CustomerPreferenceDaocustomerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDaocustomerPreferenceDao,
@Value("#{systemProperties['user.country']}"}StringdefaultLocale){
this.customerPreferenceDao=customerPreferenceDao;
this.defaultLocale=defaultLocale;
}
//...
}
6.5 语言参考
文字表达式
支持的文字表达的类型是字符串,日期,数值(整型,实型,和十六进制),布尔和空。字符串是由单引号分隔。使用反斜杠字符转移把一个单引号字符本身放在字符串中。以下清单显示文字的简单用法。通常它们不会使用这样单独使用,而是作为一个更复杂的表达式的一部分,例如,使用一个文字表达式作为逻辑比较运算符的一边。
ExpressionParserparser= new SpelExpressionParser();
//evalsto"HelloWorld"
StringhelloWorld=(String)parser.parseExpression("'HelloWorld'").getValue();
double avogadrosNumber=(Double)parser.parseExpression("6.0221415E+23").getValue();
//evalsto2147483647
int maxValue=(Integer)parser.parseExpression("0x7FFFFFFF").getValue();
boolean trueValue=(Boolean)parser.parseExpression("true").getValue();
ObjectnullValue=parser.parseExpression("null").getValue();
数字支持负号,指数符号的使用和小数点。默认情况下实数解析使用Double.parseDouble()。
Properties,Arrays,Lists,Maps,Indexers
用属性引用导航很简单,只需使用一个句点来表示一个嵌套的属性值。Inventor类实例,pupin和tesla,使用示例中使用的类一节中列出的数据填充。要“向下”导航并获得tesla的出生年份和pupin的出生的城市则使用下列表达式。
//evalsto1856
int year=(Integer)parser.parseExpression("Birthdate.Year+1900").getValue(context);
Stringcity=(String)parser.parseExpression("placeOfBirth.City").getValue(context);
属性名的首字母区分大小写,数组和列表的内容使用方括号符号得到。
ExpressionParserparser= new SpelExpressionParser();
//InventionsArray
StandardEvaluationContextteslaContext= new StandardEvaluationContext(tesla);
//evaluatesto"Inductionmotor"
Stringinvention=parser.parseExpression("inventions[3]").getValue(teslaContext,
String.class);
//MembersList
StandardEvaluationContextsocietyContext= new StandardEvaluationContext(ieee);
//evaluatesto"NikolaTesla"
Stringname=parser.parseExpression("Members[0].Name").getValue(societyContext,String.class);
//ListandArraynavigation
//evaluatesto"Wirelesscommunication"
Stringinvention=parser.parseExpression("Members[0].Inventions[6]").getValue(societyContext,
String.class);
Map的内容通过指定括号内的文字键值得到。在这种情况下,因为Officers Map的键是字符串,我们可以指定字符串。
//Officer'sDictionary
Inventorpupin=parser.parseExpression("Officers['president']").getValue(societyContext,
Inventor.class);
//evaluatesto"Idvor"
Stringcity=
parser.parseExpression("Officers['president'].PlaceOfBirth.City").getValue(societyContext,
String.class);
//settingvalues
parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").setValue(societyContext,
"Croatia");
方法
方法调用采用典型的Java编程语法。您还可以调用文字的方法。可变参数也支持。
//stringliteral,evaluatesto"bc"
Stringc=parser.parseExpression("'abc'.substring(2,3)").getValue(String.class);
//evaluatestotrue
boolean isMember=parser.parseExpression("isMember('MihajloPupin')").getValue(societyContext,
Boolean.class);
操作符
关系操作符
使用标准的操作符号支持关系操作符:等于,不等于,小于,小于等于,大于,大于等于。
//evaluatestotrue
boolean trueValue=parser.parseExpression("2==2").getValue(Boolean.class);
//evaluatestofalse
boolean falseValue=parser.parseExpression("2<-5.0").getValue(Boolean.class);
//evaluatestotrue
boolean trueValue=parser.parseExpression("'black'<'block'").getValue(Boolean.class);
除此之外,SpEL支持‘instanceof’和基于正则表达式的‘match’操作。
//evaluatestofalse
boolean falseValue=parser.parseExpression("'xyz'instanceofT(int)").getValue(Boolean.class);
//evaluatestotrue
boolean trueValue=
parser.parseExpression("'5.00'matches'^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
//evaluatestofalse
boolean falseValue=
parser.parseExpression("'5.0067'matches'^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
逻辑操作符
支持的逻辑操作符包括and,or和not,使用方法如下。
//--AND--
//evaluatestofalse
boolean falseValue=parser.parseExpression("trueandfalse").getValue(Boolean.class);
//evaluatestotrue
Stringexpression= "isMember('NikolaTesla')andisMember('MihajloPupin')";
boolean trueValue=parser.parseExpression(expression).getValue(societyContext,Boolean.class);
//--OR--
//evaluatestotrue
boolean trueValue=parser.parseExpression("trueorfalse").getValue(Boolean.class);
//evaluatestotrue
Stringexpression= "isMember('NikolaTesla')orisMember('AlbertEinstien')";
boolean trueValue=parser.parseExpression(expression).getValue(societyContext,Boolean.class);
//--NOT--
//evaluatestofalse
boolean falseValue=parser.parseExpression("!true").getValue(Boolean.class);
//--ANDandNOT--
Stringexpression= "isMember('NikolaTesla')and!isMember('MihajloPupin')";
boolean falseValue=parser.parseExpression(expression).getValue(societyContext,Boolean.class);
算术操作符
加法运算符可以用于数字,字符串和日期。减法可用于数字和日期。乘法和除法仅可以用于。其他支持的数学运算包括取模(%)和指数幂(^)。使用标准的运算符优先级。这些运算符示例如下。
//Addition
int two=parser.parseExpression("1+1").getValue(Integer.class); //2
StringtestString=
parser.parseExpression("'test'+''+'string'").getValue(String.class); //'teststring'
//Subtraction
int four=parser.parseExpression("1--3").getValue(Integer.class); //4
double d=parser.parseExpression("1000.00-1e4").getValue(Double.class); //-9000
//Multiplication
int six=parser.parseExpression("-2*-3").getValue(Integer.class); //6
double twentyFour=parser.parseExpression("2.0*3e0*4").getValue(Double.class); //24.0
//Division
int minusTwo=parser.parseExpression("6/-3").getValue(Integer.class); //-2
double one=parser.parseExpression("8.0/4e0/2").getValue(Double.class); //1.0
//Modulus
int three=parser.parseExpression("7%4").getValue(Integer.class); //3
int one=parser.parseExpression("8/5%2").getValue(Integer.class); //1
//Operatorprecedence
int minusTwentyOne=parser.parseExpression("1+2-3*8").getValue(Integer.class); //-21
赋值
属性设置是通过使用赋值运算符。这通常是在调用setValue中执行但也可以在调用getValue内。
Inventorinventor= new Inventor();
StandardEvaluationContextinventorContext= new StandardEvaluationContext(inventor);
parser.parseExpression("Name").setValue(inventorContext, "AlexanderSeovic2");
//alternatively
Stringaleks=parser.parseExpression("Name='AlexandarSeovic'").getValue(inventorContext,
String.class);
类型
特殊的‘T'操作符可以用来指定一个java.lang.Class的实例('类型')。静态方法调用也使用此操作符。该StandardEvaluationContext使用TypeLocator寻找类型,StandardTypeLocator(可更换)建立在java.lang包的基础上。这意味着T()引用在java.lang中的类型不须被完全限定,但所有其他类型的引用必须。
ClassdateClass=parser.parseExpression("T(java.util.Date)").getValue(Class.class);
ClassstringClass=parser.parseExpression("T(String)").getValue(Class.class);
boolean trueValue=
parser.parseExpression("T(java.math.RoundingMode).CEILING<T(java.math.RoundingMode).FLOOR")
.getValue(Boolean.class);
构造器
可以使用new运算符调用构造器。完全限定类名应被用于所有类型除了原始类型和字符串(如整型,浮点,等等,可以使用)。
Inventoreinstein=
p.parseExpression("neworg.spring.samples.spel.inventor.Inventor('AlbertEinstein',
'German')")
.getValue(Inventor.class);
//createnewinventorinstancewithinaddmethodofList
p.parseExpression("Members.add(neworg.spring.samples.spel.inventor.Inventor('AlbertEinstein',
'German'))")
.getValue(societyContext);
变量
变量可以在表达式中使用语法#’变量名’引用。变量设置使用StandardEvaluationContext的方法setVariable。
Inventortesla= new Inventor("NikolaTesla", "Serbian");
StandardEvaluationContextcontext= new StandardEvaluationContext(tesla);
context.setVariable("newName", "MikeTesla");
parser.parseExpression("Name=#newName").getValue(context);
System.out.println(tesla.getName()) //"MikeTesla"
#this变量
#this变量通常被定义和引用当前求值对象(该对象的不合格引用将解决)。
//createanarrayofintegers
List<Integer>primes= new ArrayList<Integer>();
primes.addAll(Arrays.asList(2,3,5,7,11,13,17));
//createparserandsetvariable'primes'asthearrayofintegers
ExpressionParserparser= new SpelExpressionParser();
StandardEvaluationContextcontext= new StandardEvaluationContext();
context.setVariable("primes",primes);
//allprimenumbers>10fromthelist(usingselection?{...})
//evaluatesto[11,13,17]
List<Integer>primesGreaterThanTen=
(List<Integer>)parser.parseExpression("#primes.?[#this>10]").getValue(context);
函数
你可以通过注册可在表达式字符串内调用的用户自定义函数来扩展SpEL。使用StandardEvaluationContext中的下列方法注册函数。
publicvoid registerFunction(Stringname,Methodm)
所引用的Java方法实现该函数,例如如下这个有用的反转字符串方法。
publicabstractclassStringUtils{
publicstaticStringreverseString(Stringinput){
StringBuilderbackwards=newStringBuilder();
for(inti=0;i<input.length();i++)
backwards.append(input.charAt(input.length()-1-i));
}
returnbackwards.toString();
}
}
随后这个方法就可以在求值上下文注册并在表达式字符串中使用了。
ExpressionParserparser= new SpelExpressionParser();
StandardEvaluationContextcontext= new StandardEvaluationContext();
context.registerFunction("reverseString",
StringUtils.class.getDeclaredMethod("reverseString",
new Class[]{String.class }));
StringhelloWorldReversed=
parser.parseExpression("#reverseString('hello')").getValue(context,String.class);
三元操作符(If-Then-Else)
可以在表达式内使用if-then-else条件逻辑三元操作符。下面是个小例子:
StringfalseString=
parser.parseExpression("false?'trueExp':'falseExp'").getValue(String.class);
这个情况下,布尔false结果返回‘falseExp‘字符串,一个真实的例子如下:
parser.parseExpression("Name").setValue(societyContext, "IEEE");
societyContext.setVariable("queryName", "NikolaTesla");
expression= "isMember(#queryName)?#queryName+'isamemberofthe'" +
"+Name+'Society':#queryName+'isnotamemberofthe'+Name+'Society'"
StringqueryResultString=
parser.parseExpression(expression).getValue(societyContext,String.class);
//queryResultString="NikolaTeslaisamemberoftheIEEESociety"
下一节将看到三元操作符更短的语法Elvis操作符。
Elvis操作符
ElvIs操作符是Groovy语言中使用的三元操作符的缩短。在三元运算符中通常要重复变量两次,例如:
Stringname="ElvisPresley";
StringdisplayName=name!=null?name:"Unknown";
可以使用Elvis操作符替代,命名为和Elvis相似的风格。
ExpressionParserparser= new SpelExpressionParser();
Stringname=parser.parseExpression("null?:'Unknown'").getValue(String.class);
System.out.println(name); //'Unknown'
这里是一个复杂的例子。
ExpressionParserparser= new SpelExpressionParser();
Inventortesla= new Inventor("NikolaTesla", "Serbian");
StandardEvaluationContextcontext= new StandardEvaluationContext(tesla);
Stringname=parser.parseExpression("Name?:'ElvisPresley'").getValue(context,String.class);
System.out.println(name); //MikeTesla
tesla.setName(null);
name=parser.parseExpression("Name?:'ElvisPresley'").getValue(context,String.class);
System.out.println(name); //ElvisPresley
安全导航操作符
安全导航操作符来源于Groovy语言,它避免了空指针异常。通常当你有一个对象的引用,在访问其方法或属性时你需要验证该引用是否为空。为了避免这种情况,安全导航操作符,只会返回null而不是抛出异常。
ExpressionParserparser= new SpelExpressionParser();
Inventortesla= new Inventor("NikolaTesla", "Serbian");
tesla.setPlaceOfBirth(new PlaceOfBirth("Smiljan"));
StandardEvaluationContextcontext= new StandardEvaluationContext(tesla);
Stringcity=parser.parseExpression("PlaceOfBirth?.City").getValue(context,String.class);
System.out.println(city); //Smiljan
tesla.setPlaceOfBirth(null);
city=parser.parseExpression("PlaceOfBirth?.City").getValue(context,String.class);
System.out.println(city); //null-doesnotthrowNullPointerException!!!
集合选择
选择是一个强大的表达式语言特性,它允许你通过从入口选择将原集合转换为其他集合。
选择使用语法?[selectExpression]。这将过滤原集合并返回包含原集合子集的新的集合。例如,选择将允许我们很容易得到Serbian 发明者列表。
List<Inventor>list=(List<Inventor>)
parser.parseExpression("Members.?[Nationality=='Serbian']").getValue(societyContext);
通常是对List和Map的选择。在前一种情况下的选择标准是针对每个人的列表求值而对Map选择标准是针对每一个映射项(Java类型Map.Entry对象)求值,而评价。Map入口有其如属性的可访问的键和值供选择使用。
这个表达式将返回一个新的Map,其元素由原始Map中项的值小于27的元素组成。
MapnewMap=parser.parseExpression("map.?[value<27]").getValue();
除了返回所有被选择的元素,也可以检索第一个和最后一个值。用^[…]获得第一个匹配入口,用$[…]获得最后一个匹配入口。
集合投影
投影允许集合驱动子表达式的求值,其结果是一个新的集合。对于投影语法![projectionExpression]。最容易理解的例子,假设我们有一个发明者列表,但要在城市名单中寻找他们在那里出生。我们要对发明者列表中每项有效地求出‘placeOfBirth.city'。使用投影:
//returns['Smiljan','Idvor']
ListplacesOfBirth=(List)parser.parseExpression("Members.![placeOfBirth.city]");
Map也可以用于驱动投影,这时投影表达式对Map中的每个项目求值(作为一个Java Map.Entry表示)。跨越Map投影的结果是对每个Map入口投影表达式求值后组成的列表。
表达式模板
表达式模板允许与一个或多个求值块复合文字文本。每个求值块有可自定义的前缀和后缀的字符定界,一个普遍的选择是使用的分隔符$()定界。例如,
StringrandomPhrase=
parser.parseExpression("randomnumberis${T(java.lang.Math).random()}",
new TemplatedParserContext()).getValue(String.class);
//evaluatesto"randomnumberis0.7038186818312008"
该字符串的求值是通过连接文字文本的‘random number is’和$()定界符内求值表达式的结果,在这里是调用的random()方法的结果。parseExpression()方法的第二个参数是ParserContext的类型。该ParserContext接口通常影响表达式如何被解析以支持模板功能。在TemplatedParserContext的定义如下所示。
publicclass TemplatedParserContext implements ParserContext{
public StringgetExpressionPrefix(){
return "${";
}
public StringgetExpressionSuffix(){
return "}";
}
publicboolean isTemplate(){
return true;
}
}
6.6 示例中使用的类
================
5.3 SpEL语法
5.3.1 基本表达式
一、字面量表达式: SpEL支持的字面量包括:字符串、数字类型(int、long、float、double)、布尔类型、null类型。
类型 | 示例 |
字符串 | String str1 = parser.parseExpression("'Hello World!'").getValue(String.class); String str2 = parser.parseExpression("\"Hello World!\"").getValue(String.class); |
数字类型 | int int1 = parser.parseExpression("1").getValue(Integer.class); long long1 = parser.parseExpression("-1L").getValue(long.class); float float1 = parser.parseExpression("1.1").getValue(Float.class); double double1 = parser.parseExpression("1.1E+2").getValue(double.class); int hex1 = parser.parseExpression("0xa").getValue(Integer.class); long hex2 = parser.parseExpression("0xaL").getValue(long.class); |
布尔类型 | boolean true1 = parser.parseExpression("true").getValue(boolean.class); boolean false1 = parser.parseExpression("false").getValue(boolean.class); |
null类型 | Object null1 = parser.parseExpression("null").getValue(Object.class); |
二、算数运算表达式: SpEL支持加(+)、减(-)、乘(*)、除(/)、求余(%)、幂(^)运算。
类型 | 示例 |
加减乘除 | int result1 = parser.parseExpression("1+2-3*4/2").getValue(Integer.class);//-3 |
求余 | int result2 = parser.parseExpression("4%3").getValue(Integer.class);//1 |
幂运算 | int result3 = parser.parseExpression("2^3").getValue(Integer.class);//8 |
SpEL还提供求余(MOD)和除(DIV)而外两个运算符,与“%”和“/”等价,不区分大小写。
三、关系表达式:等于(==)、不等于(!=)、大于(>)、大于等于(>=)、小于(<)、小于等于(<=),区间(between)运算,如“parser.parseExpression("1>2").getValue(boolean.class);”将返回false;而“parser.parseExpression("1 between {1, 2}").getValue(boolean.class);”将返回true。
between运算符右边操作数必须是列表类型,且只能包含2个元素。第一个元素为开始,第二个元素为结束,区间运算是包含边界值的,即 xxx>=list.get(0) && xxx<=list.get(1)。
SpEL同样提供了等价的“EQ” 、“NE”、 “GT”、“GE”、 “LT” 、“LE”来表示等于、不等于、大于、大于等于、小于、小于等于,不区分大小写。
四、逻辑表达式:且(and)、或(or)、非(!或NOT)。
- String expression1 = "2>1 and (!true or !false)";
- boolean result1 = parser.parseExpression(expression1).getValue(boolean.class);
- Assert.assertEquals(true, result1);
- String expression2 = "2>1 and (NOT true or NOT false)";
- boolean result2 = parser.parseExpression(expression2).getValue(boolean.class);
- Assert.assertEquals(true, result2);
String expression1 = "2>1 and (!true or !false)";
boolean result1 = parser.parseExpression(expression1).getValue(boolean.class);
Assert.assertEquals(true, result1);
String expression2 = "2>1 and (NOT true or NOT false)";
boolean result2 = parser.parseExpression(expression2).getValue(boolean.class);
Assert.assertEquals(true, result2);
注:逻辑运算符不支持 Java中的 && 和 || 。
五、字符串连接及截取表达式:使用“+”进行字符串连接,使用“'String'[0] [index]”来截取一个字符,目前只支持截取一个,如“'Hello ' + 'World!'”得到“Hello World!”;而“'Hello World!'[0]”将返回“H”。
六、三目运算及Elivis运算表达式:
三目运算符 “表达式1?表达式2:表达式3”用于构造三目运算表达式,如“2>1?true:false”将返回true;
Elivis运算符“表达式1?:表达式2”从Groovy语言引入用于简化三目运算符的,当表达式1为非null时则返回表达式1,当表达式1为null时则返回表达式2,简化了三目运算符方式“表达式1? 表达式1:表达式2”,如“null?:false”将返回false,而“true?:false”将返回true;
七、正则表达式:使用“str matches regex,如“'123' matches '\\d{3}'”将返回true;
八、括号优先级表达式:使用“(表达式)”构造,括号里的具有高优先级。
5.3.3 类相关表达式
一、类类型表达式:使用“T(Type)”来表示java.lang.Class实例,“Type”必须是类全限定名,“java.lang”包除外,即该包下的类可以不指定包名;使用类类型表达式还可以进行访问类静态方法及类静态字段。
具体使用方法如下:
- @Test
- public void testClassTypeExpression() {
- ExpressionParser parser = new SpelExpressionParser();
- //java.lang包类访问
- Class<String> result1 = parser.parseExpression("T(String)").getValue(Class.class);
- Assert.assertEquals(String.class, result1);
- //其他包类访问
- String expression2 = "T(cn.javass.spring.chapter5.SpELTest)";
- Class<String> result2 = parser.parseExpression(expression2).getValue(Class.class); Assert.assertEquals(SpELTest.class, result2);
- //类静态字段访问
- int result3=parser.parseExpression("T(Integer).MAX_VALUE").getValue(int.class);
- Assert.assertEquals(Integer.MAX_VALUE, result3);
- //类静态方法调用
- int result4 = parser.parseExpression("T(Integer).parseInt('1')").getValue(int.class);
- Assert.assertEquals(1, result4);
- }
@Test
public void testClassTypeExpression() {
ExpressionParser parser = new SpelExpressionParser();
//java.lang包类访问
Class<String> result1 = parser.parseExpression("T(String)").getValue(Class.class);
Assert.assertEquals(String.class, result1);
//其他包类访问
String expression2 = "T(cn.javass.spring.chapter5.SpELTest)";
Class<String> result2 = parser.parseExpression(expression2).getValue(Class.class); Assert.assertEquals(SpELTest.class, result2);
//类静态字段访问
int result3=parser.parseExpression("T(Integer).MAX_VALUE").getValue(int.class);
Assert.assertEquals(Integer.MAX_VALUE, result3);
//类静态方法调用
int result4 = parser.parseExpression("T(Integer).parseInt('1')").getValue(int.class);
Assert.assertEquals(1, result4);
}
对于java.lang包里的可以直接使用“T(String)”访问;其他包必须是类全限定名;可以进行静态字段访问如“T(Integer).MAX_VALUE”;也可以进行静态方法访问如“T(Integer).parseInt('1')”。
二、类实例化:类实例化同样使用java关键字“new”,类名必须是全限定名,但java.lang包内的类型除外,如String、Integer。
- @Test
- public void testConstructorExpression() {
- ExpressionParser parser = new SpelExpressionParser();
- String result1 = parser.parseExpression("new String('haha')").getValue(String.class);
- Assert.assertEquals("haha", result1);
- Date result2 = parser.parseExpression("new java.util.Date()").getValue(Date.class);
- Assert.assertNotNull(result2);
- }
@Test
public void testConstructorExpression() {
ExpressionParser parser = new SpelExpressionParser();
String result1 = parser.parseExpression("new String('haha')").getValue(String.class);
Assert.assertEquals("haha", result1);
Date result2 = parser.parseExpression("new java.util.Date()").getValue(Date.class);
Assert.assertNotNull(result2);
}
实例化完全跟Java内方式一样。
三、instanceof表达式:SpEL支持instanceof运算符,跟Java内使用同义;如“'haha' instanceof T(String)”将返回true。
四、变量定义及引用:变量定义通过EvaluationContext接口的setVariable(variableName, value)方法定义;在表达式中使用“#variableName”引用;除了引用自定义变量,SpE还允许引用根对象及当前上下文对象,使用“#root”引用根对象,使用“#this”引用当前上下文对象;
- @Test
- public void testVariableExpression() {
- ExpressionParser parser = new SpelExpressionParser();
- EvaluationContext context = new StandardEvaluationContext();
- context.setVariable("variable", "haha");
- context.setVariable("variable", "haha");
- String result1 = parser.parseExpression("#variable").getValue(context, String.class);
- Assert.assertEquals("haha", result1);
- context = new StandardEvaluationContext("haha");
- String result2 = parser.parseExpression("#root").getValue(context, String.class);
- Assert.assertEquals("haha", result2);
- String result3 = parser.parseExpression("#this").getValue(context, String.class);
- Assert.assertEquals("haha", result3);
- }
@Test
public void testVariableExpression() {
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("variable", "haha");
context.setVariable("variable", "haha");
String result1 = parser.parseExpression("#variable").getValue(context, String.class);
Assert.assertEquals("haha", result1);
context = new StandardEvaluationContext("haha");
String result2 = parser.parseExpression("#root").getValue(context, String.class);
Assert.assertEquals("haha", result2);
String result3 = parser.parseExpression("#this").getValue(context, String.class);
Assert.assertEquals("haha", result3);
}
使用“#variable”来引用在EvaluationContext定义的变量;除了可以引用自定义变量,还可以使用“#root”引用根对象,“#this”引用当前上下文对象,此处“#this”即根对象。
五、自定义函数:目前只支持类静态方法注册为自定义函数;SpEL使用StandardEvaluationContext的registerFunction方法进行注册自定义函数,其实完全可以使用setVariable代替,两者其实本质是一样的;
- @Test
- public void testFunctionExpression() throws SecurityException, NoSuchMethodException {
- ExpressionParser parser = new SpelExpressionParser();
- StandardEvaluationContext context = new StandardEvaluationContext();
- Method parseInt = Integer.class.getDeclaredMethod("parseInt", String.class);
- context.registerFunction("parseInt", parseInt);
- context.setVariable("parseInt2", parseInt);
- String expression1 = "#parseInt('3') == #parseInt2('3')";
- boolean result1 = parser.parseExpression(expression1).getValue(context, boolean.class);
- Assert.assertEquals(true, result1);
- }
@Test
public void testFunctionExpression() throws SecurityException, NoSuchMethodException {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
Method parseInt = Integer.class.getDeclaredMethod("parseInt", String.class);
context.registerFunction("parseInt", parseInt);
context.setVariable("parseInt2", parseInt);
String expression1 = "#parseInt('3') == #parseInt2('3')";
boolean result1 = parser.parseExpression(expression1).getValue(context, boolean.class);
Assert.assertEquals(true, result1);
}
此处可以看出“registerFunction”和“setVariable”都可以注册自定义函数,但是两个方法的含义不一样,推荐使用“registerFunction”方法注册自定义函数。
六、赋值表达式:SpEL即允许给自定义变量赋值,也允许给跟对象赋值,直接使用“#variableName=value”即可赋值:
- @Test
- public void testAssignExpression() {
- ExpressionParser parser = new SpelExpressionParser();
- //1.给root对象赋值
- EvaluationContext context = new StandardEvaluationContext("aaaa");
- String result1 = parser.parseExpression("#root='aaaaa'").getValue(context, String.class);
- Assert.assertEquals("aaaaa", result1);
- String result2 = parser.parseExpression("#this='aaaa'").getValue(context, String.class);
- Assert.assertEquals("aaaa", result2);
- //2.给自定义变量赋值
- context.setVariable("#variable", "variable");
- String result3 = parser.parseExpression("#variable=#root").getValue(context, String.class);
- Assert.assertEquals("aaaa", result3);
- }
@Test
public void testAssignExpression() {
ExpressionParser parser = new SpelExpressionParser();
//1.给root对象赋值
EvaluationContext context = new StandardEvaluationContext("aaaa");
String result1 = parser.parseExpression("#root='aaaaa'").getValue(context, String.class);
Assert.assertEquals("aaaaa", result1);
String result2 = parser.parseExpression("#this='aaaa'").getValue(context, String.class);
Assert.assertEquals("aaaa", result2);
//2.给自定义变量赋值
context.setVariable("#variable", "variable");
String result3 = parser.parseExpression("#variable=#root").getValue(context, String.class);
Assert.assertEquals("aaaa", result3);
}
使用“#root='aaaaa'”给根对象赋值,使用“"#this='aaaa'”给当前上下文对象赋值,使用“#variable=#root”给自定义变量赋值,很简单。
七、对象属性存取及安全导航表达式:对象属性获取非常简单,即使用如“a.property.property”这种点缀式获取,SpEL对于属性名首字母是不区分大小写的;SpEL还引入了Groovy语言中的安全导航运算符“(对象|属性)?.属性”,用来避免但“?.”前边的表达式为null时抛出空指针异常,而是返回null;修改对象属性值则可以通过赋值表达式或Expression接口的setValue方法修改。
- ExpressionParser parser = new SpelExpressionParser();
- //1.访问root对象属性
- Date date = new Date();
- StandardEvaluationContext context = new StandardEvaluationContext(date);
- int result1 = parser.parseExpression("Year").getValue(context, int.class);
- Assert.assertEquals(date.getYear(), result1);
- int result2 = parser.parseExpression("year").getValue(context, int.class);
- Assert.assertEquals(date.getYear(), result2);
ExpressionParser parser = new SpelExpressionParser();
//1.访问root对象属性
Date date = new Date();
StandardEvaluationContext context = new StandardEvaluationContext(date);
int result1 = parser.parseExpression("Year").getValue(context, int.class);
Assert.assertEquals(date.getYear(), result1);
int result2 = parser.parseExpression("year").getValue(context, int.class);
Assert.assertEquals(date.getYear(), result2);
对于当前上下文对象属性及方法访问,可以直接使用属性或方法名访问,比如此处根对象date属性“year”,注意此处属性名首字母不区分大小写。
- //2.安全访问
- context.setRootObject(null);
- Object result3 = parser.parseExpression("#root?.year").getValue(context, Object.class);
- Assert.assertEquals(null, result3);
//2.安全访问
context.setRootObject(null);
Object result3 = parser.parseExpression("#root?.year").getValue(context, Object.class);
Assert.assertEquals(null, result3);
SpEL引入了Groovy的安全导航运算符,比如此处根对象为null,所以如果访问其属性时肯定抛出空指针异常,而采用“?.”安全访问导航运算符将不抛空指针异常,而是简单的返回null。
- //3.给root对象属性赋值
- context.setRootObject(date);
- int result4 = parser.parseExpression("Year = 4").getValue(context, int.class);
- Assert.assertEquals(4, result4);
- parser.parseExpression("Year").setValue(context, 5);
- int result5 = parser.parseExpression("Year").getValue(context, int.class);
- Assert.assertEquals(5, result5);
//3.给root对象属性赋值
context.setRootObject(date);
int result4 = parser.parseExpression("Year = 4").getValue(context, int.class);
Assert.assertEquals(4, result4);
parser.parseExpression("Year").setValue(context, 5);
int result5 = parser.parseExpression("Year").getValue(context, int.class);
Assert.assertEquals(5, result5);
给对象属性赋值可以采用赋值表达式或Expression接口的setValue方法赋值,而且也可以采用点缀方式赋值。
八、对象方法调用:对象方法调用更简单,跟Java语法一样;如“'haha'.substring(2,4)”将返回“ha”;而对于根对象可以直接调用方法;
- Date date = new Date();
- StandardEvaluationContext context = new StandardEvaluationContext(date);
- int result2 = parser.parseExpression("getYear()").getValue(context, int.class);
- Assert.assertEquals(date.getYear(), result2);
Date date = new Date();
StandardEvaluationContext context = new StandardEvaluationContext(date);
int result2 = parser.parseExpression("getYear()").getValue(context, int.class);
Assert.assertEquals(date.getYear(), result2);
比如根对象date方法“getYear”可以直接调用。
九、Bean引用:SpEL支持使用“@”符号来引用Bean,在引用Bean时需要使用BeanResolver接口实现来查找Bean,Spring提供BeanFactoryResolver实现;
- @Test
- public void testBeanExpression() {
- ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
- ctx.refresh();
- ExpressionParser parser = new SpelExpressionParser();
- StandardEvaluationContext context = new StandardEvaluationContext();
- context.setBeanResolver(new BeanFactoryResolver(ctx));
- Properties result1 = parser.parseExpression("@systemProperties").getValue(context, Properties.class);
- Assert.assertEquals(System.getProperties(), result1);
- }
@Test
public void testBeanExpression() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
ctx.refresh();
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(ctx));
Properties result1 = parser.parseExpression("@systemProperties").getValue(context, Properties.class);
Assert.assertEquals(System.getProperties(), result1);
}
在示例中我们首先初始化了一个IoC容器,ClassPathXmlApplicationContext 实现默认会把“System.getProperties()”注册为“systemProperties”Bean,因此我们使用 “@systemProperties”来引用该Bean。
5.3.3 集合相关表达式
一、内联List:从Spring3.0.4开始支持内联List,使用{表达式,……}定义内联List,如“{1,2,3}”将返回一个整型的ArrayList,而“{}”将返回空的List,对于字面量表达式列表,SpEL会使用java.util.Collections.unmodifiableList方法将列表设置为不可修改。
- //将返回不可修改的空List
- List<Integer> result2 = parser.parseExpression("{}").getValue(List.class);
//将返回不可修改的空List
List<Integer> result2 = parser.parseExpression("{}").getValue(List.class);
- //对于字面量列表也将返回不可修改的List
- List<Integer> result1 = parser.parseExpression("{1,2,3}").getValue(List.class);
- Assert.assertEquals(new Integer(1), result1.get(0));
- try {
- result1.set(0, 2);
- //不可能执行到这,对于字面量列表不可修改
- Assert.fail();
- } catch (Exception e) {
- }
//对于字面量列表也将返回不可修改的List
List<Integer> result1 = parser.parseExpression("{1,2,3}").getValue(List.class);
Assert.assertEquals(new Integer(1), result1.get(0));
try {
result1.set(0, 2);
//不可能执行到这,对于字面量列表不可修改
Assert.fail();
} catch (Exception e) {
}
- //对于列表中只要有一个不是字面量表达式,将只返回原始List,
- //不会进行不可修改处理
- String expression3 = "{{1+2,2+4},{3,4+4}}";
- List<List<Integer>> result3 = parser.parseExpression(expression3).getValue(List.class);
- result3.get(0).set(0, 1);
- Assert.assertEquals(2, result3.size());
//对于列表中只要有一个不是字面量表达式,将只返回原始List,
//不会进行不可修改处理
String expression3 = "{{1+2,2+4},{3,4+4}}";
List<List<Integer>> result3 = parser.parseExpression(expression3).getValue(List.class);
result3.get(0).set(0, 1);
Assert.assertEquals(2, result3.size());
- //声明二维数组并初始化
- int[] result2 = parser.parseExpression("new int[2]{1,2}").getValue(int[].class);
//声明二维数组并初始化
int[] result2 = parser.parseExpression("new int[2]{1,2}").getValue(int[].class);
- //定义一维数组并初始化
- int[] result1 = parser.parseExpression("new int[1]").getValue(int[].class);
//定义一维数组并初始化
int[] result1 = parser.parseExpression("new int[1]").getValue(int[].class);
二、内联数组:和Java 数组定义类似,只是在定义时进行多维数组初始化。
- //定义多维数组但不初始化
- int[][][] result3 = parser.parseExpression(expression3).getValue(int[][][].class);
//定义多维数组但不初始化
int[][][] result3 = parser.parseExpression(expression3).getValue(int[][][].class);
- //错误的定义多维数组,多维数组不能初始化
- String expression4 = "new int[1][2][3]{{1}{2}{3}}";
- try {
- int[][][] result4 = parser.parseExpression(expression4).getValue(int[][][].class);
- Assert.fail();
- } catch (Exception e) {
- }
//错误的定义多维数组,多维数组不能初始化
String expression4 = "new int[1][2][3]{{1}{2}{3}}";
try {
int[][][] result4 = parser.parseExpression(expression4).getValue(int[][][].class);
Assert.fail();
} catch (Exception e) {
}
三、集合,字典元素访问:SpEL目前支持所有集合类型和字典类型的元素访问,使用“集合[索引]”访问集合元素,使用“map[key]”访问字典元素;
- //SpEL内联List访问
- int result1 = parser.parseExpression("{1,2,3}[0]").getValue(int.class);
- //即list.get(0)
- Assert.assertEquals(1, result1);
//SpEL内联List访问
int result1 = parser.parseExpression("{1,2,3}[0]").getValue(int.class);
//即list.get(0)
Assert.assertEquals(1, result1);
- //SpEL目前支持所有集合类型的访问
- Collection<Integer> collection = new HashSet<Integer>();
- collection.add(1);
- collection.add(2);
- EvaluationContext context2 = new StandardEvaluationContext();
- context2.setVariable("collection", collection);
- int result2 = parser.parseExpression("#collection[1]").getValue(context2, int.class);
- //对于任何集合类型通过Iterator来定位元素
- Assert.assertEquals(2, result2);
//SpEL目前支持所有集合类型的访问
Collection<Integer> collection = new HashSet<Integer>();
collection.add(1);
collection.add(2);
EvaluationContext context2 = new StandardEvaluationContext();
context2.setVariable("collection", collection);
int result2 = parser.parseExpression("#collection[1]").getValue(context2, int.class);
//对于任何集合类型通过Iterator来定位元素
Assert.assertEquals(2, result2);
- //SpEL对Map字典元素访问的支持
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("a", 1);
- EvaluationContext context3 = new StandardEvaluationContext();
- context3.setVariable("map", map);
- int result3 = parser.parseExpression("#map['a']").getValue(context3, int.class);
- Assert.assertEquals(1, result3);
//SpEL对Map字典元素访问的支持
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
EvaluationContext context3 = new StandardEvaluationContext();
context3.setVariable("map", map);
int result3 = parser.parseExpression("#map['a']").getValue(context3, int.class);
Assert.assertEquals(1, result3);
注:集合元素访问是通过Iterator遍历来定位元素位置的。
四、列表,字典,数组元素修改:可以使用赋值表达式或Expression接口的setValue方法修改;
- //1.修改数组元素值
- int[] array = new int[] {1, 2};
- EvaluationContext context1 = new StandardEvaluationContext();
- context1.setVariable("array", array);
- int result1 = parser.parseExpression("#array[1] = 3").getValue(context1, int.class);
- Assert.assertEquals(3, result1);
//1.修改数组元素值
int[] array = new int[] {1, 2};
EvaluationContext context1 = new StandardEvaluationContext();
context1.setVariable("array", array);
int result1 = parser.parseExpression("#array[1] = 3").getValue(context1, int.class);
Assert.assertEquals(3, result1);
- //2.修改集合值
- Collection<Integer> collection = new ArrayList<Integer>();
- collection.add(1);
- collection.add(2);
- EvaluationContext context2 = new StandardEvaluationContext();
- context2.setVariable("collection", collection);
- int result2 = parser.parseExpression("#collection[1] = 3").getValue(context2, int.class);
- Assert.assertEquals(3, result2);
- parser.parseExpression("#collection[1]").setValue(context2, 4);
- result2 = parser.parseExpression("#collection[1]").getValue(context2, int.class);
- Assert.assertEquals(4, result2);
//2.修改集合值
Collection<Integer> collection = new ArrayList<Integer>();
collection.add(1);
collection.add(2);
EvaluationContext context2 = new StandardEvaluationContext();
context2.setVariable("collection", collection);
int result2 = parser.parseExpression("#collection[1] = 3").getValue(context2, int.class);
Assert.assertEquals(3, result2);
parser.parseExpression("#collection[1]").setValue(context2, 4);
result2 = parser.parseExpression("#collection[1]").getValue(context2, int.class);
Assert.assertEquals(4, result2);
- //3.修改map元素值
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("a", 1);
- EvaluationContext context3 = new StandardEvaluationContext();
- context3.setVariable("map", map);
- int result3 = parser.parseExpression("#map['a'] = 2").getValue(context3, int.class);
- Assert.assertEquals(2, result3);
//3.修改map元素值
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
EvaluationContext context3 = new StandardEvaluationContext();
context3.setVariable("map", map);
int result3 = parser.parseExpression("#map['a'] = 2").getValue(context3, int.class);
Assert.assertEquals(2, result3);
对数组修改直接对“#array[index]”赋值即可修改元素值,同理适用于集合和字典类型。
五、集合投影:在SQL中投影指从表中选择出列,而在SpEL指根据集合中的元素中通过选择来构造另一个集合,该集合和原集合具有相同数量的元素;SpEL使用“(list|map).![投影表达式]”来进行投影运算:
- //1.首先准备测试数据
- Collection<Integer> collection = new ArrayList<Integer>();
- collection.add(4); collection.add(5);
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("a", 1); map.put("b", 2);
//1.首先准备测试数据
Collection<Integer> collection = new ArrayList<Integer>();
collection.add(4); collection.add(5);
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1); map.put("b", 2);
- //2.测试集合或数组
- EvaluationContext context1 = new StandardEvaluationContext();
- context1.setVariable("collection", collection);
- Collection<Integer> result1 =
- parser.parseExpression("#collection.![#this+1]").getValue(context1, Collection.class);
- Assert.assertEquals(2, result1.size());
- Assert.assertEquals(new Integer(5), result1.iterator().next());
//2.测试集合或数组
EvaluationContext context1 = new StandardEvaluationContext();
context1.setVariable("collection", collection);
Collection<Integer> result1 =
parser.parseExpression("#collection.![#this+1]").getValue(context1, Collection.class);
Assert.assertEquals(2, result1.size());
Assert.assertEquals(new Integer(5), result1.iterator().next());
对于集合或数组使用如上表达式进行投影运算,其中投影表达式中“#this”代表每个集合或数组元素,可以使用比如“#this.property”来获取集合元素的属性,其中“#this”可以省略。
- //3.测试字典
- EvaluationContext context2 = new StandardEvaluationContext();
- context2.setVariable("map", map);
- List<Integer> result2 =
- parser.parseExpression("#map.![ value+1]").getValue(context2, List.class);
- Assert.assertEquals(2, result2.size());
//3.测试字典
EvaluationContext context2 = new StandardEvaluationContext();
context2.setVariable("map", map);
List<Integer> result2 =
parser.parseExpression("#map.![ value+1]").getValue(context2, List.class);
Assert.assertEquals(2, result2.size());
SpEL投影运算还支持Map投影,但Map投影最终只能得到List结果,如上所示,对于投影表达式中的“#this”将是Map.Entry,所以可以使用“value”来获取值,使用“key”来获取键。
六、集合选择:在SQL中指使用select进行选择行数据,而在SpEL指根据原集合通过条件表达式选择出满足条件的元素并构造为新的集合,SpEL使用“(list|map).?[选择表达式]”,其中选择表达式结果必须是boolean类型,如果true则选择的元素将添加到新集合中,false将不添加到新集合中。
- //1.首先准备测试数据
- Collection<Integer> collection = new ArrayList<Integer>();
- collection.add(4); collection.add(5);
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("a", 1); map.put("b", 2);
//1.首先准备测试数据
Collection<Integer> collection = new ArrayList<Integer>();
collection.add(4); collection.add(5);
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1); map.put("b", 2);
- //2.集合或数组测试
- EvaluationContext context1 = new StandardEvaluationContext();
- context1.setVariable("collection", collection);
- Collection<Integer> result1 =
- parser.parseExpression("#collection.?[#this>4]").getValue(context1, Collection.class);
- Assert.assertEquals(1, result1.size());
- Assert.assertEquals(new Integer(5), result1.iterator().next());
//2.集合或数组测试
EvaluationContext context1 = new StandardEvaluationContext();
context1.setVariable("collection", collection);
Collection<Integer> result1 =
parser.parseExpression("#collection.?[#this>4]").getValue(context1, Collection.class);
Assert.assertEquals(1, result1.size());
Assert.assertEquals(new Integer(5), result1.iterator().next());
对于集合或数组选择,如“#collection.?[#this>4]”将选择出集合元素值大于4的所有元素。选择表达式必须返回布尔类型,使用“#this”表示当前元素。
- //3.字典测试
- EvaluationContext context2 = new StandardEvaluationContext();
- context2.setVariable("map", map);
- Map<String, Integer> result2 =
- parser.parseExpression("#map.?[#this.key != 'a']").getValue(context2, Map.class);
- Assert.assertEquals(1, result2.size());
- List<Integer> result3 =
- parser.parseExpression("#map.?[key != 'a'].![value+1]").getValue(context2, List.class);
- Assert.assertEquals(new Integer(3), result3.iterator().next());
//3.字典测试
EvaluationContext context2 = new StandardEvaluationContext();
context2.setVariable("map", map);
Map<String, Integer> result2 =
parser.parseExpression("#map.?[#this.key != 'a']").getValue(context2, Map.class);
Assert.assertEquals(1, result2.size());
List<Integer> result3 =
parser.parseExpression("#map.?[key != 'a'].![value+1]").getValue(context2, List.class);
Assert.assertEquals(new Integer(3), result3.iterator().next());
对于字典选择,如“#map.?[#this.key != 'a']”将选择键值不等于”a”的,其中选择表达式中“#this”是Map.Entry类型,而最终结果还是Map,这点和投影不同;集合选择和投影可以一起使用,如“#map.?[key != 'a'].![value+1]”将首先选择键值不等于”a”的,然后在选出的Map中再进行“value+1”的投影。
5.3.4 表达式模板
模板表达式就是由字面量与一个或多个表达式块组成。每个表达式块由“前缀+表达式+后缀”形式组成,如“${1+2}”即表达式块。在前边我们已经介绍了使用ParserContext接口实现来定义表达式是否是模板及前缀和后缀定义。在此就不多介绍了,如“Error ${#v0} ${#v1}”表达式表示由字面量“Error ”、模板表达式“#v0”、模板表达式“#v1”组成,其中v0和v1表示自定义变量,需要在上下文定义。