One of the new features in Spring 3.0 is the Spring Expression Language (Spring EL or SpEL). While evaluation Spring 3.0 I was also checking out the SpEL capabilities, in this blog entry I'll try to cover some of the more interesting, and less obvious, features and aspects of the SpEL.
The Basics
In its basics SpEL is yet another Expression Language, similar to Unified EL, it supports expressions (no control statements in the language) mainly used to access bean properties, the Spring Expression Language can be used as part of the Spring bean factory configuration (XML or annotation based) but can also be evaluated directly by the application code - meaning we can read and evaluate expressions at runtime. Here is the most basic example of SpEL used in
XML factory bean:
<!-- Classic - Simple bean with random value -->
<bean id="randomNumber" class="java.lang.Math" factory-method="random"/>
<bean id="classicBean" class="com.jroller.blogs.eyallupu.MyBean" p:random-ref="randomNumber"/>
<!-- SpEL - Getting the value of the Random bean -->
<bean id="elBean" class="com.jroller.blogs.eyallupu.MyBean" p:random="#{randomNumber}"/>