Spring 表达式语言(SpEL)

来源:互联网 发布:梦幻西游手游多开软件 编辑:程序博客网 时间:2024/05/16 06:49

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 示例中使用的类

0 0
原创粉丝点击