Panduan Bahasa Ekspresi Musim Semi

1. Ikhtisar

Spring Expression Language (SpEL) adalah bahasa ekspresi kuat yang mendukung kueri dan manipulasi grafik objek saat runtime. Ini dapat digunakan dengan XML atau konfigurasi Spring berbasis anotasi.

Ada beberapa operator yang tersedia dalam bahasa:

Tipe Operator
Hitung +, -, *, /,%, ^, div, mod
Relasional , ==,! =, =, lt, gt, eq, ne, le, ge
Logis dan, atau, tidak, &&, ||,!
Bersyarat ?:
Regex korek api

2. Operator

Untuk contoh ini, kami akan menggunakan konfigurasi berbasis anotasi. Detail selengkapnya tentang konfigurasi XML dapat ditemukan di bagian selanjutnya dari artikel ini.

Ekspresi SpEL dimulai dengan simbol # , dan dibungkus dengan tanda kurung kurawal: # {ekspresi} . Properti bisa direferensikan dengan cara yang sama, dimulai dengan simbol $ , dan dibungkus dengan tanda kurung: $ {property.name} . Tempat penampung properti tidak boleh berisi ekspresi SpEL, tetapi ekspresi dapat berisi referensi properti:

#{${someProperty} + 2}

Dalam contoh di atas, asumsikan someProperty memiliki nilai 2, sehingga ekspresi yang dihasilkan adalah 2 + 2, yang akan dievaluasi menjadi 4.

2.1. Operator Aritmatika

Semua operator aritmatika dasar didukung.

@Value("#{19 + 1}") // 20 private double add; @Value("#{'String1 ' + 'string2'}") // "String1 string2" private String addString; @Value("#{20 - 1}") // 19 private double subtract; @Value("#{10 * 2}") // 20 private double multiply; @Value("#{36 / 2}") // 19 private double divide; @Value("#{36 div 2}") // 18, the same as for / operator private double divideAlphabetic; @Value("#{37 % 10}") // 7 private double modulo; @Value("#{37 mod 10}") // 7, the same as for % operator private double moduloAlphabetic; @Value("#{2 ^ 9}") // 512 private double powerOf; @Value("#{(2 + 2) * 2 + 9}") // 17 private double brackets; 

Operasi membagi dan modulo memiliki alias alfabetis, div untuk / dan mod untuk % . The + operator juga dapat digunakan untuk string concatenate.

2.2. Operator Relasional dan Logis

Semua operasi relasional dan logis dasar juga didukung.

@Value("#{1 == 1}") // true private boolean equal; @Value("#{1 eq 1}") // true private boolean equalAlphabetic; @Value("#{1 != 1}") // false private boolean notEqual; @Value("#{1 ne 1}") // false private boolean notEqualAlphabetic; @Value("#{1 < 1}") // false private boolean lessThan; @Value("#{1 lt 1}") // false private boolean lessThanAlphabetic; @Value("#{1  1}") // false private boolean greaterThan; @Value("#{1 gt 1}") // false private boolean greaterThanAlphabetic; @Value("#{1 >= 1}") // true private boolean greaterThanOrEqual; @Value("#{1 ge 1}") // true private boolean greaterThanOrEqualAlphabetic; 

Semua operator relasional juga memiliki alias alfabet. Misalnya, dalam konfigurasi berbasis XML kita tidak dapat menggunakan operator yang mengandung tanda kurung sudut ( < , <=, > , > = ). Sebaliknya, kita bisa menggunakan lt (kurang dari), le (kurang dari atau sama), gt (lebih besar dari), atau ge (lebih besar dari atau sama).

2.3. Operator Logis

SpEL mendukung semua operasi logika dasar:

@Value("#") // true private boolean orAlphabetic; @Value("#{!true}") // false private boolean not; @Value("#{not true}") // false private boolean notAlphabetic;

Seperti halnya operator aritmatika dan relasional, semua operator logika juga memiliki klon alfabet.

2.4. Operator Bersyarat

Operator bersyarat digunakan untuk memasukkan nilai yang berbeda tergantung pada beberapa kondisi:

@Value("#{2 > 1 ? 'a' : 'b'}") // "a" private String ternary;

Operator terner digunakan untuk menjalankan logika kondisional if-then-else kompak di dalam ekspresi. Dalam contoh ini kami mencoba memeriksa apakah ada benar atau tidak.

Penggunaan umum lainnya untuk operator terner adalah untuk memeriksa apakah beberapa variabel null dan kemudian mengembalikan nilai variabel atau default:

@Value("#{someBean.someProperty != null ? someBean.someProperty : 'default'}") private String ternary;

Operator Elvis adalah cara mempersingkat sintaks operator terner untuk kasus di atas yang digunakan dalam bahasa Groovy. Ini juga tersedia dalam SpEL. Kode di bawah ini sama dengan kode di atas:

