Spring Tutorial - Spring Expression Language Operators








Spring Expression Language supports the standard mathematical, logical or relational operators.

The following Relational operators are supported.

  • equal: ==, eq
  • not equal: !=, ne
  • less than: <, lt
  • less than or equal: <= , le
  • greater than: >, gt
  • greater than or equal: >=, ge

The following Logical operators are supported.

  • and
  • or
  • not (!)

The following Mathematical operators are supported.

  • addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Exponential power (^)




Example

The following code shows how to use operators from Spring expression language.

package com.java2s.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Server {

//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;
}




Spring EL Operator in XML

We can also use the operators in bean definition XML file.

<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.java2s.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>
</beans>

Spring EL ternary operator (if-then-else)

Spring Expression Language ternary operator has the following syntax. and it performs if then else conditional logic.

condition ? trueAction : falseAction

If condition is true it will execute the trueAction, if the the condition is false it will run the falseAction.

The following Java bean has a property value for qtyOnHand whose value is 99.

package com.java2s.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("itemBean")
public class Item {
  @Value("99")
  private int qtyOnHand;
}

The Customer bean use ternary operator with @Value annotation. If "itemBean.qtyOnHand" is less than 100, then set "customerBean.warning" to true, else set it to false.

package com.java2s.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

  @Value("#{itemBean.qtyOnHand < 100 ? true : false}")
  private boolean warning;
}

The following xml configuration file shows how to use tenary operator in xml markup.

<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="itemBean" class="com.java2s.core.Item">
    <property name="qtyOnHand" value="99" />
  </bean>

  <bean id="customerBean" class="com.java2s.core.Customer">
    <property name="warning" 
                  value="#{itemBean.qtyOnHand &lt; 100 ? true : false}" />
  </bean>
</beans>

Spring EL regular expression

The following Java bean has an email field which will be validated by using regular expression.

package com.java2s.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("emailBean")
public class Email {
  @Value("your email here")
  String emailAddress;
}

The following codes use regular expression to validate a number and stores the result in boolean value.

  // if this is a digit?
  @Value("#{'1' matches '\\d+' }")
  private boolean validDigit;

The following code uses the regular expression in tenary operator.

  @Value("#{ ('abc' matches '\\d+') == true ? " +
      "'yes this is digit' : 'No this is not a digit'  }")
  private String msg;

The following code uses regular expression to validate an email address from another Java bean.

  // email regular expression
  String emailRegEx = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)" +
      "*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

  @Value("#{emailBean.emailAddress matches customerBean.emailRegEx}")
  private boolean validEmail;

Use the same regular expression in xml

...
  <bean id="customerBean" class="com.java2s.core.Customer">
    <property name="validDigit" value="#{'1' matches '\d+' }" />
    <property name="msg"
    value="#{ ('abc' matches '\d+') == true ? 'yes this is digit' : 'No this is not a digit'  }" />
    <property name="validEmail"
    value="#{emailBean.emailAddress matches '^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$' }" />
  </bean>

  <bean id="emailBean" class="com.java2s.core.Email">
    <property name="emailAddress" value="your email" />
  </bean>

</beans>