Spring EL Relational Operators Example

0

In this article, we will see some examples of Spring EL relational operators.

The relational operators are equal, not equal, less than, less than or equal, greater than, and greater than or equal. They are supported using standard operator notation as well as alphabetic equivalent.

The relational operator notations are equal (==, eq), not equal (!=, ne), less than (<, lt), less than or equal (<= , le), greater than (>, gt), and greater than or equal (>=, ge).

If you want to see examples of Spring EL Equal and Not Equal Operators, click here.

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 Relational Operators in @Value Annotation

RelationalOprBean:

package com.javarticles.spring;

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

@Component
public class RelationalOprBean {
    @Value("#{5000 < 5000}")
    private boolean equalIntLessThan;
    
    @Value("#{3 < 5}")
    private boolean intLessThan;
    
    @Value("#{3.0d < 5.0d}")
    private boolean doubleLessThan;
    
    @Value("#{3L < 5L}")
    private boolean longLessThan;    
    
    @Value("#{new java.math.BigDecimal('3') < new java.math.BigDecimal('5')}")
    private boolean bigDecLessThan;
    
    @Value("#{3 < new java.math.BigDecimal('5')}")
    private boolean intLessThanBigDecimal;
    
    @Value("#{'hello' < 'there'}")
    private boolean strLessThan;
    
    @Value("#{3L lt 5L}")
    private boolean longTextualLessThan;
    
    @Value("#{3L gt 5L}")
    private boolean longTextualGreaterThan;
    
    @Value("#{3L <= 5L}")
    private boolean longLessThanEqualTo;
    
    @Value("#{3L >= 5L}")
    private boolean longGreaterThanEqualTo;
    
    @Value("#{3L le 5L}")
    private boolean longTextualLessThanEqualTo;
    
    @Value("#{3L ge 5L}")
    private boolean longTextualGreaterThanEqualTo;
    
    @Value("#{3.0d Ge new java.lang.Double('5')}")
    private boolean doubleTextualGreaterThanEqualTo;
    

    public boolean isEqualIntLessThan() {
        return equalIntLessThan;
    }

    public void setEqualIntLessThan(boolean equalIntLessThan) {
        this.equalIntLessThan = equalIntLessThan;
    }

    public boolean isIntLessThan() {
        return intLessThan;
    }

    public void setIntLessThan(boolean intLessThan) {
        this.intLessThan = intLessThan;
    }

    public boolean isDoubleLessThan() {
        return doubleLessThan;
    }

    public void setDoubleLessThan(boolean doubleLessThan) {
        this.doubleLessThan = doubleLessThan;
    }

    public boolean isLongLessThan() {
        return longLessThan;
    }

    public void setLongLessThan(boolean longLessThan) {
        this.longLessThan = longLessThan;
    }

    public boolean isBigDecLessThan() {
        return bigDecLessThan;
    }

    public void setBigDecLessThan(boolean bigDecLessThan) {
        this.bigDecLessThan = bigDecLessThan;
    }

    public boolean isIntLessThanBigDecimal() {
        return intLessThanBigDecimal;
    }

    public void setIntLessThanBigDecimal(boolean intLessThanBigDecimal) {
        this.intLessThanBigDecimal = intLessThanBigDecimal;
    }

    public boolean isStrLessThan() {
        return strLessThan;
    }

    public void setStrLessThan(boolean strLessThan) {
        this.strLessThan = strLessThan;
    }

    public boolean isLongTextualLessThan() {
        return longTextualLessThan;
    }

    public void setLongTextualLessThan(boolean longTextualLessThan) {
        this.longTextualLessThan = longTextualLessThan;
    }

    public boolean isLongTextualGreaterThan() {
        return longTextualGreaterThan;
    }

    public void setLongTextualGreaterThan(boolean longTextualGreaterThan) {
        this.longTextualGreaterThan = longTextualGreaterThan;
    }

    public boolean isLongLessThanEqualTo() {
        return longLessThanEqualTo;
    }

    public void setLongLessThanEqualTo(boolean longLessThanEqualTo) {
        this.longLessThanEqualTo = longLessThanEqualTo;
    }

    public boolean isLongGreaterThanEqualTo() {
        return longGreaterThanEqualTo;
    }

    public void setLongGreaterThanEqualTo(boolean longGreaterThanEqualTo) {
        this.longGreaterThanEqualTo = longGreaterThanEqualTo;
    }

    public boolean isLongTextualLessThanEqualTo() {
        return longTextualLessThanEqualTo;
    }

    public void setLongTextualLessThanEqualTo(boolean longTextualLessThanEqualTo) {
        this.longTextualLessThanEqualTo = longTextualLessThanEqualTo;
    }

    public boolean isLongTextualGreaterThanEqualTo() {
        return longTextualGreaterThanEqualTo;
    }

    public void setLongTextualGreaterThanEqualTo(
            boolean longTextualGreaterThanEqualTo) {
        this.longTextualGreaterThanEqualTo = longTextualGreaterThanEqualTo;
    }

    public boolean isDoubleTextualGreaterThanEqualTo() {
        return doubleTextualGreaterThanEqualTo;
    }

