2-4. Using the Spring Expression Language
You want to dynamically evaluate some condition or property and use it as the value configured in the
IoC container. Or perhaps you need to defer evaluation of something—not at design time but at runtime,
as might be the case in a custom scope. Or if nothing else, perhaps you just need a way to add a strong
expression language to your own application.
Use Spring 3.0's Spring Expression Language (SpEL), which provides functionality similar to the
Unified EL from JSF and JSP, or Object Graph Navigation Language (OGNL). SpEL provides easy-to-use
infrastructure that can be leveraged outside of the Spring container. Within the container, it can be used
to make configuration much easier in a lot of cases.
How It Works
Today, there are many different types of expression languages in the enterprise space. If you use
WebWork/Struts 2 or Tapestry 4, you've no doubt used the OGNL. If you've used JSP or JSF in recent
years, you've used one or both of the expression languages that are available in those environments. If
you've used JBoss Seam, you've used the expression language made available there, which is a superset
of the standard expression language shipped with JSF (Unified EL).
The expression language draws its heritage from many places. Certainly, the expression language is
a superset of what's available via the Unified EL. Spring.NET has had a similar expression language for
awhile, and the feedback has been very favorable. The need to be able to evaluate certain expressions at
arbitrary points in a life cycle, such as during a scoped beans initialization, contributed to some of the
qualities of this expression language.
Some of these expression languages are very powerful, bordering on being scripting languages in
their own right. The SpEL is no different. It's available almost everywhere you can imagine needing
it—from annotations to XML configuration. The SpringSource Tool Suite also provides robust support
for the expression language in the way of auto-completion and lookup.
Features of the Language Syntax
The expression language supports a long list of features. Table 2-1 briefly runs through the various
constructs and demonstrates their usage.