Spring EL支持大多数标准的数学,逻辑和关系运算符。 例如,
  1. 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , le), 大于 (>, gt), 和大于或等于 (>=, ge).
  2. 逻辑运算符 – 且, 或, 非 (!).
  3. 数学运算符 – 加法(+), 减法 (-), 乘法 (*), 除法(/), 除模(%) 和指数幂 (^).

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>

Spring EL运算符实例的更多相关文章

  1. Spring EL方法调用实例

    Spring表达式语言(使用SpEL)允许开发人员使用表达式来执行方法和将返回值以注入的方式到属性,或叫作“使用SpEL方法调用”. Spring EL在注解的形式 了解如何实现Spring EL方法 ...

  2. Spring EL bean引用实例

    在Spring EL,可以使用点(.)符号嵌套属性参考一个bean.例如,“bean.property_name”. public class Customer { @Value("#{ad ...

  3. Spring EL hello world实例

    Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行.此外,所有的Spring表达式都可以通过XML或注解. 在本教程中,我们将学习如何使用Spring表达式语言(SpEL) ...

  4. Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)

    一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...

  5. Spring EL表达式和资源调用

    Spring EL表达式     Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于在jsp的EL表达式语言.     Spring 开发中经常涉及调用各种资源的情况, ...

  6. spring 使用Spring表达式(Spring EL)

    Spring还提供了更灵活的注入方式,那就是Spring表达式,实际上Spring EL远比以上注入方式强大,我们需要学习它.Spring EL拥有很多功能. 使用Bean的id来引用Bean. •调 ...

  7. 转载:Spring+EhCache缓存实例

    转载来自:http://www.cnblogs.com/mxmbk/articles/5162813.html 一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干 ...

  8. Spring Rabbitmq HelloWorld实例

    之前的博客和大家分享了Rabbitmq的基本框架,及其工作原理,网址为 < http://www.cnblogs.com/jun-ma/p/4840869.html >.今天呢,想和大家一 ...

  9. Spring+EhCache缓存实例

    一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...

随机推荐

  1. 理解HTTP幂等性(转)

    原文链接:http://www.cnblogs.com/weidagang2046/archive/2011/06/04/2063696.html

  2. mysql 创建数据库的时候选择 utf8 bin 和 utf8 ci的区别

    utf8 ci  不区分大小写: utf8 bin 区分大小写:

  3. Builder设计模式--改善构造器多个参数时可显著改善可读性

    作为一名程序开发者,设计模式其实一直有在接触,只是没有专门的去学过,所以可能对设计模式没有一个系统的理解.在一次项目中,需要使用到第三方服务商提供的功能,为了尽快的熟悉其功能代码,在官网下了demo来 ...

  4. 转:google测试分享-分层测试

    原文: http://blog.sina.com.cn/s/blog_6cf812be0102vctg.html 上一次分享了google测试分享-SET和TE,有一些自动化测试的细节没有说清楚,那这 ...

  5. 保存进程的pid 文件目录/var/run/

    http://blog.ddup.us/?p=110 http://blog.csdn.net/fyinsonw/article/details/4113124 首先声明这不是愚人节消息,事实上这个消 ...

  6. 【转】Android打印机--没有设备驱动sdk,自己实现USB打印功能

    原文:http://blog.csdn.net/johnwcheung/article/details/71576833 Android下的设备调试,如果设备提供了驱动,按照厂家的驱动调试即可:设备未 ...

  7. HashMap在Java1.7与1.8中的区别

    基于JDK1.7.0_80与JDK1.8.0_66做的分析 JDK1.7中 使用一个Entry数组来存储数据,用key的hashcode取模来决定key会被放到数组里的位置,如果hashcode相同, ...

  8. TestDirector自定义管理:工程配置

    一.工程配置 1.进入工程配置界面 2.点击“customize project entities”弹出自定义字段对话框.(这里有六个表,每个表中的字段分为系统字段(system fields)和用户 ...

  9. SaltStack配置管理--状态间的关系(六)

    一.include的引用 需求场景:用于含有多个SLS的状态,使用include可以进行多个状态的组合,将安装apache,php,mysql集合在一个sls中 [root@7mini-node1 p ...

  10. python配置libsvm

    转载博文:win10(64-bit) + python3.6.0(64-bit) 配置libsvm-3.22 https://blog.csdn.net/weixin_35884839/article ...