Spring EL运算符实例

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



  1. 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , le), 大于 (>, gt), 和大于或等于 (>=, ge).

  1. 逻辑运算符 – 且, 或, 非 (!).
  2. 数学运算符 – 加法(+), 减法 (-), 乘法 (*), 除法(/), 除模(%) 和指数幂 (^).

Spring EL以注解的形式

这个例子说明了如何在 SpEL 中使用运算符。
package com.yiibai.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.yiibai.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]

 

Spring EL以XML形式

请参阅在XML文件定义bean的等效版本。在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.yiibai.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.yiibai.core.Number">
		<property name="no" value="999" />
	</bean>

</beans>

本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/6367437.html,如需转载请自行联系原作者
相关文章
|
6月前
|
Java Spring 容器
如何解决spring EL注解@Value获取值为null的问题
本文探讨了在使用Spring框架时,如何避免`@Value(&quot;${xxx.xxx}&quot;)`注解导致值为null的问题。通过具体示例分析了几种常见错误场景,包括类未交给Spring管理、字段被`static`或`final`修饰以及通过`new`而非依赖注入创建对象等,提出了相应的解决方案,并强调了理解框架原理的重要性。
315 4
|
27天前
|
Java 微服务 Spring
微服务——SpringBoot使用归纳——Spring Boot中使用拦截器——拦截器使用实例
本文主要讲解了Spring Boot中拦截器的使用实例,包括判断用户是否登录和取消特定拦截操作两大场景。通过token验证实现登录状态检查,未登录则拦截请求;定义自定义注解@UnInterception实现灵活取消拦截功能。最后总结了拦截器的创建、配置及对静态资源的影响,并提供两种配置方式供选择,帮助读者掌握拦截器的实际应用。
29 0
|
9月前
|
消息中间件 Java Kafka
Spring boot 自定义kafkaTemplate的bean实例进行生产消息和发送消息
Spring boot 自定义kafkaTemplate的bean实例进行生产消息和发送消息
329 5
|
8月前
|
Java 开发者 Spring
|
8月前
|
XML Java 数据库
Spring5入门到实战------10、操作术语解释--Aspectj注解开发实例。AOP切面编程的实际应用
这篇文章是Spring5框架的实战教程,详细解释了AOP的关键术语,包括连接点、切入点、通知、切面,并展示了如何使用AspectJ注解来开发AOP实例,包括切入点表达式的编写、增强方法的配置、代理对象的创建和优先级设置,以及如何通过注解方式实现完全的AOP配置。
|
8月前
|
Java Spring
Spring Boot Admin 离线实例
Spring Boot Admin 离线实例
66 0
|
10月前
|
前端开发 安全 Java
Spring EL表达式:概念、特性与应用深入解析
Spring EL表达式:概念、特性与应用深入解析
|
10月前
|
Java Maven Spring
Spring中AOP最简单实例-@注解形式
Spring中AOP最简单实例-@注解形式
58 0
|
10月前
|
XML Java Maven
Spring中AOP最简单实例-XML形式
Spring中AOP最简单实例-XML形式
41 0
|
10月前
|
移动开发 Java Maven
基于OSGi的Virgo Server最简单Spring web实例
基于OSGi的Virgo Server最简单Spring web实例
110 0