2. Short EL introduction Introducing SpEL General Spel Usage Using SpEL Advance usage Elaborate & Q&A Agenda :
3. Many EL out there Jboss-EL, MvEL, Unified-EL Giving Java the Dynamic-Typing power it requiresmuch like Groovy and Ruby Some EL’s are pretty powerful, most require to learn a new language entirely, but the convention is much the same, so learning one will provide tools to basically understand them all Expression Languages -
5. Spring 3.0 biggest new featureoriginal conceived in Spring.Net (don’t mistake it with the original SPEL) Works with all Spring’s portfolio Succinctly express complex concepts Can be used to wire bean properties Has many strong capabilities (but with cost!) Can be used outside of Spring What it SpEL
7. Can be a stand-alone EL parser Fully integrated into to Spring portfolio Has code completion in IntelliJ & Eclipse SpringSource Tool Suite Will be mostly used in XML & Annotations based beans definitions FundemtalsSpEL
8. More Functionality Literal Expressions Boolean, Relational &Ternary operations Regular expressions Accessing Arrays/Maps/Lists Collection projections & selections Templated expressions Full objects access & manipulation And much more…
11. Configuring beans with SpEL (@Value) @Component public class DataSourceFactory { @Value("#{systemEnvironment[‘DB_NAME']}") private String dbName; // ... } Spring consistency carries on - {#{systemEnvironment.DB_NAME
12.
13. More Ready-to-use variables #{request.getParameter(‘UserId')} #{session.getAttribute(‘UserName')} will only work in appropriate scoped beans!
14.
15. Used for raw parsing & complex expression evaluations (we’ll see it in a while)ExpressionParser parser = newSpelExpressionParser(); Expression exp = parser.parseExpression("'Hello World'"); System.out.println("expression = " + exp.getValue()); Expression raw = ((SpelExpressionParser) parser).parseRaw("1 + 5"); System.out.println("raw.getValue() = " + raw.getValue());
16.
17. The context sets and holds the #root object, it also gives us ability to override SpEL’s resolvers for constructors & methods, and even has operatorOverloding capabilities
18. A less common, but useful at times is the ParserContext that provides the behavior of the parser during the lexical stagesEvaluationContext context = new StandardEvaluationContext(); context.setVariable("name", "J.J Abrams"); parser.parseExpression("'Producer name : ' + #name").getValue(context, String.class);
19. Constructor invocations parser.parseExpression(“new String[Hi Everybody]”) #{“new com.idi.astro.basket()”} #{"new Double(3.0d)“} #{“new Integer(3.0234457d)”} This works as well as Spring’s internal automatic conversion takes places!
20.
21.
22. Using the @Value to set an object member requires a valid target (Field, Method ,Parameter), a compile error helps us there
34. One should supply an implemented ParserContext which contains a prefix & a suffix expression
35.
36.
37. With merely a few characters one is able to do predicate based selection and projection similar to that in functional programming languages
38.
39. Collection Projection Select the Clients Policy Nrs #{Clients.![PolicyNr]} This actually is not what the parser expects, and returns a list of Booleanregardless of the expected type Select Clients’ last names #{Clients.![LastName]} Select Clients’ Ages that accedes 100 #{Clients.![Age > 100]}
40.
41.
42. Constructors & methods can be resolved differently if implementing the MethodResolver or ConstructorResolver respectively
43.
44.
45.
46. What about performance, let’s say we Collection Project & Select using SpEL a large list of objects, will it be more efficient with SpEL or do it in the good old java way?
47. Well, naturally, parsing takes time, and building the lexical model is not that cheap, so using SpEL will have a cost in performance, but still…
Original SPEL – Simplest Possible Expression Language used for JSP and JSTL (Java pages Standard Tag library) replaced by the Unified EL created for JSP 2.0 and JCF