Sping Expression Language -- SpEL, sping expression -- spel

Source: Internet
Author: User
Tags arithmetic operators

Sping Expression Language -- SpEL, sping expression -- spel

Spring Expression Language --- SpEL
Is a powerful expression language that supports running query and operation objects.
The syntax is similar to EL: SpEL uses # {...} as the separator. All characters in braces will be considered as SpEL.
SpEL facilitates the Dynamic assignment of bean attributes.
With SpEL, you can:
Use the bean id to reference the bean.
Call methods and attributes of referenced objects
Calculate the expression value
Regular Expression matching

SpEL: literal
Literal representation:
-Integer: <property name = "count" value = "# {5}"/>
-Decimal point: <property name = "frequency" value = "# {89.7}"/>
-Scientific Note: <property name = "capacity" value = "# {1e4}"/>
-Single or double quotation marks can be used as the separator of a String: <property name = "name" value = "# {'Tom '}"/> or <property name = "name" value = "# {" Tom "}"/>
-Boolean: <property name = "enabled" value = "# {false}"/>

SpEL: Reference beans, attributes, and methods
Reference other objects
<! -- Configure the application relationship between beans through the value Attribute and SpEL -->
<Property name = "prefix" value = "# {prefixGenerator}"/>
Reference attributes of other objects
<! -- Configure the suffix attribute value as the suffix attribute value of another bean through the value Attribute and SpEL -->
<Property name = "suffix" value = "# {sequenceGenerator2.suffix}"/>
Other methods can be called and chained
<! -- Use the value Attribute and SpEL to configure the suffix property value as the return value of another bean Method -->
<Property name = "suffix" value = "# {sequenceGenerator2.toString ()}"/>
<! -- Method link -->
<Property name = "suffix" value = "# {sequenceGenerator2.toString (). toUpperCase ()}"/>

SpEL: supported Operators
Arithmetic Operators: +,-, *,/, %, ^
The plus sign can also be used as a string connection.
Comparison operators: <,>, =, <=,> =, It, gt, eq, le, ge
Logical operators: and, or, not, |
If-else operator and ternary expression
Regular Expression: matches

Call static methods or static attributes: Call the static method of a Class through T (). It returns a Class Object and then calls the corresponding method or attribute:
<Property name = "initValue" value = "# {T (java. lang. Math). PI}"/>

Sample Code:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <bean id = "address" class = "com. yl. spel. address "> 7 <! -- Use SpEL to assign a literal value to the attribute --> 8 <property name = "city" value = "# {'beijing'}"> </property> 9 <property name =" street "value =" # {'wudaokou'} "> </property> 10 </bean> 11 12 <bean id =" car "class =" com. yl. spel. car "> 13 <property name =" brand "value =" Audi "> </property> 14 <property name =" price "value =" 500000 "> </property> 15 <! -- Use SpEL to reference static attributes of the class --> 16 <property name = "tyrePerimeter" value = "# {T (java. lang. math ). PI * 80} "> </property> 17 </bean> 18 19 <bean id =" person "class =" com. yl. spel. person "> 20 <property name =" name "value =" Tom "> </property> 21 <! -- Use SpEL to reference other beans --> 22 <property name = "car" value = "# {car}"> </property> 23 <! -- Use SpEL to reference attributes of other beans --> 24 <property name = "city" value = "# {address. city}"> </property> 25 <! -- Use the SpEL operator --> 26 <property name = "info" value = "# {car. price> 300000? 'Golden limit': 'white-collar '} "> </property> 27 </bean> 28 </beans>

 

Related Article

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.