EL-Operator-Beispiel

Spring EL unterstützt die meisten mathematischen, logischen oder relationalen Standardoperatoren. Zum Beispiel,

  1. Vergleichsoperatoren - gleich (==, eq), nicht gleich (! =, Ne), kleiner als

(<, lt), kleiner oder gleich (⇐, le), größer als (>, gt) und größer als oder gleich (> =, ge).

  1. Logische Operatoren und und oder und nicht (!).

  2. Mathematische Operatoren - Addition (), Subtraktion (-),

Multiplikation (** ), Division (/), Modul (%) und Exponentialkraft (^).

Spring EL in Annotation

Dieses Beispiel zeigt die Verwendung von Operatoren 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;
    }

}

Starte es

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

Ausgabe

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

Siehe gleichwertige Version in der Bean-Definitions-XML-Datei. In XML symbolähnlich
"**  less than ** " wird immer nicht unterstützt, stattdessen sollten Sie den Text verwenden
oben gezeigte Äquivalente, zum Beispiel ('**  <** ' = '**  lt ** ') und ('**  <= ** ' =
'**  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} "/> <Eigenschaftsname =" testLessThan "value =" # {1 lt 1} "/> <Eigenschaftsname =" testLessThanOrEqual "value =" # {1 le 1} "/> <Name der Eigenschaft = "testGreaterThan" value = "# {1> 1}"/> <property name = "testGreaterThanOrEqual" value = "# {1> = 1}"/> <property name = "testAnd" value = "# {numberBean. nein == 999 und numberBean.no lt 900} "/> <property name =" testOr "value =" # {numberBean.no == 999 oder numberBean.no lt 900} "/> <property name =" testNot " = "# {! (numberBean.no == 999)}"/> <Eigenschaftsname = "testAdd" value = "# {1 + 1}"/> <Eigenschaftsname = "testAddString" value = "# {'1 '' @ '' 1 '} "/> <Eigenschaftsname =" testSubtraction "value =" # {1 - 1} "/> <Eigenschaftsname =" testMultiplication "value =" # {1 ** 1} "/> <property name = "testDivision" value = "# {10/2}"/> <Eigenschaft name = "testModulus" value = "# {10% 10}"/> <Eigenschaft name = "testExponentialPower" value = "# {2 ^ 2}"/> </bean> <bean id = "numberBean" class = " com.mkyong.core.Number "> <property name =" no "value =" 999 "/> </bean>

</Beans>

=== Quellcode herunterladen

Lade es herunter -
link://wp-content/uploads/2011/06/Spring3-EL-Operator-Example.zip[Spring3-EL-Operator-Example.zip](7 KB)

=== Referenz

. http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#d0e11931[Official
Spring EL-Betreiberreferenzen]
link://tag/spring-el/[spring el]link://tag/spring3/[spring3]