Spring Framework - Expression Language

9,459 views

Published on

The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.

Published in: Technology, Education
0 Comments
12 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,459
On SlideShare
0
From Embeds
0
Number of Embeds
299
Actions
Shares
0
Downloads
316
Comments
0
Likes
12
Embeds 0
No embeds

No notes for slide

Spring Framework - Expression Language

  1. 1. Spring Framework - Expression Language SPRING FRAMEWORK 3.0Dmitry Noskov Spring Expression Language
  2. 2. What is SpEL? is a powerful expression language much like OGNL, Jboss EL, etc. supports querying and manipulating an object graph at runtime can be used across all the products in the Spring portfolio can be used outside of Spring Spring Framework - Expression Language Dmitry Noskov
  3. 3. Features expressions accessing properties, arrays, etc. assignment method invocation collection selection & projection etc. Spring Framework - Expression Language Dmitry Noskov
  4. 4. Fundamentals ExpressionParser Expression  getValue  setValue EvaluationContext  root  setVariable  propertyAccessor Spring Framework - Expression Language Dmitry Noskov
  5. 5. Expression access configuration XML / @Value  #{expression} programming  parser.parseExpression(“expression for root”)  parser.parseExpression(“#expression for variable”) custom template  parser.parseExpression(“it is #{expression}”) Spring Framework - Expression Language Dmitry Noskov
  6. 6. Using SpEL Spring Framework - Expression Language Dmitry Noskov
  7. 7. XML<bean id="systemConfig" class="org.training.spel.SystemConfig"> <property name="operatingSystem" value="#{systemProperties[os.name]}"/> <property name="javaVersion" value="#{systemProperties[java.vm.version]}"/></bean> Spring Framework - Expression Language Dmitry Noskov
  8. 8. @Valuepublic class SystemConfig { @Value("#{systemProperties[java.vm.version]}") private String operatingSystem; @Value("#{systemProperties[java.vm.version]}") private String javaVersion;}Note: <context:annotation-config/> Spring Framework - Expression Language Dmitry Noskov
  9. 9. Expressions Spring Framework - Expression Language Dmitry Noskov
  10. 10. Literal expressionsExpressionParser parser = new SpelExpressionParser();parser.parseExpression("Hello World").getValue(String.class);parser.parseExpression("6.0221415E+23").getValue(Double.class);parser.parseExpression("0x7FFFFFFF").getValue(Integer.class);parser.parseExpression("2011/01/17").getValue(Date.class);parser.parseExpression("true").getValue();parser.parseExpression("null").getValue(); Spring Framework - Expression Language Dmitry Noskov
  11. 11. Type conversion Converter public interface Converter<S, T> { T convert(S source); } ConversionService http://static.springsource.org/spring/docs/3.0.x/spri ng-framework-reference/html/validation.html#core- convert Spring Framework - Expression Language Dmitry Noskov
  12. 12. Object properties #{person.name} #{person.Name} #{person.getName()} Spring Framework - Expression Language Dmitry Noskov
  13. 13. Collections #{list[0]} #{list[0].name} #{map[‘key’]} Spring Framework - Expression Language Dmitry Noskov
  14. 14. Methods #{‘Some Text’.substring(0, 2)} #{‘Some Text’.startsWith(‘text’)} #{“variable.toString()”} Spring Framework - Expression Language Dmitry Noskov
  15. 15. Relational operators #{5 == 5} or #{5 eq 5} #{‘black’ > ’block’} or #{‘black’ gt ‘block’} #{‘text’ instanceof T(int)} #{5.00 matches ^-?d+(.d{2})?$} Spring Framework - Expression Language Dmitry Noskov
  16. 16. Arithmetic operators #{5 + 5} #{(5 + 5) * 2} #{17 / 5 % 3} #{‘Hello’ + ‘ ‘ + ‘world’} Spring Framework - Expression Language Dmitry Noskov
  17. 17. Logical operators #{true or false} #{!true} #{not isUserInGroup(‘admin’)} Spring Framework - Expression Language Dmitry Noskov
  18. 18. AssignmentSimpleBean dima = new SimpleBean("Dima", 26);EvaluationContext context = new StandardEvaluationContext(dima);parser.parseExpression("name").setValue(context, "Dmitry");parser.parseExpression("age=27").getValue(context); Spring Framework - Expression Language Dmitry Noskov
  19. 19. Type operator #{T(java.util.Date)} #{T(String)} #{T(int)} accessing static class members  #{T(Math).PI}  #{T(Math).random()} Spring Framework - Expression Language Dmitry Noskov
  20. 20. instanceof #{‘text’ instanceof T(String)} #{27 instanceof T(Integer)} #{false instanceof T(Boolean)} Spring Framework - Expression Language Dmitry Noskov
  21. 21. Constructor #{new org.training.spel.Person(‘Misha’, 28)} #{list.add(new org.training.spel.Person())} Spring Framework - Expression Language Dmitry Noskov
  22. 22. Variable registrationMap<String, Person> map = new HashMap<String, Person>();map.put("Dima", new Person("Dima", 27));map.put("Anya", new Person("Anya", 23));ExpressionParser parser = new SpelExpressionParser();StandardEvaluationContext ctx = new StandardEvaluationContext();ctx.setVariable("map", map);ctx.setVariable("anya", "Anya");parser.parseExpression("#map[Dima]").getValue(ctx);parser.parseExpression("#map[#anya]").getValue(ctx); Spring Framework - Expression Language Dmitry Noskov
  23. 23. If-then-else #{person.age>50 ? ‘Old’ : ‘Young’} #{person.name ? : ‘N/A’} Spring Framework - Expression Language Dmitry Noskov
  24. 24. Safe navigation #{address.city?.name} #{person.name?.length()} Spring Framework - Expression Language Dmitry Noskov
  25. 25. Collection selection select all  #{list.?[age>20]}  #{list.?[name.startsWith(‘D’)]} select first  #{list.^[age>20]} select last  #{list.$[getAge()>20]} Spring Framework - Expression Language Dmitry Noskov
  26. 26. Collection projection select the names of all elements  #{list.![name]} select the names length of all elements  #{list.![name.length()]} Spring Framework - Expression Language Dmitry Noskov
  27. 27. FunctionsExpressionParser parser = new SpelExpressionParser();EvaluationContext context = new StandardEvaluationContext();context.registerFunction("max", Collections.class. getDeclaredMethod("max", new Class[]{Collection.class}));parser.parseExpression("#max(#list.![age])").getValue(context); Spring Framework - Expression Language Dmitry Noskov
  28. 28. TemplatingExpressionParser parser = new SpelExpressionParser();String value = parser.parseExpression( "Random number is #{T(java.lang.Math).random()}", new TemplateParserContext()).getValue(String.class);But:parser.parseExpression("#{#primes.?[#this>10]}", …) Spring Framework - Expression Language Dmitry Noskov
  29. 29. #root and #this array of integerlist.addAll(Arrays.asList(2,3,5,7,11,13,17));p.parseExpression("#list.?[#this>10]").getValue(context); list of ageList<Person> list = new ArrayList<Person>();p.parseExpression("#list.![age].?[#this>20]").getValue(context); Spring Framework - Expression Language Dmitry Noskov
  30. 30. Using root object unchanging StandardEvaluationContext context = new StandardEvaluationContext(new Person("Dima", 25)); parser.parseExpression("name").getValue(context); changing parser.parseExpression("name").getValue(new Person("Dima", 27)); cached context StandardEvaluationContext context = new StandardEvaluationContext(new Person("Dima", 25)); parser.parseExpression("name").getValue(context, person1); parser.parseExpression("name").getValue(context, person2); Spring Framework - Expression Language Dmitry Noskov
  31. 31. Access to Spring context<bean id="simpleBean" class="org.training.spel.Person" p:name="Misha" p:age="#{25+23}"/>ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");Person bean = context.getBean(Person.class);ExpressionParser parser = new SpelExpressionParser();StandardEvaluationContext evaluation = new StandardEvaluationContext(context);evaluation.addPropertyAccessor(new BeanFactoryAccessor());parser.parseExpression("simpleBean").getValue(evaluation); Spring Framework - Expression Language Dmitry Noskov
  32. 32. Wiring properties simple @Value("#{systemProperties[locale]}") private Locale locale; default @Value("#{systemProperties[locale]?:RU}") private Locale locale; selective @Value("#{systemProperties[level]>2 ? gold : default}") private AccountRepository repository; Spring Framework - Expression Language Dmitry Noskov
  33. 33. Information Spring type conversion reference http://static.springsource.org/spring/docs/3.0.x/spring- framework-reference/html/validation.html#core-convert Spring EL reference http://static.springsource.org/spring/docs/3.0.x/spring- framework-reference/html/expressions.html Spring Framework - Expression Language Dmitry Noskov
  34. 34. Questions Spring Framework - Expression Language Dmitry Noskov
  35. 35. The end http://www.linkedin.com/in/noskovd http://www.slideshare.net/analizator/presentations

×