Spring Tutorial – Spring Expression Language (EL)

Spring Expression Language

Spring Tutorial – Spring Expression Language (EL)

Vue d'ensemble:

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 dans 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, them being JBoss EL, MVEL, ONGL and more. Cependant, 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 Printemps 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.

  • Simplifié

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 – Étapes pour travailler avec 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 site officiel. 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.








Exemple d'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;

publique classe Entreprise {

privé Product product;

privé String prname;

privé String prprice;

publique String getPrprice() {

return prprice;

}

publique annulera setPrprice(String prprice) {

ce.prprice = prprice;

}

publique Product getProduct() {

return product;

}

publique annulera setProduct(Product product) {

ce.product = product;

}

publique String getPrname() {

return prname;

}

publique annulera setPrname(String prname) {

ce.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;

publique classe Product {

privé String name;

privé String price;

publique String getName() {

return nom;

}

publique annulera setName(String name) {

ce.name = name;

}

publique String getPrice() {

return price;

}

publique annulera setPrice(String price) {

ce.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

<haricots 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”>

<id = bean“prodBean” class =“com.techalpine.spel.Product”>

<property name=“nom” value=“Car” />

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

</haricot>

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

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

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

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

</haricot>

</des haricots>

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;

importer org.springframework.context.ApplicationContext;

importer org.springframework.context.support.FileSystemXmlApplicationContext;

publique classe SpELMain {

publique statique annulera principale(Chaîne[] args) {

ApplicationContext context = nouveau FileSystemXmlApplicationContext(

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

Company compobj = (Entreprise) context.getBean(“companyBean”);

Système.sur.println(“Product Name: “+compobj.getPrname());

Système.sur.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 – Apprendre la botte de printemps en,,en,Minutes,,en 30 Minutes








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.

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