Spring EL Equal and Not Equal Operators Example

0

Spring EL supports most of the relational operators. In this article, we will look into the examples of equal and not equal operators. The operators can be specified using standard operator notation ==, != as well as alphabetic equivalent eq and ne. Note that the alphabetic symbols are case insensitive so eq and eQ both are same. Why do we need an alphabetic notation? This is an nice alternative in cases where the symbols used have special meaning for the document type in which the expression is embedded.

The equal/not equal symbols are:

  1. ==
  2. eq
  3. !=
  4. ne

This example uses the following frameworks:

  1. Maven 3.2.3
  2. Spring 4.1.5.RELEASE
  3. Eclipse  as the IDE, version Luna 4.4.1.

Dependencies

Add the following dependencies:

  1. spring-core
  2. spring-context
  3. spring-beans

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javarticles.spring</groupId>
	<artifactId>springListExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>

	<properties>
		<spring.version>4.1.6.RELEASE</spring.version>
	</properties>

</project>

Spring EL equal operator using Annotation

This example demonstrates the use of equal operators in SpEL.

EqualOperatorBean:

package com.javarticles.spring;

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

@Component
public class EqualOperatorBean {

    @Value("#{9 == 6}")
    private boolean integerEqual;    
    
    @Value("#{9.0f == 6.0f}")
    private boolean floatEqual;    
    
    @Value("#{new java.lang.Double('9.0') == new java.lang.Double('6.0')}")
    private boolean objectsEqual;
    
    @Value("#{'hello' == null}")
    private boolean nullCheck;

    @Value("#{9.0f eq 9.0f}")
    private boolean floatEqualUsingEq;
    
    @Value("#{'hello' eq new java.lang.StringBuilder('hello')}")
    private boolean stringEqStringBuilder;
    
    @Value("#{new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello') == new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello')}")
    private boolean innerClassEqual;

    public boolean isIntegerEqual() {
        return integerEqual;
    }

    public boolean isFloatEqual() {
        return floatEqual;
    }

    public boolean isObjectsEqual() {
        return objectsEqual;
    }

    public boolean isNullCheck() {
        return nullCheck;
    }

    public boolean isFloatEqualUsingEq() {
        return floatEqualUsingEq;
    }

    public boolean isStringEqStringBuilder() {
        return stringEqStringBuilder;
    }           
    
    public boolean isInnerClassEqual() {
        return innerClassEqual;
    }

    private static class StringWrapper {
        private String value;
        public StringWrapper(String value) {
            this.value = value;
        }
        public String getValue() {
            return value;
        }
        public boolean equals(Object o) {
            if (o == null) {
                return false;
            }
            if (!(o instanceof StringWrapper)) {
                return false;
            }
            StringWrapper sw = (StringWrapper)o;
            return value.equals(sw.getValue());
        }
    }

    public void setIntegerEqual(boolean integerEqual) {
        this.integerEqual = integerEqual;
    }

    public void setFloatEqual(boolean floatEqual) {
        this.floatEqual = floatEqual;
    }

    public void setObjectsEqual(boolean objectsEqual) {
        this.objectsEqual = objectsEqual;
    }

    public void setNullCheck(boolean nullCheck) {
        this.nullCheck = nullCheck;
    }

    public void setFloatEqualUsingEq(boolean floatEqualUsingEq) {
        this.floatEqualUsingEq = floatEqualUsingEq;
    }

    public void setStringEqStringBuilder(boolean stringEqStringBuilder) {
        this.stringEqStringBuilder = stringEqStringBuilder;
    }

    public void setInnerClassEqual(boolean innerClassEqual) {
        this.innerClassEqual = innerClassEqual;
    }    
    
}

EqualOperatorsExample:

package com.javarticles.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;

public class EqualOperatorsExample {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            EqualOperatorBean equalOperatorBean = (EqualOperatorBean) context
                    .getBean("equalOperatorBean");

            System.out.println("#{9 == 6}->"
                    + equalOperatorBean.isIntegerEqual());

            System.out.println("#{9.0f == 6.0f}->"
                    + equalOperatorBean.isFloatEqual());

            System.out
                    .println("#{new java.lang.Double('9.0') == new java.lang.Double('6.0')}->"
                            + equalOperatorBean.isObjectsEqual());

            System.out.println("#{'hello' == null}->"
                    + equalOperatorBean.isNullCheck());

            System.out.println("#{9.0f eq 9.0f}->"
                    + equalOperatorBean.isFloatEqualUsingEq());

            System.out
                    .println("#{'hello' eq new java.lang.StringBuilder('hello')}->"
                            + equalOperatorBean.isStringEqStringBuilder());

            System.out
                    .println("#{new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello') eq new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello')}->"
                            + equalOperatorBean.isInnerClassEqual());

            SpelExpressionParser parser = new SpelExpressionParser();
            Expression exp = parser.parseExpression("9 == '6'");
            Boolean is9EqualsString6 = (Boolean) exp.getValue();
            System.out.println("#{9 == '6'}? " + is9EqualsString6);
        } finally {
            context.close();
        }
    }
}

Output:

#{9 == 6}->false
#{9.0f == 6.0f}->false
#{new java.lang.Double('9.0') == new java.lang.Double('6.0')}->false
#{'hello' == null}->false
#{9.0f eq 9.0f}->true
#{'hello' eq new java.lang.StringBuilder('hello')}->true
#{new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello') eq new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello')}->true
#{9 == '6'}? false

