Operators in spEL

The various operators that are most frequently used in this context are :-
  • Relational operators
    1. equal (==)
    2. not equal (!=)
    3. less than (<)
    4. less than or equal (<=)
    5. greater than (>)
    6. greater than or equal (>=)
  • Logical operators
    1. And(&&)
    2. Or(||)
    3. not (!).
  • Mathematical operators
    1. addition (+)
    2. Subtraction (-)
    3. Multiplication (*)


    4. division (/)
    5. modulus (%)
    6. exponential power (^).
  • Ternary operator
    1. if-then-else (<condn> ? <stmt1 if true> : <Stmt2 if false>)
      .

Step 1 : Create a sample POJO


File : Operators.java
package com.simpleCodeStuffs.spEL;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("operators")
public class Operators {

        @Value("#{1==1}")
        private boolean equals;

        public boolean isEquals() {
                return equals;
        }

        public void setEquals(boolean equals) {
                this.equals = equals;
        }
}

Step 2 : Create the main class


File : MainClass.java
package com.simpleCodeStuffs.spEL;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {

        public static void main(String[] args) {

                ApplicationContext context =
                     new ClassPathXmlApplicationContext("elBeans.xml");
                Operators op = (Operators) context.getBean("operators");
                System.out.println("equals" + op.isEquals());
        }
}

Step 3 : Enable automatic component scanning in elBeans.xml


File : elBeans.xml
<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.simpleCodeStuffs.spEL" />
</beans>

Step 5 : Other operators and the functionalities


    @Value("#{1 != 5}") //true
        private boolean NotEqual;
 
        @Value("#{1 < 1}") //false
        private boolean LessThan;
 
        @Value("#{1 <= 1}") //true
        private boolean LessThanOrEqual;
 
        @Value("#{1 > 1}") //false
        private boolean GreaterThan;
 
        @Value("#{1 >= 1}") //true
        private boolean GreaterThanOrEqual;
 
        /*
         * Logical operators , numberBean.no == 999
         * @Value("#{numberBean.no == 999 and numberBean.no < 900}") 
         * can be used in case 'numberBean' is another bean.
         */
 
        @Value("#{999 == 999 and 999 < 900}") //false
        private boolean And;
 
        @Value("#{999 == 999 or 999 < 900}") //true
        private boolean Or;
 
        @Value("#{!(999 == 999)}") //false
        private boolean Not;
 
        //Mathematical operators
 
        @Value("#{1 + 1}") //2.0
        private double testAdd;
 
        @Value("#{'1' + '@' + '1'}") //1@1
        private String AddString;
 
        @Value("#{1 - 1}") //0.0
        private double Subtraction;
 
        @Value("#{1 * 1}") //1.0
        private double Multiplication;
 
        @Value("#{10 / 2}") //5.0
        private double Division;
 
        @Value("#{10 % 10}") //0.0
        private double Modulus ;
 
        @Value("#{2 ^ 2}") //4.0
        private double ExponentialPower;

//Ternary operator
@Value("#{99!=0?'expresion holds true':'expression is false'}") 
        private String name;
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

File : Operators.java

package com.simpleCodeStuffs.spEL;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("operators")
public class Operators {

        @Value("#{99!=0?'expresion holds true':'expression is false'}")
        private String name;

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }
}

File : MainClass.java

package com.simpleCodeStuffs.spEL;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {

        public static void main(String[] args) {

                ApplicationContext context = 
                       new ClassPathXmlApplicationContext("elBeans.xml");
                Operators op = (Operators) context.getBean("operators");
                System.out.println(op.getName());
        }
}