Spring EL Operators示例

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

  1. 关系运算符 –等于(==,eq),不等于(!=,ne),小于(<,lt),小于或等于(<=,le),大于(>,gt)和大于或等于(> =,ge)。
  2. 逻辑运算符 –和,或,而不是(!)。
  3. 数学运算符 –加(+),减(-),乘(*),除(/),模(%)和指数幂(^)。

注释中的Spring EL

本示例演示了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;
	}

}

运行

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

输出量

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
]

XML中的Spring EL

请参见bean定义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.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>

下载源代码

下载它– Spring3-EL-Operator-Example.zip (7 KB)

参考

  1. Spring EL官方运营商参考

翻译自: https://mkyong.com/spring3/spring-el-operators-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值