Spring Expression Language (spel)

Source: Internet
Author: User

Spring 3.0 creates a new method for configuring object injection (set injection or constructor injection). It is spring Expression Language (spel.

▲Basic features

-- Spel usage #{...} As the delimiter, All characters in the Ampersand are considered as spel.

-- 1. literal representation

1> integer

<property name="count" value="#{5}"/>

2> decimal

<property name="frequency" value="#{89.7}"/>

3> scientific notation

<property name="capacity" value="#{1e4}"/>

4> single or double quotation marks can be used as the separator of a string.

<property name="name" value="#{'Chuck'}"/><property name='name' value='#{"Chuck"}'/>

5> Boolean

<property name="enabled" value="#{false}"/>

-- 2. Reference bean, attributes, and methods

1> reference other objects

<bean id=”saxophone” value=”com.xxx.xxx.Xxx”/><bean ..>.<property name="instrument" value="#{saxophone}"/>.<bean/>

Inject an object to the instrument attribute using ID: "saxophone", which is the same as the following Configuration:

<property name="instrument" ref="saxophone"/>

2> reference attributes of other objects

<bean id="carl"class="com.springinaction.springidol.Instrumentalist"><property name="song" value="#{kenny.song}" /></bean>

Kenny is the bean ID and song is the attribute name, so the configuration is as follows:

Instrumentalist carl = new Instrumentalist();carl.setSong(kenny.getSong());

3> call other methods

<property name="song" value="songSelector.selectSong()"/>

The selectsong () method of the object whose beanid is "songselector" is called, and the return value is injected into the song attribute. You can also perform chained operations. As follows:

<property name="song" value="songSelector.selectSong().toUpperCase()"/>

If songselector. selectsong () returns NULL, an exception is thrown. To avoid this, we need to use ?. Expression. In this way, if songselector. selectsong () is null, the subsequent method will no longer be called. As follows:

<property name="song" value="songSelector.selectSong()?.toUpperCase()"/>

4> call static methods

We already know how to call its method through an object, but how to call a static method? Use T (). It returns a Class Object
Then we can call the corresponding method:

<property name="multiplier" value="T(java.lang.Math).PI"/>

▲Operator numbers supported by spel
-- 1. Arithmetic Operator: +,-, *,/, %, ^

<property name="adjustedAmount" value="#{counter.total + 42}"/><property name="adjustedAmount" value="#{counter.total - 20}"/><property name="circumference" value="#{2 * T(java.lang.Math).PI * circle.radius}"/><property name="average" value="#{counter.total / counter.count}"/><property name="remainder" value="#{counter.total % counter.count}"/><property name="area" value="#{T(java.lang.Math).PI * circle.radius ^ 2}"/>

The plus sign can also be used as a string connection.

<property name="fullName" value="#{performer.firstName + ' ' + performer.lastName}"/>

-- 2. Comparison operators: <, >,=, <=, >=, lt, GT, EQ, le, Ge

<property name="equal" value="#{counter.total == 100}"/>

The <and> numbers cannot be used. They should have special meanings in XML. We should replace them with Lt and GT.

<property name="hasCapacity" value="#{counter.total le 100000}"/>

-- 3. logical operator number: And, or, not, |

<property name="largeCircle" value="#{shape.kind == 'circle' and shape.perimeter gt 10000}"/><property name="outOfStock" value="#{!product.available}"/><property name="outOfStock" value="#{not product.available}"/>

-- 4. If-else OPERATOR :? : (Ternary ),? : (ELVIS)

10. What is the most basic? (As we are using the El Expression Language ):

<property name="instrument" value="#{songSelector.selectSong() == 'Jingle Bells' ? piano : ' Jingle Bells '}"/>

Of the 〇 variant? :

<Property name = "song" value = "# {Kenny. Song! = NULL? Kenny. Song: 'greensleeves'} "/>

The upper and lower sides are the same semantics, but the following is obviously concise

<property name="song" value="#{kenny.song ?: 'Greensleeves'}"/>

-- 5. Regular Expression: matches

<property name="validEmail" value="#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}'}"/>

The expression returns the logical value. If the expression matches, true is returned. Otherwise, false is returned.

▲Spel's support for collections

-- Environment

The entity city is defined as follows:

package com.habuma.spel.cities;public class City {private String name;private String state;private int population;}

XML has the following definitions:

<util:list id="cities"><bean class="com.habuma.spel.cities.City"p:name="Chicago" p:state="IL" p:population="2853114"/><bean class="com.habuma.spel.cities.City"p:name="Atlanta" p:state="GA" p:population="537958"/><bean class="com.habuma.spel.cities.City"p:name="Dallas" p:state="TX" p:population="1279910"/><bean class="com.habuma.spel.cities.City"p:name="Houston" p:state="TX" p:population="2242193"/><bean class="com.habuma.spel.cities.City"p:name="Odessa" p:state="TX" p:population="90943"/><bean class="com.habuma.spel.cities.City"p:name="El Paso" p:state="TX" p:population="613190"/><bean class="com.habuma.spel.cities.City"p:name="Jal" p:state="NM" p:population="1996"/><bean class="com.habuma.spel.cities.City"p:name="Las Cruces" p:state="NM" p:population="91865"/></util:list>

-- 1. Obtain an object in the collection.

10. Access through subscript, as shown below:

<property name="chosenCity" value="#{cities[2]}"/>

We will get the city whose population is 1279910 (remember that the subscript starts from 0)

The subscript can be specified through variables, as shown below:

<property name="chosenCity" value="#{cities[T(java.lang.Math).random() * cities.size()]}"/>

If it is obtained from map, you can specify the key value as follows:

<property name="chosenCity" value="#{cities['Dallas']}"/>

You can also access the properties value through the key, as shown below:

<util:properties id="settings" location="classpath:settings.properties"/><property name="accessToken" value="#{settings['twitter.accessToken']}"/>

0. Values in systemenvironment and systemproperties can be accessed through subscript

<property name="homePath" value="#{systemEnvironment['HOME']}"/>

If-dapplication. Home =/etc/MyApp is configured during JRE running, we can access

<property name="homePath" value="#{systemProperties['application.home']}"/>

0. Obtain a character in a string by subscript

'This is a test'[3]

-- 2. Get the subset in the collection.-filter by conditions (note that the new object is a new collection)

1> filter subsets (.? [])

<property name="bigCities" value="#{cities.?[population gt 100000]}"/>

2> obtain the first (. ^ [])

<property name="aBigCity" value="#{cities.^[population gt 100000]}"/>

3> get the last one (. $ [])

<property name="aBigCity" value="#{cities.$[population gt 100000]}"/>

-- 3. projection of a set (.! [])

To obtain a list composed of the names of all cities, perform the following operations:

<property name="cityNames" value="#{cities.![name]}"/>

Returns "Chicago", "Atlanta", "Dallas"

You can also combine two columns as follows:

<property name="cityNames" value="#{cities.![name + ', ' + state]}"/>

Return "Chicago, Il", "Atlanta, Ga", and "Dallas, TX ".

-- 4. Combining projection and filtering

<property name="cityNames" value="#{cities.?[population gt 100000].![name + ', ' + state]}"/>

 

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.