JAVA后台开发_从入门到精通

18 Spring EL运算符实例

2017-08-21  本文已影响3人  笑Skr人啊

Spring EL支持大多数标准的数学,逻辑和关系运算符。 例如,

Spring EL以注解的形式

这个例子说明了如何在 SpEL 中使用运算符。

package com.gp6.el.operator;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

    @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.gp6.el.operator;

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;
    }

}

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.gp6.el.operator" />

</beans>

测试文件

package com.gp6.el.operator;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

     public static void main( String[] args ) {
            
         ApplicationContext context = new ClassPathXmlApplicationContext(
                    "com/gp6/el/operator/el_operator.xml");

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



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以XML形式

在XML中,类似 < 小于符号不支持,应该使用下面所示文本形式,例如文本等值, ('<' = 'lt') 和 ('<=' = '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.gp6.el.operator.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.gp6.el.operator.Number">
        <property name="no" value="999" />
    </bean>
    
</beans> 

-实体类

package com.gp6.el.operator;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

    //@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;

    public boolean isTestEqual() {
        return testEqual;
    }

    public void setTestEqual(boolean testEqual) {
        this.testEqual = testEqual;
    }

    public boolean isTestNotEqual() {
        return testNotEqual;
    }

    public void setTestNotEqual(boolean testNotEqual) {
        this.testNotEqual = testNotEqual;
    }

    public boolean isTestLessThan() {
        return testLessThan;
    }

    public void setTestLessThan(boolean testLessThan) {
        this.testLessThan = testLessThan;
    }

    public boolean isTestLessThanOrEqual() {
        return testLessThanOrEqual;
    }

    public void setTestLessThanOrEqual(boolean testLessThanOrEqual) {
        this.testLessThanOrEqual = testLessThanOrEqual;
    }

    public boolean isTestGreaterThan() {
        return testGreaterThan;
    }

    public void setTestGreaterThan(boolean testGreaterThan) {
        this.testGreaterThan = testGreaterThan;
    }

    public boolean isTestGreaterThanOrEqual() {
        return testGreaterThanOrEqual;
    }

    public void setTestGreaterThanOrEqual(boolean testGreaterThanOrEqual) {
        this.testGreaterThanOrEqual = testGreaterThanOrEqual;
    }

    public boolean isTestAnd() {
        return testAnd;
    }

    public void setTestAnd(boolean testAnd) {
        this.testAnd = testAnd;
    }

    public boolean isTestOr() {
        return testOr;
    }

    public void setTestOr(boolean testOr) {
        this.testOr = testOr;
    }

    public boolean isTestNot() {
        return testNot;
    }

    public void setTestNot(boolean testNot) {
        this.testNot = testNot;
    }

    public double getTestAdd() {
        return testAdd;
    }

    public void setTestAdd(double testAdd) {
        this.testAdd = testAdd;
    }

    public String getTestAddString() {
        return testAddString;
    }

    public void setTestAddString(String testAddString) {
        this.testAddString = testAddString;
    }

    public double getTestSubtraction() {
        return testSubtraction;
    }

    public void setTestSubtraction(double testSubtraction) {
        this.testSubtraction = testSubtraction;
    }

    public double getTestMultiplication() {
        return testMultiplication;
    }

    public void setTestMultiplication(double testMultiplication) {
        this.testMultiplication = testMultiplication;
    }

    public double getTestDivision() {
        return testDivision;
    }

    public void setTestDivision(double testDivision) {
        this.testDivision = testDivision;
    }

    public double getTestModulus() {
        return testModulus;
    }

    public void setTestModulus(double testModulus) {
        this.testModulus = testModulus;
    }

    public double getTestExponentialPower() {
        return testExponentialPower;
    }

    public void setTestExponentialPower(double testExponentialPower) {
        this.testExponentialPower = 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 + "]";
    }
    
}
上一篇下一篇

猜你喜欢

热点阅读