    public void setDoubleTextualGreaterThanEqualTo(
            boolean doubleTextualGreaterThanEqualTo) {
        this.doubleTextualGreaterThanEqualTo = doubleTextualGreaterThanEqualTo;
    }
    
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("#{5000 < 5000}->").append(isEqualIntLessThan()).append("\n")
        .append("#{3 < 5}->").append(isIntLessThan()).append("\n")
        .append("#{3.0d < 5.0d}->").append(isDoubleLessThan()).append("\n")
        .append("#{3L < 5L}->").append(isLongLessThan()).append("\n")
        .append("#{new java.math.BigDecimal('3') < new java.math.BigDecimal('5')}->").append(isBigDecLessThan()).append("\n")
        .append("#{3 < new java.math.BigDecimal('5')}->").append(isIntLessThanBigDecimal()).append("\n")
        .append("#{'hello' < 'there'}->").append(isStrLessThan()).append("\n")
        .append("#{3L lt 5L}->").append(isLongTextualLessThan()).append("\n")
        .append("#{3L gt 5L}->").append(isLongTextualGreaterThan()).append("\n")
        .append("#{3L <= 5L}->").append(isLongLessThanEqualTo()).append("\n")
        .append("#{3L >= 5L}->").append(isLongGreaterThanEqualTo()).append("\n")
        .append("#{3L le 5L}->").append(isLongTextualLessThanEqualTo()).append("\n")
        .append("#{3L ge 5L}->").append(isLongTextualGreaterThanEqualTo()).append("\n")
        .append("#{3.0d Ge new java.lang.Double('5')}->").append(isDoubleTextualGreaterThanEqualTo()).append("\n");
        return sb.toString();
    }
        
}

RelationalOprExpressionExample:

package com.javarticles.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RelationalOprExpressionExample {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            System.out.println("Spring EL using @Value Annotation\n");
            RelationalOprBean relationalOprBean = (RelationalOprBean) context.getBean("relationalOprBean");
            System.out.println(relationalOprBean);
        } finally {
            context.close();
        }
    }
}

Output:

Spring Relational Operator EL using @Value Annotation

#{5000 < 5000}->false
#{3 < 5}->true
#{3.0d < 5.0d}->true
#{3L < 5L}->true
#{new java.math.BigDecimal('3') < new java.math.BigDecimal('5')}->true
#{3 < new java.math.BigDecimal('5')}->true
#{'hello' < 'there'}->true
#{3L lt 5L}->true
#{3L gt 5L}->false
#{3L <= 5L}->true
#{3L >= 5L}->false
#{3L le 5L}->true
#{3L ge 5L}->false
#{3.0d Ge new java.lang.Double('5')}->false

Spring EL Relational Operators in XML Context

In XML, < is to be used in form of &lt;. Instead of the standard operator, one can use the alphabetic equivalents, for example, (‘<‘ = ‘lt’). In this example, we have configured the spring EL mathematical operators in both ways.

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="myRelationalOprBean" class="com.javarticles.spring.RelationalOprBean">
		<property name="equalIntLessThan" value="#{5000 < 5000}" />
		<property name="intLessThan" value="#{3 < 5}" />
		<property name="doubleLessThan" value="#{3.0d < 5.0d}" />
		<property name="longLessThan" value="#{3L < 5L}" />
		<property name="bigDecLessThan"
			value="#{new java.math.BigDecimal('3') < new java.math.BigDecimal('5')}" />
		<property name="intLessThanBigDecimal" value="#{3 < new java.math.BigDecimal('5')}" />
		<property name="strLessThan" value="#{'hello' < 'there'}" />
		<property name="longTextualLessThan" value="#{3L lt 5L}" />
		<property name="longTextualGreaterThan" value="#{3L gt 5L}" />
		<property name="longLessThanEqualTo" value="#{3L <= 5L}" />
		<property name="longGreaterThanEqualTo" value="#{3L >= 5L}" />
		<property name="longTextualLessThanEqualTo" value="#{3L le 5L}" />
		<property name="longTextualGreaterThanEqualTo" value="#{3L ge 5L}" />
		<property name="doubleTextualGreaterThanEqualTo" value="#{3.0d Ge new java.lang.Double('5')}" />
	</bean>

</beans>

RelationalOprExpressionExample:

package com.javarticles.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RelationalOprExpressionExample {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            System.out.println("Spring EL using @Value Annotation\n");
            RelationalOprBean relationalOprBean = (RelationalOprBean) context.getBean("relationalOprBean");
            System.out.println(relationalOprBean);
            System.out.println("Spring EL in XML Context\n");
            System.out.println(context.getBean("myRelationalOprBean"));
        } finally {
            context.close();
        }
    }
}

Output:

Spring Relational Operator EL using @Value Annotation

#{5000 < 5000}->false
#{3 < 5}->true
#{3.0d < 5.0d}->true
#{3L < 5L}->true
#{new java.math.BigDecimal('3') < new java.math.BigDecimal('5')}->true
#{3 < new java.math.BigDecimal('5')}->true
#{'hello' < 'there'}->true
#{3L lt 5L}->true
#{3L gt 5L}->false
#{3L <= 5L}->true
#{3L >= 5L}->false
#{3L le 5L}->true
#{3L ge 5L}->false
#{3.0d Ge new java.lang.Double('5')}->false

Spring Relational Operator EL in XML Context

#{5000 < 5000}->false
#{3 < 5}->true
#{3.0d < 5.0d}->true
#{3L < 5L}->true
#{new java.math.BigDecimal('3') < new java.math.BigDecimal('5')}->true
#{3 < new java.math.BigDecimal('5')}->true
#{'hello' < 'there'}->true
#{3L lt 5L}->true
#{3L gt 5L}->false
#{3L <= 5L}->true
#{3L >= 5L}->false
#{3L le 5L}->true
#{3L ge 5L}->false
#{3.0d Ge new java.lang.Double('5')}->false

Download source code

This was an example about Spring EL Relational Operators.

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

Comments are closed.