Spring EL Operators example

Spring EL supports most of the standard mathematical, logical or relational operators. For example,

  • Relational operators – equal (==, eq), not equal (!=, ne), less than (<, lt), less than or equal (<= , le), greater than (>, gt), and greater than or equal (>=, ge).
  • Logical operators – and, or, and not (!).
  • Mathematical operators – addition (+), Subtraction (-), Multiplication (*), division (/), modulus (%) and exponential power (^).

Spring EL in Annotation

This example demonstrates the use of operators in SpEL.

package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer { 
    //Relational operators 
    @Value("#{1 == 1}") //true 
    private boolean testEqual; 
    @Value("#{1 != 1}") //false 
    private boolean testNotEqual; 
   @Value("#{1 < 1}") //false 
    private boolean testLessThan; 
    @Value("#{1 <= 1}") //true 
    private boolean testLessThanOrEqual; 
    @Value("#{1 > 1}") //false 
    private boolean testGreaterThan; 
    @Value("#{1 >= 1}") //true 
    private boolean testGreaterThanOrEqual; 
    //Logical operators , numberBean.no == 999 
    @Value("#{numberBean.no == 999 and numberBean.no < 900}") //false 
    private boolean testAnd; 
    @Value("#{numberBean.no == 999 or numberBean.no < 900}") //true 
    private boolean testOr; 
    @Value("#{!(numberBean.no == 999)}") //false 
    private boolean testNot; 
    //Mathematical operators @Value("#{1 + 1}") //2.0 
    private double testAdd; 
    @Value("#{'1' + '@' + '1'}") //1@1 
    private String testAddString; 
    @Value("#{1 - 1}") //0.0 
    private double testSubtraction; 
    @Value("#{1 * 1}") //1.0 
    private double testMultiplication; 
    @Value("#{10 / 2}") //5.0 
    private double testDivision; 
    @Value("#{10 % 10}") //0.0 
    private double testModulus ; 
    @Value("#{2 ^ 2}") //4.0 
    private double testExponentialPower; 
    @Override 
    public String toString() { 
        return "Customer [testEqual=" + testEqual + ", testNotEqual=" + testNotEqual + ", testLessThan=" + testLessThan + ", testLessThanOrEqual=" + testLessThanOrEqual + ", testGreaterThan=" + testGreaterThan + ", testGreaterThanOrEqual=" + testGreaterThanOrEqual + ", testAnd=" + testAnd + ", testOr=" + testOr + ", testNot=" + testNot + ", testAdd=" + testAdd + ", testAddString=" + testAddString + ", testSubtraction=" + testSubtraction + ", testMultiplication=" + testMultiplication + ", testDivision=" + testDivision + ", testModulus=" + testModulus + ", testExponentialPower=" + testExponentialPower + "]"; 
    } 
}
package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("numberBean")
public class Number { 
    @Value("999") 
    private int no; 
    public int getNo() { 
        return no; 
    } 
    public void setNo(int no) { 
        this.no = no; 
    }
}

*Run it *

Customer obj = (Customer) context.getBean("customerBean"); 
System.out.println(obj);

Output
Customer [ testEqual=true, testNotEqual=false, testLessThan=false, testLessThanOrEqual=true, testGreaterThan=false, testGreaterThanOrEqual=true, testAnd=false, testOr=true, testNot=false, testAdd=2.0, testAddString=1@1, testSubtraction=0.0, testMultiplication=1.0, testDivision=5.0, testModulus=0.0, testExponentialPower=4.0]

Spring EL in XML
See equivalent version in bean definition XML file. In XML, symbol like "less than" is always not support, instead, you should use the textual equivalents shown above, for example, ('<' = 'lt') and ('<=' = 'le').

<beans 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="customerBean" class="com.mkyong.core.Customer"> 
        <property name="testEqual" value="#{1 == 1}" /> 
        <property name="testNotEqual" value="#{1 != 1}" /> 
        <property name="testLessThan" value="#{1 lt 1}" /> 
        <property name="testLessThanOrEqual" value="#{1 le 1}" /> 
        <property name="testGreaterThan" value="#{1 > 1}" /> 
        <property name="testGreaterThanOrEqual" value="#{1 >= 1}" /> 
        <property name="testAnd" value="#{numberBean.no == 999 and numberBean.no lt 900}" /> 
         <property name="testOr" value="#{numberBean.no == 999 or numberBean.no lt 900}" /> 
         <property name="testNot" value="#{!(numberBean.no == 999)}" /> 
         <property name="testAdd" value="#{1 + 1}" /> 
         <property name="testAddString" value="#{'1' + '@' + '1'}" /> 
         <property name="testSubtraction" value="#{1 - 1}" /> 
         <property name="testMultiplication" value="#{1 * 1}" /> 
         <property name="testDivision" value="#{10 / 2}" /> 
         <property name="testModulus" value="#{10 % 10}" /> 
         <property name="testExponentialPower" value="#{2 ^ 2}" /> 
    </bean> 
    <bean id="numberBean" class="com.mkyong.core.Number"> 
        <property name="no" value="999" /> 
     </bean>
</beans>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,500評(píng)論 19 139
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,640評(píng)論 5 6
  • 01 “你將來想要一個(gè)什么樣的人?!?“很厲害的人?!?每每回想起小時(shí)候這段大言不慚的對(duì)話,都覺得既傻得可以,又天...
    影子影閱讀 5,520評(píng)論 67 249
  • “不積硅步,無以至千里;不積小流,無以成江海?!边@周我們完成了小組的報(bào)告,完成了18km的定向越野,完成了...
    趙影閱讀 261評(píng)論 0 0
  • 大概一年前,我開始發(fā)現(xiàn)自己眼角皺紋很多。于是在朋友的推薦下,我開始使用蘭蔻的護(hù)膚品,祛斑霜一滴管一千多元,眼霜一瓶...
    姵說閱讀 487評(píng)論 0 2

友情鏈接更多精彩內(nèi)容