From 8ac0faff06c7785c5cfd1a1448a60e6f969bf32b Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Tue, 6 Jul 2010 21:40:25 +0000 Subject: [PATCH] SpEL documentation (!) on @Bean referencing, inline lists and array construction git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3474 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../src/expressions.xml | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/spring-framework-reference/src/expressions.xml b/spring-framework-reference/src/expressions.xml index bbfc8f8019d..299c3f51f15 100644 --- a/spring-framework-reference/src/expressions.xml +++ b/spring-framework-reference/src/expressions.xml @@ -84,6 +84,18 @@ Calling constructors + + + Bean references + + + + Array construction + + + + Inline lists + Ternary operator @@ -563,7 +575,42 @@ parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").setValue( +
+ Inline lists + + Lists can be expressed directly in an expression using {} notation. + + + +// evaluates to a Java list containing the four numbers +List numbers = (List) parser.parseExpression("{1,2,3,4}").getValue(context); + +List listOfLists = (List) parser.parseExpression("{{'a','b'},{'x','y'}}").getValue(context); + + {} by itself means an empty list. For performance reasons, if the + list is itself entirely composed of fixed literals then a constant list is created + to represent the expression, rather than building a new list on each evaluation. +
+ +
+ Array construction + Arrays can be built using the familiar Java syntax, optionally + supplying an initializer to have the array populated at construction time. + + + int[] numbers1 = (int[]) parser.parseExpression("new int[4]").getValue(context); + +// Array with initializer +int[] numbers2 = (int[]) parser.parseExpression("new int[]{1,2,3}").getValue(context); + +// Multi dimensional array +int[][] numbers3 = (int[][]) parser.parseExpression("new int[4][5]").getValue(context); + + It is not currently allowed to supply an initializer when constructing + a multi-dimensional array. +
+
Methods @@ -858,6 +905,19 @@ String helloWorldReversed = parser.parseExpression("#reverseString('hello')").getValue(context, String.class);
+
+ Bean references + If the evaluation context has been configured with a bean resolver it is possible to + lookup beans from an expression using the (@) symbol. + + ExpressionParser parser = new SpelExpressionParser(); +StandardEvaluationContext context = new StandardEvaluationContext(); +context.setBeanResolver(new MyBeanResolver()); + +// This will end up calling resolve(context,"foo") on MyBeanResolver during evaluation +Object bean = parser.parseExpression("@foo").getValue(context); +
+
Ternary Operator (If-Then-Else)