EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Spring Tutorial Spring Expression Language
 

Spring Expression Language

Priya Pedamkar
Article byPriya Pedamkar

Updated March 28, 2023

Spring Expression Language

 

 

Introduction to Spring Expression Languages (SpEL)

Spring Expression Language, usually referred as SpEL is one of the modules available under Spring’s Core Module. As the name suggests, it is a powerful expression language mainly used in Spring Framework for evaluating expressions such as Mathematical, Relational & Logical Operations, Conditional & Regular Expressions, etc., at runtime.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

It can be used by means of XML Configurations as well as Annotation based Configurations.

It is to be noted that there are many Expression Languages available for Java such as JBoss EL (Expression Language), OGNL (Object-Graph Navigation Language) & MVEL (MVFLEX Expression Language), etc., Spring Expression Language was created to satisfy the developers in Spring Community that can be used & easily coupled with other Spring Products such as Thymeleaf (Spring’s Template Engine)

SpEL couples well along with Spring Products, but it is not tightly coupled to Spring i.e., it can also be used independently. This can be achieved by creating bootstrapping few infrastructure classes by making use of Expression Parser Libraries.

Need of Spring Expression Language

As we came through, Spring Expression Language (SpEL) is very powerful in providing support for modifying the object value at run time.

Though SpEL provides support for evaluating the expression, it provides immense support in rendering the view layers using JSP (Java Server Pages) or Thymeleaf Template Engine as well as in querying the values from a various properties file (such as application.properties) in Spring Boot framework.

Example of Spring Expression Language

Let us see a practical example of using SpEL in view layer using Thymeleaf.

Spring Expression Language output 1

Spring Expression Language output 2

employee-details.html

From the above code snippets, when a request is made for /getEmployeeDetails, we are assigning the values of employee details in model object & returning employee-details.html to the browser.

In employee-details.html, by making use of Spring Expression Language (SpEL) we are performing null checks of Employee attributes. So, only if the attributes are not null, we are allowing the browser to render & print the values.

On Line 10, th:if=”${firstName != null}” – with this, we are performing null check for firstName attribute. If the firstName attribute is not null, then we are printing the value of employee’s first name by using, th:text=”${firstName}”.

Note: Here th: refers to Thymeleaf template engine.

Let us see another real time example for querying & manipulating the values using SpEL in Spring Boot Application.

country.properties

output 3

output 4

From the above code snippet, we could see that in CountryConfig class, by making use of @Value annotation, we are fetching the property values from country.properties file.

On Line 25, @Value (“#{‘${country.names}’.split(‘,’)}”) we could see that we are making use of SpEL to query the value from country.properties & splitting it based on comma(,) and assigning it to a countryList variable.

Output of countryList: [India, Australia, Singapore, HongKong]

Similarly, on Line 28, @Value (“#{${country.id}.two}”), we are making use of SpEL to query the value from a Map in country.properties file and taking out the value which is matching the key value as two.

Output of countryName:

Australia

How Spring Expression Language works?

SpEL as an expression language evaluates the given expression at the run time & these results are injected into the browser for rendering or into other beans.

Internally, SpEL is a collection of classes that Spring uses for parsing & evaluating the given expression. Mainly, the parseExpression() method on the SpELExpressionParser takes in the input expression & returns a SpELExpression object. This returned object is finally used to evaluate the expression by making a call to getValue().

SpEL provides supports for both XML & annotation-based configurations. In the advanced versions, such as in Spring Boot, @value is being used to evaluate the expression.

Some of the interfaces & classes available in SpEL are,

  • Expression (Interfaces)
  • SpelExpression (class)
  • ExpressionParser (Interface)
  • SpelExpressionParser (class)
  • EvaluationContext (Interface)
  • StandardEvaluationContext (class)

Features of Spring Expression Language

Features for evaluating expression such as:

  • Mathematical Operations: Mathematical Operations such as Addition, Subtraction, Multiplication, Divisions & Modulus are supported by SpEL.
  • Relational Operations: Relational Operations such as equals (==), not equals(!=), greater than (>), greater than or equals (>=), less than (<) less than or equals to are supported in SpEL.
  • Logical Operations: Logical Operations such as AND (&&), OR (||) & NOT(!) are supported in SpEL.
  • Ternary Operations: Ternary Operations used for evaluating if-else-then conditions are supported in SpEL.
  • Regular Expression: Regular Expressions that are used for checking whether the given string matches a given regular expression or not are supported in SpEL.
  • Literal Expression: Literal Expressions for manipulating the string operations such as concat, trim, etc., are supported by SpEL.

Let us witness a few real time examples for the above operations.

output 5

Output:

Mathematical operator value: 25

Relational operator value: true

Logical operator value: true

Ternary operator value: Success

From the code snippet, we can interpret that by instantiating SpELExpressionParser, SpEL can be used for evaluating expressions & provide results for Mathematical, Logical, Relational & Ternary Operations.

Advantages of Spring Expression Language

Some of the advantages are as follows.

  • SpEL provides supports for querying & manipulating the object value at run time.
  • SpEL provides supports for both XML Configurations as well as Annotation based Configurations
  • SpEL provides great support in View Layers such as Thymeleaf (Spring’s Template Engine) and JSP for evaluating & manipulating the objects and values to be rendered in the browser.
  • With SpEL, we can perform various operations such as Mathematical, Logical, Relational, Conditional & Ternary Operations as well as perform Literal & Regular Expression.
  • Though it is one of the modules in Spring Core, it is not tied up to Spring. SpEL can also be used independently by making use of bootstrapping infrastructure classes such as Parser.
  • The syntax used in View Layer are very simple & easy to grasp by Developers.

Conclusion

From this article, we have covered the basics of Spring Expression Language with real-time examples for justifying the need for Spring’s Expression Language in View and Service Layer. Also, we have looked at the working principle of SpEL & its API’s, core features & advantages of SpEL with real-time examples & code snippets.

Recommended Articles

This is a guide to Spring Expression Language. Here we discuss the basics of Spring Expression Language along with the examples. You may also have a look at the following articles to learn more –

  1. Spring Boot Architecture
  2. Spring Architecture
  3. What is Spring Framework?
  4. Spring Cloud Components
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW