Java ProgrammingLearn How to Work With Spring Expression Language (EL)

Learn How to Work With Spring Expression Language (EL)

Spring Expression

The Spring Expression Language, abbreviated as SpEL, is basically an expression language that is also very powerful in its workings and offers support for queries and manipulation of an object graph during runtime. The feature was released with Spring 3.0 in 2009.

While the syntax of the language is pretty similar to Unified EL, it does offer more features. Of them, the most popular ones are string templates and method invocation. There are several Java expression languages available, including JBoss EL, MVEL, ONGL and more. However, the creation of a separate Spring Expression Language was to ensure that the Spring community had a single supportive expression language that could be used across everything in the Spring framework. Many of its features are largely due to the requirements that projects are likely to have in the Spring environment, such as tooling for code completion in the Spring Tool Suite. SpEL is based on agnostic API, meaning other expression languages can be integrated when it is necessary to do so. Despite Spring being the foundation framework on which expression evaluation works, it is not dependent on it. It is independent and can be viewed as an independent expression language.

Why SpEL?
Considering that there are several languages that can be used for the evaluation of expressions, Spring does stand out for a few reasons. Here are a few reasons as to why you should consider Spring Expression Language.

  • Syntax: – SpEL can be used in both annotation metadata (Spring configuration) and XML. In both the cases, the designation takes place using the syntax #{“expression string in SpEL”} for runtime evaluation.
  • Reduces code: – SpEL is a very simple alternative to many of the other expression languages that are there. In fact, it can reduce a lot of code for you.
  • Simplified: – When using SpEL, the coder only needs to take care of writing the expressions in certain properties file. The framework code is responsible for extraction handling of the new data.

What are the features?
Spring Expression Language comes with a number of features which have been mentioned below –

  • Regular expressions
  • Literal expressions
  • Class expressions
  • Boolean and relational operators
  • Assignment operators
  • Templated expressions
  • Method invocation
  • Calling constructors
  • Collection selection and projection
  • Variables
  • Ternary operator
  • Array construction
  • User defined functions
  • Inline lists
  • Bean references
  • Accessing arrays, lists, properties, maps

Some of the features will be covered in the following examples. The other features can also be tested using sample applications. But the application structure will remain the same.

Environment setup: –
Setting up the environment for Spring based application development involves just three major steps.

  • Setting up Java Development Kit: –
    Download JDK from the Oracle site and then, install and configure it. Set the PATH and JAVA_HOME environment variables.
  • Setting of Eclipse IDE: –
    Eclipse can be downloaded from the official website. Once downloaded, unpack the binaries and then set the PATH as well.
  • Setting up Spring libraries: –
    The Spring libraries may be obtained from http://repo.spring.io/release/org/springframework/spring/. Again, it is important to set the CLASSPATH correctly.
    Now your environment is ready to start developing Spring based Java applications.

Sample application: –
In this section, we will explore the expression language used in an XML file. The expression language can also be used as annotation in Java file. If the EL is used as annotation, then XML file is not required. EL is generally written as #{SpEL expression}
Following is a simple company bean having reference of Product bean. It contains product name and price as shown below.

Listing 1: – Company bean

package com.eduonix.spel;

public class Company {
	
	private Product product;
	private String prname;
	private String prprice;
	
	public String getPrprice() {
		return prprice;
	}
	public void setPrprice(String prprice) {
		this.prprice = prprice;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public String getPrname() {
		return prname;
	}
	public void setPrname(String prname) {
		this.prname = prname;
	}	
	
}

Following is the product bean having name and price. It is referred in company bean above.

Listing 2: – Product bean

package com.eduonix.spel;

public class Product {
	
	private String name;
	private String price;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	
	

}

Now, this is the XML file. Here product bean, product name and product price is mentioned as expression language. The default values are also set in the XML file. So, these will be available in the company bean also. Please check how the expression language syntax is used in the bean file. Similar syntax is also used in case of annotation based program.

Listing 3: – Configuration XML

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="prodBean" class="com.eduonix.spel.Product">
		<property name="name" value="Car" />
		<property name="price" value="1000000" />
	</bean>

	<bean id="companyBean" class="com.eduonix.spel.Company">
		<property name="product" value="#{prodBean}" />
		<property name="prname" value="#{prodBean.name}" />
		<property name="prprice" value="#{prodBean.price}" />
	</bean>

</beans>

This is the main Java application. It gets reference of the company bean and then prints the product name and price. Some more properties and their values can also be injected in a similar way and used in the sample application.

Listing 4: – Main Java program to test

package com.eduonix.spel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SpELMain {

	public static void main(String[] args) {
	
		ApplicationContext context = new FileSystemXmlApplicationContext(
                "WebContent/WEB-INF/spelConfig.xml");

	    Company compobj = (Company) context.getBean("companyBean");
	    
	    System.out.println("Product Name: "+compobj.getPrname());
	    System.out.println("Product Price: "+compobj.getPrprice());

	}

}

Once you run the application, the following output will be display on the console.
Showing output
Conclusion: –
Spring Expression Language can be a great option if you are looking for an expression languages that is similar to the Unified EL. SpEL allows you to dynamically assign values at runtime and therefore saves a lot coding on the part of the programmer. Besides, it can also work on two different configuration schemes – XML and annotation based. So, any option can be selected as per your need. Spring EL is very powerful and it can be extended to add more functionality in your application.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -