- Spring初学快速入门
- 安装Spring工具套件到Eclipse
- Spring快速入门
- Spring自动装配Bean
- Spring JDBC支持
- Spring JavaConfig
- Spring 依赖注入(DI)
-
Spring Bean基础
- Spring Bean引用例子
- 如何注入值到Spring bean属性
- Spring bean加载多个配置文件
- Spring内部bean实例
- Spring Bean作用域实例
- Spring集合 (List,Set,Map,Properties) 实例
- Spring ListFactoryBean实例
- Spring SetFactoryBean实例
- Spring MapFactoryBean例子
- Spring注入日期到bean属性-CustomDateEditor
- Spring PropertyPlaceholderConfigurer实例
- Spring bean配置继承
- Spring依赖检查
- Spring使用@Required注解依赖检查
- Spring自定义@Required-style注解
- Spring Bean InitializingBean和DisposableBean实例
- Spring Bean init-method 和 destroy-method实例
- Spring @PostConstruct和@PreDestroy实例
- Spring表达式语言
- Spring自动组件扫描
- Spring AOP (面向方面编程)
- Spring AOP + AspectJ框架
- Spring Hibernate支持
- Spring E-mail支持
-
Spring与其它Web框架集成
Spring EL运算符实例
Spring EL支持大多数标准的数学,逻辑和关系运算符。 例如,
- 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , le), 大于 (>, gt), 和大于或等于 (>=, ge).
- 逻辑运算符 – 且, 或, 非 (!).
- 数学运算符 – 加法(+), 减法 (-), 乘法 (*), 除法(/), 除模(%) 和指数幂 (^).
Spring EL以注解的形式
这个例子说明了如何在 SpEL 中使用运算符。
package com.zyiz.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.zyiz.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.zyiz.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.zyiz.core.Number"> <property name="no" value="999" /> </bean> </beans>
分类导航
- Java教程
- Vim教程
- Swing教程
- Spring教程
- Spring Web Services教程
- Spring MVC教程
- Spring JDBC教程
- Spring Cloud教程
- Spring Boot教程
- Spring Boot CLI教程
- Spring Batch教程
- Spring AOP教程
- PDFBox教程
- JSP教程
- JSF教程
- JPA教程
- Java面向对象设计
- Java设计模式
- Java虚拟机教程
- Java泛型教程
- Java正则表达式教程
- Java数据类型教程
- Java并发编程教程
- Java密码学教程
- Java多线程教程
- Java国际化(i18n)教程
- JavaFX教程
- Java9教程
关注微信小程序
扫描二维码
程序员编程王