View RSS Feed

Spring Framework

The Basics of SpEL

Rate this Entry
by , 11-16-2011 at 04:44 PM (1287 Views)
The goal of a SpEL expression is to arrive at some value after evaluation. In the course of calculating that value, other values are considered and operated upon. The simplest kinds of values that SpEL can evaluate may be literal values, references to a beanís properties, or perhaps a constant on some class.

The most basic type of SpEL expression is one that contains only a literal value. A literal value would be a perfectly valid SpEL expression:

Java Code:
<property name="message" value="The value is #{8}"/>
This is wired into this value into a beanís property by using #{} markers in a <property> elementís value attribute. The #{} markers are the clue to Spring that the content that they contain is a SpEL
expression. Note also that it can be mixed with non-SpEL values as well. There are other types that can be expressed in SpEL.

Floating-point numbers:

Java Code:
<property name="frequency" value="#{89.7}"/>
Scientific notation:

Java Code:
<property name="capacity" value="#{1e4}"/>
Literal String values expressed in either single or double quote mark:

Java Code:
<property name="name" value="#{'Chuck'}"/>
When using single quote marks for XML attribute values, then use double quotes in the SpEL expression:

Java Code:
<property name='name' value='#{"Chuck"}'/>
Literal values you may use are the Boolean true and false values:

Java Code:
<property name="enabled" value="#{false}"/>
SpEL expressions using literal values doesnít provide a great deal of value. In a later tip we will look at more interesting SpEL expressions. The key thing to keep in mind is that more powerful SpEL expressions are built up from simpler expressions. So this knowledge of basic expressions with SpEL will serve us well in the future.

Submit "The Basics of SpEL" to Facebook Submit "The Basics of SpEL" to Digg Submit "The Basics of SpEL" to Submit "The Basics of SpEL" to StumbleUpon Submit "The Basics of SpEL" to Google