博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring EL Operators example
阅读量:7226 次
发布时间:2019-06-29

本文共 4167 字,大约阅读时间需要 13 分钟。

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').

转载于:https://www.cnblogs.com/ghgyj/p/4749591.html

你可能感兴趣的文章
DES/3DES(TripleDES)加密、解密测试数据
查看>>
Maven项目标准目录结构
查看>>
Tomcat 系统架构与设计模式,第 1 部分: 工作原理
查看>>
Hadoop输出参数信息详解(16)
查看>>
ERROR 2002 (HY000): Can't connect to local MySQL错误
查看>>
Java版冒泡排序法
查看>>
关于FB4.6插件安装后默认语言环境的更改问题
查看>>
免费分区助手
查看>>
Javascript通过Name调用Function
查看>>
统计当前在线用户数量
查看>>
IntelliJ IDEA 乱码解决方案 (项目代码、控制台等)
查看>>
PHP项目记录
查看>>
.net面试题系列文章七(附答案)
查看>>
FastSocket
查看>>
ionic $ionicSlideBoxDelegate 滑动框事件
查看>>
点击文字,把input type="radio"也选中
查看>>
第一章 Java多线程技能
查看>>
Java 集合系列-第八篇-Map架构
查看>>
springmvc 3.2 @MatrixVariable bug 2
查看>>
React-Native PanResponder手势识别器
查看>>