Spring EL not equal operator using Annotation

Now we see examples of not equal operators in SpEL.

NotEqualOperatorBean:

package com.javarticles.spring;

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

@Component
public class NotEqualOperatorBean {

    @Value("#{9 != 6}")
    private boolean integerNotEqual;

    @Value("#{9.0f != 9.0f}")
    private boolean floatNotEqual;    
    
    public boolean isFloatNotEqualUsingNEe() {
        return floatNotEqualUsingNEe;
    }

    @Value("#{new java.lang.Double('9.0') != new java.lang.Double('6.0')}")
    private boolean objectsNotEqual;
    
    @Value("#{'hello' != null}")
    private boolean notNullCheck;

    @Value("#{9.0f ne 9.0f}")
    private boolean floatNotEqualUsingNeq;
    
    @Value("#{9.0f Ne 9.0f}")
    private boolean floatNotEqualUsingNEe;

    public boolean isIntegerNotEqual() {
        return integerNotEqual;
    }

    public boolean isFloatNotEqual() {
        return floatNotEqual;
    }

    public boolean isObjectsNotEqual() {
        return objectsNotEqual;
    }

    public boolean isNotNullCheck() {
        return notNullCheck;
    }

    public boolean isFloatNotEqualUsingNeq() {
        return floatNotEqualUsingNeq;
    }
}

NotEqualOperatorsExample:

package com.javarticles.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;

public class NotEqualOperatorsExample {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            NotEqualOperatorBean notEqualOperatorBean = (NotEqualOperatorBean) context.getBean("notEqualOperatorBean");
 
            System.out.println("#{9 != 6}->" + notEqualOperatorBean.isIntegerNotEqual());
            
            System.out.println("#{9.0f != 9.0f}->" + notEqualOperatorBean.isFloatNotEqual());           
            
            System.out.println("#{new java.lang.Double('9.0') != new java.lang.Double('6.0')}" + notEqualOperatorBean.isObjectsNotEqual()); 
            
            System.out.println("'hello' != null" + notEqualOperatorBean.isNotNullCheck());
            
            System.out.println("#{9.0f neq 9.0f}" + notEqualOperatorBean.isFloatNotEqualUsingNeq());
            
            System.out.println("#{9.0f NEq 9.0f}" + notEqualOperatorBean.isFloatNotEqualUsingNEe()); 
            
            SpelExpressionParser parser = new SpelExpressionParser();
            Expression exp = parser.parseExpression("9 != '6'");
            Boolean is9EqualsString6 = (Boolean) exp.getValue();
            System.out.println("#{9 != '6'}? " + is9EqualsString6);
        } finally {
            context.close();
        }
    }
}

Output:

#{9 != 6}->true
#{9.0f != 9.0f}->false
#{new java.lang.Double('9.0') != new java.lang.Double('6.0')}true
'hello' != nulltrue
#{9.0f neq 9.0f}false
#{9.0f NEq 9.0f}false
#{9 != '6'}? true

Spring EL Equal Operator Expressions in XML

The above annotated expressions can also be configured in spring context XML. We have done it for the equal operator.

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:component-scan base-package="com.javarticles.spring" />

	<bean id="eqOprBean" class="com.javarticles.spring.EqualOperatorBean">

		<property name="integerEqual" value="#{9 == 6}"></property>
		<property name="floatEqual" value="#{9 == 6}"></property>
		<property name="objectsEqual" value="#{9 == 6}"></property>
		<property name="nullCheck" value="#{9 == 6}"></property>
		<property name="floatEqualUsingEq" value="#{9 == 6}"></property>
		<property name="stringEqStringBuilder" value="#{9 == 6}"></property>
		<property name="innerClassEqual" value="#{9 == 6}"></property>
	</bean>

</beans>

EqualOperatorsXmlContextExample:

package com.javarticles.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;

public class EqualOperatorsXmlContextExample {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            EqualOperatorBean equalOperatorBean = (EqualOperatorBean) context
                    .getBean("eqOprBean");

            System.out.println("#{9 == 6}->"
                    + equalOperatorBean.isIntegerEqual());

            System.out.println("#{9.0f == 6.0f}->"
                    + equalOperatorBean.isFloatEqual());

            System.out
                    .println("#{new java.lang.Double('9.0') == new java.lang.Double('6.0')}->"
                            + equalOperatorBean.isObjectsEqual());

            System.out.println("#{'hello' == null}->"
                    + equalOperatorBean.isNullCheck());

            System.out.println("#{9.0f eq 9.0f}->"
                    + equalOperatorBean.isFloatEqualUsingEq());

            System.out
                    .println("#{'hello' eq new java.lang.StringBuilder('hello')}->"
                            + equalOperatorBean.isStringEqStringBuilder());

            System.out
                    .println("#{new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello') eq new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello')}->"
                            + equalOperatorBean.isInnerClassEqual());
        } finally {
            context.close();
        }
    }
}

Output:

#{9 == 6}->false
#{9.0f == 6.0f}->false
#{new java.lang.Double('9.0') == new java.lang.Double('6.0')}->false
#{'hello' == null}->false
#{9.0f eq 9.0f}->false
#{'hello' eq new java.lang.StringBuilder('hello')}->false
#{new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello') eq new com.javarticles.spring.EqualOperatorBean$StringWrapper('hello')}->false

Download the source code

This was an example out Spring EL equal and not equal operators.

You can download the source code here: springELEqualOperatorsExample.zip
Share.

Comments are closed.