@Value("#{someBean.someProperty ?: 'default'}") // Will inject provided string if someProperty is null private String elvis;

2.5. Menggunakan Regex di SpEL

The pertandingan operator dapat digunakan untuk memeriksa apakah atau tidak string sesuai dengan ekspresi reguler yang diberikan.

@Value("#{'100' matches '\\d+' }") // true private boolean validNumericStringResult; @Value("#{'100fghdjf' matches '\\d+' }") // false private boolean invalidNumericStringResult; @Value("#{'valid alphabetic string' matches '[a-zA-Z\\s]+' }") // true private boolean validAlphabeticStringResult; @Value("#{'invalid alphabetic string #$1' matches '[a-zA-Z\\s]+' }") // false private boolean invalidAlphabeticStringResult; @Value("#{someBean.someValue matches '\d+'}") // true if someValue contains only digits private boolean validNumericValue;

2.6. Mengakses Objek Daftar dan Peta

Dengan bantuan SpEL, kita dapat mengakses konten Peta atau Daftar manapun dalam konteksnya. Kami akan membuat bean workerHolder baru yang akan menyimpan informasi tentang beberapa pekerja dan gaji mereka dalam Daftar dan Peta :

@Component("workersHolder") public class WorkersHolder { private List workers = new LinkedList(); private Map salaryByWorkers = new HashMap(); public WorkersHolder() { workers.add("John"); workers.add("Susie"); workers.add("Alex"); workers.add("George"); salaryByWorkers.put("John", 35000); salaryByWorkers.put("Susie", 47000); salaryByWorkers.put("Alex", 12000); salaryByWorkers.put("George", 14000); } //Getters and setters }

Sekarang kita dapat mengakses nilai koleksi menggunakan SpEL:

@Value("#{workersHolder.salaryByWorkers['John']}") // 35000 private Integer johnSalary; @Value("#{workersHolder.salaryByWorkers['George']}") // 14000 private Integer georgeSalary; @Value("#{workersHolder.salaryByWorkers['Susie']}") // 47000 private Integer susieSalary; @Value("#{workersHolder.workers[0]}") // John private String firstWorker; @Value("#{workersHolder.workers[3]}") // George private String lastWorker; @Value("#{workersHolder.workers.size()}") // 4 private Integer numberOfWorkers;

3. Gunakan dalam Konfigurasi Musim Semi

3.1. Mereferensikan Bean

Dalam contoh ini, kita akan melihat bagaimana menggunakan SpEL dalam konfigurasi berbasis XML. Ekspresi dapat digunakan untuk mereferensikan kacang atau ladang / metode kacang. Misalnya, kita memiliki kelas-kelas berikut:

public class Engine { private int capacity; private int horsePower; private int numberOfCylinders; // Getters and setters } public class Car { private String make; private int model; private Engine engine; private int horsePower; // Getters and setters }

Sekarang kita membuat konteks aplikasi di mana ekspresi digunakan untuk memasukkan nilai:

Lihatlah kacang someCar . Bidang engine dan horsePower dari someCar menggunakan ekspresi yang merupakan referensi kacang ke bidang mesin bean dan horsePower .

Untuk melakukan hal yang sama dengan konfigurasi berbasis anotasi, gunakan anotasi @Value (“# {ekspresi}”) .

3.2. Menggunakan Operator dalam Konfigurasi

Each operator from the first section of this article can be used in XML and annotation-based configurations. However, remember that in XML-based configuration, we can't use the angle bracket operator “<“. Instead, we should use the alphabetic aliases, such as lt (less than) or le (less than or equals). For annotation-based configurations, there are no such restrictions.

public class SpelOperators { private boolean equal; private boolean notEqual; private boolean greaterThanOrEqual; private boolean and; private boolean or; private String addString; // Getters and setters
 @Override public String toString() { // toString which include all fields }

Now we will add a spelOperators bean to the application context:

   = 6}"/>   300 or someCar.engine.capacity > 3000}"/>

Retrieving that bean from the context, we can then verify that values were injected properly:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); SpelOperators spelOperators = (SpelOperators) context.getBean("spelOperators"); 

Here we can see the output of the toString method of spelOperators bean:

[equal=true, notEqual=false, greaterThanOrEqual=true, and=true, or=true, addString=Some model manufactured by Some make] 

4. Parsing Expressions Programmatically

At times, we may want to parse expressions outside the context of configuration. Fortunately, this is possible, using SpelExpressionParser. We can use all operators that we saw in previous examples but should use them without braces and hash symbol. That is, if we want to use an expression with the + operator when used in Spring configuration, the syntax is #{1 + 1}; when used outside of configuration, the syntax is simply 1 + 1.

In the following examples, we will use the Car and Engine beans defined in the previous section.

4.1. Using ExpressionParser

Let's look at a simple example:

ExpressionParser expressionParser = new SpelExpressionParser(); Expression expression = expressionParser.parseExpression("'Any string'"); String result = (String) expression.getValue(); 

ExpressionParser is responsible for parsing expression strings. In this example, SpEL parser will simply evaluate the string ‘Any String' as an expression. Unsurprisingly, the result will be ‘Any String'.

As with using SpEL in configuration, we can use it to call methods, access properties, or call constructors.

Expression expression = expressionParser.parseExpression("'Any string'.length()"); Integer result = (Integer) expression.getValue();

Additionally, instead of directly operating on the literal, we could call the constructor:

Expression expression = expressionParser.parseExpression("new String('Any string').length()");

We can also access the bytes property of String class, in the same way, resulting in the byte[] representation of the string:

Expression expression = expressionParser.parseExpression("'Any string'.bytes"); byte[] result = (byte[]) expression.getValue();

We can chain method calls, just as in normal Java code:

Expression expression = expressionParser.parseExpression("'Any string'.replace(\" \", \"\").length()"); Integer result = (Integer) expression.getValue();

In this case, the result will be 9, because we have replaced whitespace with the empty string. If we don't wish to cast the expression result, we can use the generic method T getValue(Class desiredResultType), in which we can provide the desired type of class that we want to be returned. Note that EvaluationException will be thrown if the returned value cannot be cast to desiredResultType:

Integer result = expression.getValue(Integer.class);

The most common usage is to provide an expression string that is evaluated against a specific object instance:

Car car = new Car(); car.setMake("Good manufacturer"); car.setModel("Model 3"); car.setYearOfProduction(2014); ExpressionParser expressionParser = new SpelExpressionParser(); Expression expression = expressionParser.parseExpression("model"); EvaluationContext context = new StandardEvaluationContext(car); String result = (String) expression.getValue(context);

In this case, the result will be equal to the value of the model field of the car object, “Model 3“. The StandardEvaluationContext class specifies which object the expression will be evaluated against.

It cannot be changed after the context object is created. StandardEvaluationContext is expensive to construct, and during repeated usage, it builds up cached state that enables subsequent expression evaluations to be performed more quickly. Because of caching it is good practice to reuse StandardEvaluationContext where it possible if the root object does not change.

However, if the root object is changed repeatedly, we can use the mechanism shown in the example below:

Expression expression = expressionParser.parseExpression("model"); String result = (String) expression.getValue(car);

Here, we call the getValue method with an argument that represents the object to which we want to apply a SpEL expression. We can also use the generic getValue method, just as before:

Expression expression = expressionParser.parseExpression("yearOfProduction > 2005"); boolean result = expression.getValue(car, Boolean.class);

4.2. Using ExpressionParser to Set a Value

Using the setValue method on the Expression object returned by parsing an expression, we can set values on objects. SpEL will take care of type conversion. By default, SpEL uses org.springframework.core.convert.ConversionService. We can create our own custom converter between types. ConversionService is generics aware, so it can be used with generics. Let's take a look how we can use it in practice:

Car car = new Car(); car.setMake("Good manufacturer"); car.setModel("Model 3"); car.setYearOfProduction(2014); CarPark carPark = new CarPark(); carPark.getCars().add(car); StandardEvaluationContext context = new StandardEvaluationContext(carPark); ExpressionParser expressionParser = new SpelExpressionParser(); expressionParser.parseExpression("cars[0].model").setValue(context, "Other model");

The resulting car object will have modelOther model” which was changed from “Model 3“.

4.3. Parser Configuration

In the following example, we will use the following class:

public class CarPark { private List cars = new ArrayList(); // Getter and setter }

It is possible to configure ExpressionParser by calling the constructor with a SpelParserConfiguration object. For example, if we try to add car object into the cars array of CarPark class without configuring the parser, we will get an error like this:

EL1025E:(pos 4): The collection has '0' elements, index '0' is invalid

We can change the behavior of the parser, to allow it to automatically create elements if the specified index is null (autoGrowNullReferences, the first parameter to the constructor), or to automatically grow an array or list to accommodate elements beyond its initial size (autoGrowCollections, the second parameter).

SpelParserConfiguration config = new SpelParserConfiguration(true, true); StandardEvaluationContext context = new StandardEvaluationContext(carPark); ExpressionParser expressionParser = new SpelExpressionParser(config); expressionParser.parseExpression("cars[0]").setValue(context, car); Car result = carPark.getCars().get(0);

The resulting car object will be equal to the car object which was set as the first element of the cars array of carPark object from the previous example.

5. Conclusion

SpEL adalah bahasa ekspresi yang kuat dan didukung dengan baik yang dapat digunakan di semua produk dalam portofolio Spring. Ini dapat digunakan untuk mengkonfigurasi aplikasi Spring atau menulis parser untuk melakukan tugas yang lebih umum dalam aplikasi apa pun.

Contoh kode dalam artikel ini tersedia di repositori GitHub tertaut.