Spring expression language operators example
This page gives more details about spring expression language operators and their usage. Spring expression language
supports most of the mathematical, logical and relational operations. Here is the description about each operator:
Use < or lt for less than.
Use <= or le for less than or equals to.
Use eq or == for equals.
Use ne or != for not equals.
Use gt or > for greater than.
Use ge or >= for greater than or equals.
Use and for logical and operation.
Use or for logical or operation.
Use ! for logical not operation.
Use + for addition operation.
Use - for subtraction operation.
Use * for multiplication operation.
Use / for division operation.
Use ^ for exponential power operation.
Use % for modulus operation.
Below example shows how to use these operators:
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("myNumberBean")
public class MyNumber {
@Value("1016")
private int no;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
}
|
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("myMathTestBean")
public class MyMathTest {
@Value("#{myNumberBean.no == 1016}")
private boolean isEqual;
@Value("#{myNumberBean.no > 300}")
private boolean isGreaterThan;
@Value("#{myNumberBean.no != 1015}")
private boolean isNotEquals;
@Value("#{myNumberBean.no > 1000 and myNumberBean.no < 2000}")
private boolean isInBetween;
@Value("#{myNumberBean.no == 1000 or myNumberBean.no eq 2000}")
private boolean isEitherOr;
@Value("#{myNumberBean.no + 2000}")
private int sumValue;
@Value("#{myNumberBean.no / 16}")
private int divisionValue;
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("isEqual: ").append(isEqual).append("\n");
sb.append("isGreaterThan: ").append(isGreaterThan).append("\n");
sb.append("isNotEquals: ").append(isNotEquals).append("\n");
sb.append("isInBetween: ").append(isInBetween).append("\n");
sb.append("isEitherOr: ").append(isEitherOr).append("\n");
sb.append("sumValue: ").append(sumValue).append("\n");
sb.append("divisionValue: ").append(divisionValue).append("\n");
return sb.toString();
}
}
|
Here is the xml based configuration, you must enable component scan.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.java2novice.beans" />
</beans>
|
Spring bean demo class:
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.beans.MyMathTest;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
MyMathTest mathTest = (MyMathTest) context.getBean("myMathTestBean");
System.out.println(mathTest.toString());
}
}
|
|