Spring: Using a type in an expression

Source: Internet
Author: User

If you want to access the methods and constants of the class scope in Spel, you depend on the key operator of T (). For example, to express Java's math class in Spel, you need to use the T () operator in the following way:

T{java.lang.math}

The result of the T () operator shown here is a class object that represents the Java.lang.Math. If necessary, we can even assemble it into a class-type bean attribute. But the real value of the T () operator is that it can access static methods and constants of the target type.

For example, suppose you need to assemble the PI value into a bean attribute. The following spel will be able to complete the task:

T{java.lang.math}.random ()

Spel also provides a ternary operator (ternary), which is very similar to the ternary operator in Java. For example, the following expression will determine if scoreboard.score>1000, evaluates to a string of type "winner! "Otherwise, the result is loser:

#{scoreboard.score > 100? "Winner": "Loser}

A common scenario for ternary operators is to check for null values and replace NULL with a default value. For example, the expression below will determine if the value of Disc.title is null, and if it is null, then the expression will be evaluated as "Rattleand Hum":

#{disc.title? "Rattleand Hum"}

The Spel also provides query operators (.? []), which is used to filter the collection to get a subset of the set. As an example of the elaboration, suppose you want to get all the songs artist property in Jukebox to Aerosmith. The following expression uses the query operator to get all the songs that are Aerosmith:

#{jukebox.songs.? [Artist eq ' Aerosmith ']}

Spel also provides two additional query operators: ". ^[" and ". $[]", which are used to query the collection for the first match and the last occurrence, respectively. For example, consider the following expression, which looks for the first song in the list whose artist property is Aerosmith:

#{jukebox.songs.^[artist eq ' Aerosmith '}

Finally, Spel also provides the projection operator (.! []), which selects a specific attribute from each member of the collection to be placed in another collection. As an example, suppose we don't want a collection of song objects, but rather a collection of all the song names. The following expression projects the Title property into a new collection of string types:

#{jukebox.songs.! [Title]}

In fact, the projection operation can be used with any of the other spel operators. For example, we can use the following expression to get a list of the names of all Aerosmith songs:

#{jukebox.songs.? [Artist eq ' Aerosmith '].! [Title]}

Note the point:

(1) The query set has three kinds of symbols

.? [] Returns a collection

. ^[] Returns the first object of a collection

. $[] Returns the last object of the collection

(2) Mapping element set symbol

.! [] Returns a collection

(3) When querying elements, we can introduce the contrast to compare some properties, for example: Cakes.$[size > 6]

(4) When alluding to a collection of elements, the brackets inside the set is a property of an element in the collection, and the return is the collection of all elements in the collection of this property

Spring: Using a type in an expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.