// evals to 1856int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);
属性名称的第一个字母允许不区分大小写。数组和列表的内容是使用方括号表示法获得的,如下例所示
ExpressionParser parser =newSpelExpressionParser();EvaluationContext context =SimpleEvaluationContext.forReadOnlyDataBinding().build();// Inventions Array// evaluates to "Induction motor"String invention =parser.parseExpression("inventions[3]").getValue( context, tesla,String.class);// Members List// evaluates to "Nikola Tesla"String name =parser.parseExpression("Members[0].Name").getValue( context, ieee,String.class);// List and Array navigation// evaluates to "Wireless communication"String invention =parser.parseExpression("Members[0].Inventions[6]").getValue( context, ieee,String.class);
映射的内容是通过在括号内指定文本键值获得的:
// Officer's DictionaryInventor pupin =parser.parseExpression("Officers['president']").getValue( societyContext,Inventor.class);// evaluates to "Idvor"String city =parser.parseExpression("Officers['president'].PlaceOfBirth.City").getValue( societyContext,String.class);// setting valuesparser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").setValue( societyContext,"Croatia");
Inline List
你可以直接在表达式中表示列表:
// evaluates to a Java list containing the four numbersList numbers = (List) parser.parseExpression("{1,2,3,4}").getValue(context);List listOfLists = (List) parser.parseExpression("{{'a','b'},{'x','y'}}").getValue(context);
Inline Map
你还可以使用key:value表示法在表达式中直接表示映射。以下示例显示了如何执行此操作:
// evaluates to a Java map containing the two entriesMap inventorInfo = (Map) parser.parseExpression("{name:'Nikola',dob:'10-July-1856'}").getValue(context);Map mapOfMaps = (Map) parser.parseExpression("{name:{first:'Nikola',last:'Tesla'},dob:{day:10,month:'July',year:1856}}").getValue(context);
Inventor einstein =p.parseExpression("new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')").getValue(Inventor.class);//create new inventor instance within add method of Listp.parseExpression("Members.add(new org.spring.samples.spel.inventor.Inventor( 'Albert Einstein', 'German'))").getValue(societyContext);
// create an array of integersList<Integer> primes =newArrayList<Integer>();primes.addAll(Arrays.asList(2,3,5,7,11,13,17));// create parser and set variable 'primes' as the array of integersExpressionParser parser =newSpelExpressionParser();EvaluationContext context =SimpleEvaluationContext.forReadOnlyDataAccess();context.setVariable("primes", primes);// all prime numbers > 10 from the list (using selection ?{...})// evaluates to [11, 13, 17]List<Integer> primesGreaterThanTen = (List<Integer>) parser.parseExpression("#primes.?[#this>10]").getValue(context);
ExpressionParser parser =newSpelExpressionParser();StandardEvaluationContext context =newStandardEvaluationContext();context.setBeanResolver(newMyBeanResolver());// This will end up calling resolve(context,"something") on MyBeanResolver during evaluationObject bean =parser.parseExpression("@something").getValue(context);
要访问工厂bean本身,您应该在bean名称前面加上&符号。以下示例显示了如何执行此操作:
ExpressionParser parser =newSpelExpressionParser();StandardEvaluationContext context =newStandardEvaluationContext();context.setBeanResolver(newMyBeanResolver());// This will end up calling resolve(context,"&foo") on MyBeanResolver during evaluationObject bean =parser.parseExpression("&foo").getValue(context);
String randomPhrase =parser.parseExpression("random number is #{T(java.lang.Math).random()}",newTemplateParserContext()).getValue(String.class);// evaluates to "random number is 0.7038186818312008"
字符串的计算方法是将文本“random number is”与计算#{ }分隔符内表达式的结果(在本例中,是调用该random()方法的结果)连接起来。parseExpression()方法的第二个参数的类型为parserContext。ParserContext接口用于影响表达式的解析方式,以支持表达式模板化功能。TemplateParserContext的定义如下: