鍍金池/ 教程/ Java/ Spring EL運(yùn)算符實(shí)例
Spring EL hello world實(shí)例
Spring使用@Autowired注解自動裝配
MapFactoryBean實(shí)例
Bean InitializingBean和DisposableBean實(shí)例
Spring使用@Required注解依賴檢查
安裝Spring工具套件到Eclipse
CGLIB is required to process @Configuration classes
Spring EL Arrays, Lists, Maps實(shí)例
Spring由類型(Type)自動裝配
Spring在bean配置文件中定義電子郵件模板
Spring發(fā)送帶附件郵件
ListFactoryBean實(shí)例
Struts 2 + Spring + Hibernate集成實(shí)例
Spring bean引用實(shí)例
Spring JdbcTemplate batchUpdate()實(shí)例
Spring SetFactoryBean實(shí)例
Spring通過setter方法注入
Spring EL hello world實(shí)例
在bean配置文件中的電子郵件模板
Spring發(fā)送帶附件的Email
Spring EL 方法調(diào)用實(shí)例
Spring自動裝配Beans
Spring由構(gòu)造方法自動裝配
Spring使用@Required注解依賴檢查
Spring SimpleJdbcTemplate查詢示例
自定義@Required-style注解
Spring+JDBC實(shí)例
Spring EL三元運(yùn)算(if-then-else)實(shí)例
JdbcTemplate+JdbcDaoSupport實(shí)例
Spring自動掃描組件
Spring EL方法調(diào)用實(shí)例
Spring自動裝配Beans
Spring EL bean引用實(shí)例
Spring依賴注入(DI)
Spring EL Lists,Maps實(shí)例
Spring通過構(gòu)造方法依賴注入
通過MailSender發(fā)送電子郵件
Spring AOP實(shí)例(Pointcut, Advisor)
Spring EL運(yùn)算符實(shí)例
Spring JavaConfig實(shí)例
Spring依賴注入 (DI)
Spring EL bean引用實(shí)例
Spring由AutoDetect自動裝配
Spring由構(gòu)造方法自動裝配
Spring JdbcTemplate查詢實(shí)例
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring+JDBC實(shí)例
如何注入值到Spring bean屬性
Spring SimpleJdbcTemplate類命名參數(shù)實(shí)例
Spring PropertyPlaceholderConfigurer實(shí)例
Spring JdbcTemplate batchUpdate() 實(shí)例
Spring EL與ExpressionParser測試
Spring內(nèi)部Bean實(shí)例
加載多個Spring bean配置文件
Spring+Hibernate+MySql實(shí)例(注解)
Spring AOP+AspectJ 在XML配置實(shí)例
Spring自動代理創(chuàng)建者實(shí)例
Spring EL正則表達(dá)式實(shí)例
測試 Spring EL與ExpressionParser
java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLExcep
Spring PropertyPlaceholderConfigurer實(shí)例
Spring bean配置繼承
Spring使用Setter依賴注入
Spring自動裝配@Qualifier實(shí)例
Spring依賴檢查
Spring hello world實(shí)例
Spring依賴注入servlet會話監(jiān)聽器
Spring自定義@Required-style注解
Spring bean作用域
Spring AOP攔截器的序列
注入值到Spring Bean的屬性
Spring JdbcTemplate+JdbcDaoSupport實(shí)例
Spring教程
Spring自動裝配@Qualifier實(shí)例
Spring MapFactoryBean例子
Spring Bean作用域?qū)嵗?/span>
Cannot proxy target class because CGLIB2 is not available
Spring AOP攔截器的順序
Spring集合(List, Set, Map, and Properties)實(shí)例
Spring SimpleJdbcTemplate類命名參數(shù)實(shí)例
Spring通過自動檢測自動裝配
Spring+Hibernate+MySql實(shí)例
Spring JavaConfig @Import實(shí)例
訪問MessageSource的bean(MessageSourceAware)
Spring JdbcTemplate查詢實(shí)例
Spring JavaConfig @Import實(shí)例
Spring Object/XML映射實(shí)例
Spring Bean引用例子
Spring AOP+AspectJ注解實(shí)例
java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSe
Spring依賴檢查
Spring JavaConfig實(shí)例
Spring AOP+AspectJ注解實(shí)例
Spring bean配置繼承
Spring自動掃描組件
自動代理創(chuàng)建者實(shí)例
Spring Bean init-method 和 destroy-method實(shí)例
Spring通過構(gòu)造方法注入
Spring過濾器組件自動掃描
Spring構(gòu)造方法注入類型歧義
Spring+JDBC實(shí)例
Spring EL正則表達(dá)式實(shí)例
Spring通過Gmail SMTP服務(wù)器MailSender發(fā)送電子郵件
Spring AOP+AspectJ在XML配置實(shí)例
SetFactoryBean實(shí)例
Spring注入日期到bean屬性-CustomDateEditor
Spring AOP通知實(shí)例 – Advice
Spring使用@Autowired注解自動裝配
Spring AOP實(shí)例(Pointcut,Advisor)
Spring AOP在Hibernate的事務(wù)管理
Spring初學(xué)快速入門
Spring內(nèi)部bean實(shí)例
Spring bean加載多個配置文件
Spring + Hibernate+ MySql注解實(shí)例
Spring @PostConstruct和@PreDestroy實(shí)例
Spring依賴注入servlet會話監(jiān)聽器
Spring ListFactoryBean實(shí)例
Spring松耦合的實(shí)例
Spring過濾器組件自動掃描
Spring按名稱(Name)自動裝配
Spring AOP通知實(shí)例(Advice)
Spring AOP在Hibernate事務(wù)管理
Bean init-method和destroy-method實(shí)例
Spring EL操作符實(shí)例
Spring注入日期到bean屬性(CustomDateEditor)
資源加載程序的getResource()示例
Bean @PostConstruct和@PreDestroy實(shí)例
Struts2+Spring集成實(shí)例
Spring集合 (List,Set,Map,Properties) 實(shí)例
Spring按類型(Type)自動裝配
Spring由名稱(Name)自動裝配
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring hello world實(shí)例
Spring Bean InitializingBean和DisposableBean實(shí)例
Spring構(gòu)造方法注入類型歧義
Spring EL三元操作符(if-then-else)實(shí)例
Spring+Hibernate+MySql實(shí)例
Spring松耦合實(shí)例

Spring EL運(yùn)算符實(shí)例

Spring EL支持大多數(shù)標(biāo)準(zhǔn)的數(shù)學(xué),邏輯和關(guān)系運(yùn)算符。 例如,
  1. 關(guān)系運(yùn)算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , le), 大于 (>, gt), 和大于或等于 (>=, ge).
  2. 邏輯運(yùn)算符 – 且, 或, 非 (!).
  3. 數(shù)學(xué)運(yùn)算符 – 加法(+), 減法 (-), 乘法 (*), 除法(/), 除模(%) 和指數(shù)冪 (^).

Spring EL以注解的形式

這個例子說明了如何在 SpEL 中使用運(yùn)算符。
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;
	}

}

執(zhí)行

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

輸出結(jié)果

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中,類似 < 小于符號不支持,應(yīng)該使用下面所示文本形式,例如文本等值, ('<' = '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>