Spring Tutorial – Spring Expression Language (EL)

Spring Expression Language

Spring Tutorial – Spring Expression Language (EL)

Prehľad:

The Spring Expression Language, abbreviated as SpEL, is basically an expression language. It is 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 Jáva expression languages available, them being 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 jar 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.

Must Read – Learn Spring AOP in 30 minutes








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.

Must Read – Kroky pre prácu s Spring Integration,en

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. Then the PATH and JAVA_HOME environment variables must be set.

  • 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 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.techalpine.spel;

verejnosť trieda Spoločnosť {

súkromný Product product;

súkromný String prname;

súkromný String prprice;

verejnosť String getPrprice() {

return prprice;

}

verejnosť zrušiť setPrprice(String prprice) {

this.prprice = prprice;

}

verejnosť Product getProduct() {

return product;

}

verejnosť zrušiť setProduct(Product product) {

this.product = product;

}

verejnosť String getPrname() {

return prname;

}

verejnosť zrušiť 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.techalpine.spel;

verejnosť trieda Product {

súkromný String name;

súkromný String price;

verejnosť String getName() {

return názov;

}

verejnosť zrušiť setName(String name) {

this.name = name;

}

verejnosť String getPrice() {

return price;

}

verejnosť zrušiť 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

<fazuľa 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.techalpine.spel.Product”>

<property name=“názov” value=“Car” />

<property name=“price” value=“1000000” />

</fazuľa>

<bean id =“companyBean” class =“com.techalpine.spel.Company”>

<property name=“product” value=“#{prodBean}” />

<property name=“prname” value=“#{prodBean.name}” />

<property name=“prprice” value=“#{prodBean.price}” />

</fazuľa>

</fazuľa>

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.techalpine.spel;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

verejnosť trieda SpELMain {

verejnosť statický zrušiť hlavné(Reťaz[] args) {

ApplicationContext context = nový FileSystemXmlApplicationContext(

“WebContent/WEB-INF/spelConfig.xml”);

Company compobj = (Spoločnosť) context.getBean(“companyBean”);

Systém.von.System.out.println(“Product Name: “+compobj.getPrname());

Systém.von.System.out.println(“Product Price: “+compobj.getPrprice());

}

}

Once you run the application following output will be display on the console.

Product Name: Car

Product Price: 1000000

Must Read – Learn Spring Boot in 30 Minutes








Záver:

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.

Explore – Spring related articles

 

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share