Tutorial Musim Semi,,en,Pemindaian komponen dalam kerangka Spring,,en – Spring Expression Language (EL)

Spring Expression Language

Tutorial Musim Semi,,en,Pemindaian komponen dalam kerangka Spring,,en – Spring Expression Language (EL)

Ikhtisar:

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 di 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 Jawa 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 Musim semi 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 – Pelajari Spring AOP di,en 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 – Steps to work with Spring Integration

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

  • Setting of Eclipse IDE

Eclipse can be downloaded from the situs resmi. 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:

Di bagian ini kita akan mengeksplorasi bahasa ekspresi yang digunakan dalam file XML,,en,Bahasa ekspresi juga dapat digunakan sebagai anotasi dalam file Java,,en,Jika EL digunakan sebagai anotasi,,en,maka file XML tidak diperlukan,,en,EL umumnya ditulis sebagai,,en,ekspresi SPEL,,sv,Berikut ini adalah kacang perusahaan sederhana yang memiliki referensi kacang Produk:,,en,Ini berisi nama produk dan harga seperti yang ditunjukkan di bawah ini,,en,kacang perusahaan,,en,com.techalpine.spel,,sv,produk produk,,en,Nama string,,en,Harga tali,,en,String getHarga,,en,harga,,ro,setHargaP,,en,harga = harga,,ro,Dapatkan produkProduk,,en,setProduk,,en,produk = produk,,en,String getPrname,,en,nama depan,,en,setPrname,,en,pranama = pranama,,en,Berikut adalah produk kacang yang memiliki nama dan harga:,,en,Itu disebut dalam kacang perusahaan di atas,,en,Produk kacang,,en,Produk,,en,harga tali,,en,String getHarga,,en,harga,,en,setHarga,,nl,harga = harga,,en,ini file XMLnya,,en,Berikut produk kacang be,,en. 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;

publik kelas Perusahaan {

pribadi Product product;

pribadi String prname;

pribadi String prprice;

publik String getPrprice() {

return prprice;

}

publik membatalkan setPrprice(String prprice) {

this.prprice = prprice;

}

publik Product getProduct() {

return product;

}

publik membatalkan setProduct(Product product) {

this.product = product;

}

publik String getPrname() {

return prname;

}

publik membatalkan 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;

publik kelas Product {

pribadi String name;

pribadi String price;

publik String getName() {

return nama;

}

publik membatalkan setName(String name) {

this.name = name;

}

publik String getPrice() {

return price;

}

publik membatalkan setPrice(String price) {

this.price = price;

}

}

Now, this is the XML file. Here product bean, nama produk dan harga produk disebutkan sebagai bahasa ekspresi,,en,Nilai default juga diatur dalam file XML,,en,Jadi ini akan tersedia di kacang perusahaan juga,,en,Silakan periksa bagaimana sintaks bahasa ekspresi digunakan dalam file kacang,,en,Sintaks serupa juga digunakan dalam kasus program berbasis anotasi,,en,XML konfigurasi,,en,prodBean,,en,permainan.Produk,,nl,perusahaanBean,,ro,ejaan.Perusahaan,,en,prodBean.name,,en,prodBean.price,,en,Ini adalah aplikasi Java utama,,en,Itu mendapat referensi kacang perusahaan dan kemudian mencetak nama dan harga produk,,en,Beberapa properti lagi dan nilainya juga dapat disuntikkan dengan cara yang sama dan digunakan dalam aplikasi sampel,,en,Program Java utama untuk diuji,,en,org.springframework.context.ApplicationContext,,en,org.springframework.context.support.FileSystemXmlApplicationContext,,en,Permainan Utama,,nl,Konteks ApplicationContext =,,en,FileSystemXmlApplicationContext,,en. 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

<kacang 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=“nama” value=“Car” />

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

</kacang>

<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}” />

</kacang>

</kacang polong>

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;

mengimpor org.springframework.context.ApplicationContext;

mengimpor org.springframework.context.support.FileSystemXmlApplicationContext;

publik kelas SpELMain {

publik statis membatalkan utama(Senar[] args) {

ApplicationContext context = baru FileSystemXmlApplicationContext(

“Konten Web/WEB-INF/spelConfig.xml,,nl,Perusahaan compobj =,,en,Nama Produk,,en,compobj.getPrname,,en,Harga Produk,,en,compobj.getHarga,,en,Setelah Anda menjalankan aplikasi, output berikut akan ditampilkan di konsol,,en,Spring Expression Language bisa menjadi pilihan yang bagus jika Anda mencari bahasa ekspresi yang mirip dengan Unified EL,,en,SpEL memungkinkan Anda untuk menetapkan nilai secara dinamis saat runtime dan karenanya menghemat banyak pengkodean dari pihak programmer,,en,itu juga dapat bekerja pada dua skema konfigurasi yang berbeda – berbasis XML dan anotasi,,en,opsi apa pun dapat dipilih sesuai kebutuhan Anda,,en,Spring EL sangat kuat dan dapat diperluas untuk menambahkan lebih banyak fungsionalitas dalam aplikasi Anda,,en,Artikel terkait musim semi,,en”);

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

Sistem.di luar.println(“Product Name: “+compobj.getPrname());

Sistem.di luar.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 – Pelajari Spring Boot,,en,Menit,,en 30 Minutes








Kesimpulan:

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