Introduction to Spring Expression Language (SpEL)

Introduction to Spring Expression Language (SpEL)

4 Comments

Introduction

Spring 3 introduced the Spring Expression Language (SpEL) which has a syntax similar to Unified EL. It can save you a lot of coding, because you can dynamically assign values at runtime.

The folks at Spring, made the Spring Expression Language part of what is considered ‘Spring Core’. Thus, the Spring Expression Language is available for use throughout the Spring Framework.

Spring Framework Overview

SpEL can be configured through files and bean classes using XML and respectively Java annotation.

Spring Expression Language Examples

Project Setup

In this introductory course we will demonstrate how to use SpEL with XML and annotation based configuration to implement a simple example.

The @Component  annotation is used to register the component in Spring. Here are two components annotated correspondingly :

 

We run the application using the App class.

In the first step:
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
we create an application context using ClassPathXmlApplicationContext which loads the configuration from bean file which is located in class path of the application.
Subsequent steps load the beans (in our case Employee and Company) from the context.
In the end: context.close() the ConfigurableApplicationContext.close() will close the application context, releasing all resources and also destroying all cached singleton beans.

where applicationContext.xml content is:

The applicationContext.xml can enable the auto component-scan to avoid bean declaration in XML file:

Once we created source and bean configuration files, let us run the application. The output will be:

Employee James Smith in Acme Company company and department Information Technology

Project Dependencies:

For a Maven project the pom.xml file is:

Annotation-based configuration

The @Value  annotation allows to specify a default value by placing it on fields, methods and method/constructor parameters. College.java:

Student.java:

after we add in App class the lines:

and run again we will get as result:

Student James Smith from Eastern Florida State College with a Computer Science major

What is actually happening :

1.Beans definitions are loaded
This means scanning the application context XML files, the packages defined by component-scan, and loading the bean definitions found into the bean factory.

2.The beans are instantiated and stored in the application context
The bean factory creates each bean from the bean definitions, while the bean dependencies get injected.

3.The beans are post processed
All initializing methods declared in the XML method will be executed, while Annotations will be validated.

XML Based Configuration

Literal Values

We used literal values to bean properties:

Any data type can be used.

Bean Reference

we can refer other spring beans using SpEL:

which can be also written:

The outcome from both the above expressions is same.

Property references

We can use literal values to bean properties using SpEL:

for a nested property value a period (.) is used

for which is the same as:

Method references

are the way a method references another bean:

on which we can use operator for example:

To avoid a NullPointerExeception we should use the Safe Navigation Operator “?.”:

SpEL Predefined Variables

In SpEL “systemProperties” and “systemEnvironment“ are part of a Environment bean which provide information at runtime (late binding):

systemProperties that contains the environment variables on the machine where the application is running.
systemEnvironment – that is retrieving environment specific properties from the runtime environment, more specifically Java properties that we set in Java when the application started (using the -D argument)

Let’s first define an environment variable appHome with the value: C:/somePath and see how to make it available through SpEL.

After we autowire the Environment object in the current Spring container and we use the injected parameter by @Value  in an instance variable of the class we have the environment variable we looking for.

Running it:

will give the same result: environment variable value appHome = C:/somePath as:

where in the configuration.properties file (under resources directory) we have defined: appHome=C:/somepath

In this example we used @PropertySource  annotations to read properties file, annotation placed on core Spring configuration classes ( @Configuration ).

Conclusion

The Spring Expression Language is a powerful feature of a the Spring Framework. As you develop enterprise class applications in Spring, you will frequently encounter uses for the Spring Expression Language. A very common use for the Spring Expression Language is in defining your database settings. In a typical enterprise, you will have a development environment, a QA or testing environment, and your production deployment environment. The database server and settings will be different for each of these environments. The Spring Expression Language makes it easy to externalize your database properties.

Get the code!

Source Code

The source code for this post is available on github. You can download it here.

0

About jt

    You May Also Like

    4 comments on “Introduction to Spring Expression Language (SpEL)

    1. November 3, 2015 at 1:04 pm

      Looks like a very unique feature for Spring. Very interesting. Thank you!

      Reply
    2. May 19, 2016 at 3:18 am

      Spring Guru,

      The image for student.java is wrong, it is showing the code from employee.java.

      Reply

    Leave a Reply