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
, andnot
(!
). - 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>
Spring EL Operators example的更多相关文章
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
- Spring学习(十三)-----Spring 表达式语言(Spring EL)
本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂 ...
- Test Spring el with ExpressionParser
Spring expression language (SpEL) supports many functionality, and you can test those expression fea ...
- Spring EL运算符实例
Spring EL支持大多数标准的数学,逻辑和关系运算符. 例如, 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , l ...
- 把功能强大的Spring EL表达式应用在.net平台
Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...
- Spring EL regular expression example
Spring EL supports regular expression using a simple keyword "matches", which is really aw ...
- Spring EL Lists, Maps example
In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...
- Spring EL ternary operator (if-then-else) example
Spring EL supports ternary operator , perform "if then else" conditional checking. For exa ...
- Spring EL method invocation example
In Spring EL, you can reference a bean, and nested properties using a 'dot (.)' symbol. For example, ...
随机推荐
- 基于XMPP的即时通信系统的建立(四)— 协议详解
Presence 在XMPP协议中,我们使用presence来获取用户是否已经上线以及是否可以通信的状态. 为了能够知道自己联系人的状态以及让联系人知道自己的状态,用户上线后需要订阅联系人的状态,联系 ...
- Codeforces Round #273 (Div. 2)
A. Initial Bet 题意:给出5个数,判断它们的和是否为5的倍数,注意和为0的情况 #include<iostream> #include<cstdio> #incl ...
- ionic cordova plugin for ios
源代码结构目录: payplugin: |_src |_android |_PayPlugin.java |_ios |_CDVPayPlugin.h |_CDVPayPlugin.m |_www | ...
- I.MX6 lcd lvds hdmi bootargs
/********************************************************************* * I.MX6 lcd lvds hdmi bootarg ...
- LeetCode: Single Number I && II
I title: Given an array of integers, every element appears twice except for one. Find that single on ...
- Android好用且常用的插件及工具
1.GitHub,这个不管是做安卓还是其他,只要是开发就必上的网站,也是天朝没有墙掉为数不多的网站 2.Stack OverFlow,这个和上面一样,国外非常著名的问答网站,在上面基本上很多问题都可以 ...
- Windows环境自动获取AWR报告
1.双击awr.cmd,通过cmd窗口运行awr.sql cmd.exe /c sqlplus lcam_1230/zcpzg1z_1230@54_orcl @awr.sql awr.cmd 2.aw ...
- MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器
转:http://boke.25k5.com/kan141919.html 通过前面几篇,我们已经完成了内容类型,列表定义,列表实例g 8h"@的开发.本篇继续讲解列表中的一个重要环节- ...
- windows主线程等待子线程退出卡死问题
在windows下调用_beginthread创建子线程并获得子线程id(函数返回值),如果子线程很快退出,在主线程中调用WaitForSingleObject等待该线程id退出,会导致主线程卡死.需 ...
- [译]LINT TO SQL 介绍(数据库查询) - Part.3
出处:Linq To Sql (Part.3 – Querying our database) 术语表 Built-in:内置的 Clause:子句 Debugger:调试器 Object Relat ...