Spring EL Mathematical Operators Example

0

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

The mathematical operators used are addition (+), Subtraction (-), Multiplication (*), division (/), modulus (%) and exponential power (^).

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

The addition and multiplication operator can be used on both numbers and strings.

#{'hello' * 2}

will result in

hellohello
#{'John' + 2}

will result in

John2

If subtraction is used on a character, then it will be converted into ASCII value and will result into a new character.
For example:

#{'J' - 2}"

results into ‘H’.

The division and modulus mathematical operators has an alphabetic equivalent ‘div’ and ‘mod’.
Standard operator precedence is enforced.

#{3 + 2 * 3 -1}

results into calculation of 2 * 3 first and then 3 + 6 -1 which is 8.

MathematicalOprBean:

package com.javarticles.spring;

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

@Component
public class MathematicalOprBean {
    
    @Value("#{'hello' * 2}")
    private String strMultiply;
    
    @Value("#{2.5d + 2}")
    private Double addDoubleValue;    
    
    @Value("#{2.5d - 2}")
    private Double substractDoubleValue;    
    
    @Value("#{2.5d * 2}")
    private Double multiplyDoubleValue;    
    
    @Value("#{2.5d / 2}")
    private Double divideDoubleValue;
    
    @Value("#{2.5d div 2}")
    private Double divideTextualDoubleValue;
    
    @Value("#{3 % 2}")
    private Double modulusOperandValue;
    
    @Value("#{3 Mod 2}")
    private Double modulusTextualOperandValue;
    
    @Value("#{3 + 2 * 3 -1}")
    private Double arthimeticValue;
    
    @Value("#{'John' + 2}")
    private String appendStrValue;
    
    @Value("#{'J' - 2}")
    private String substractStrValue;
    
    @Value("#{3^2}")
    private int powerValue;
    
    @Value("#{3 * new java.lang.Double('2')}")
    private Double multiplyDoubleObj;


    public String getStrMultiply() {
        return strMultiply;
    }

    public void setStrMultiply(String strMultiply) {
        this.strMultiply = strMultiply;
    }

    public Double getAddDoubleValue() {
        return addDoubleValue;
    }

    public void setAddDoubleValue(Double addDoubleValue) {
        this.addDoubleValue = addDoubleValue;
    }

    public Double getSubstractDoubleValue() {
        return substractDoubleValue;
    }

    public void setSubstractDoubleValue(Double substractDoubleValue) {
        this.substractDoubleValue = substractDoubleValue;
    }

    public Double getDivideDoubleValue() {
        return divideDoubleValue;
    }

    public void setDivideDoubleValue(Double divideDoubleValue) {
        this.divideDoubleValue = divideDoubleValue;
    }

    public Double getMultiplyDoubleValue() {
        return multiplyDoubleValue;
    }

    public void setMultiplyDoubleValue(Double multiplyDoubleValue) {
        this.multiplyDoubleValue = multiplyDoubleValue;
    }

    public Double getModulusOperandValue() {
        return modulusOperandValue;
    }

    public void setModulusOperandValue(Double modulusOperandValue) {
        this.modulusOperandValue = modulusOperandValue;
    }

    public Double getModulusTextualOperandValue() {
        return modulusTextualOperandValue;
    }

    public void setModulusTextualOperandValue(Double modulusTextualOperandValue) {
        this.modulusTextualOperandValue = modulusTextualOperandValue;
    }

    public Double getArthimeticValue() {
        return arthimeticValue;
    }

    public void setArthimeticValue(Double arthimeticValue) {
        this.arthimeticValue = arthimeticValue;
    }

    public String getAppendStrValue() {
        return appendStrValue;
    }

    public void setAppendStrValue(String appendStrValue) {
        this.appendStrValue = appendStrValue;
    }

    public String getSubstractStrValue() {
        return substractStrValue;
    }

    public void setSubstractStrValue(String substractStrValue) {
        this.substractStrValue = substractStrValue;
    }

    public int getPowerValue() {
        return powerValue;
    }

    public void setPowerValue(int powerValue) {
        this.powerValue = powerValue;
    }

    public Double getMultiplyDoubleObj() {
        return multiplyDoubleObj;
    }

    public void setMultiplyDoubleObj(Double multiplyDoubleObj) {
        this.multiplyDoubleObj = multiplyDoubleObj;
    }

    public Double getDivideTextualDoubleValue() {
        return divideTextualDoubleValue;
    }

    public void setDivideTextualDoubleValue(Double divideTextualDoubleValue) {
        this.divideTextualDoubleValue = divideTextualDoubleValue;
    } 
    
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("#{'John' + 2}->").append(getAppendStrValue()).append("\n")
        .append("#{#{3^2}}->").append(getPowerValue()).append("\n")
        .append("#{'hello' * 2}->").append(getStrMultiply()).append("\n")
        .append("#{'J' - 2}->").append(getSubstractStrValue()).append("\n")
        .append("#{2.5d + 2}->").append(getAddDoubleValue()).append("\n")
        .append("#{3 + 2 * 3 -1}->").append(getArthimeticValue()).append("\n")
        .append("#{2.5d / 2}->").append(getDivideDoubleValue()).append("\n")
        .append("#{2.5d div 2}->").append(getDivideTextualDoubleValue()).append("\n")
        .append("#{3 % 2}->").append(getModulusOperandValue()).append("\n")
        .append("#{3 Mod 2}->").append(getModulusTextualOperandValue()).append("\n")
        .append("#{2.5d * 2}->").append(getMultiplyDoubleValue()).append("\n")
        .append("#{3 * new java.lang.Double('2')}->").append(getMultiplyDoubleObj()).append("\n")
        .append("#{2.5d - 2}->").append(getSubstractDoubleValue()).append("\n");
        return sb.toString();
    }
        
}

MathematicalOprExpressionExample:

package com.javarticles.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MathematicalOprExpressionExample {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            System.out.println("Spring EL Mathematical Operators in @Value Annotation\n");
            MathematicalOprBean mathematicalOprBean = (MathematicalOprBean) context.getBean("mathematicalOprBean");
            System.out.println(mathematicalOprBean);
        } finally {
            context.close();
        }
    }
}

Output:

Spring EL Mathematical Operators in @Value Annotation

#{'John' + 2}->John2
#{#{3^2}}->9
#{'hello' * 2}->hellohello
#{'J' - 2}->H
#{2.5d + 2}->4.5
#{3 + 2 * 3 -1}->8.0
#{2.5d / 2}->1.25
#{2.5d div 2}->1.25
#{3 % 2}->1.0
#{3 Mod 2}->1.0
#{2.5d * 2}->5.0
#{3 * new java.lang.Double('2')}->6.0
#{2.5d - 2}->0.5

Spring EL Mathematical Operators in XML Context

In this section, you will see Spring EL mathematical expressions used in bean definition XML file.

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="myMathematicalOprBean" class="com.javarticles.spring.MathematicalOprBean">
		<property name="strMultiply" value="#{'hello' * 2}" />
		<property name="addDoubleValue" value="#{2.5d + 2}" />
		<property name="substractDoubleValue" value="#{2.5d - 2}" />
		<property name="multiplyDoubleValue" value="#{2.5d * 2}" />
		<property name="divideDoubleValue" value="#{2.5d / 2}" />
		<property name="divideTextualDoubleValue" value="#{2.5d div 2}" />
		<property name="modulusOperandValue" value="#{3 % 2}" />
		<property name="modulusTextualOperandValue" value="#{3 Mod 2}" />
		<property name="arthimeticValue" value="#{3 + 2 * 3 -1}" />
		<property name="appendStrValue" value="#{'John' + 2}" />
		<property name="substractStrValue" value="#{'J' - 2}" />
		<property name="powerValue" value="#{3^2}" />
		<property name="multiplyDoubleObj" value="#{3 * new java.lang.Double('2')}" />
	</bean>

</beans>

MathematicalOprExpressionExample:

package com.javarticles.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MathematicalOprExpressionExample {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            System.out.println("Spring EL Mathematical Operators in @Value Annotation\n");
            MathematicalOprBean mathematicalOprBean = (MathematicalOprBean) context.getBean("mathematicalOprBean");
            System.out.println(mathematicalOprBean);
            System.out.println("Spring EL Mathematical Operators in XML Context\n");
            System.out.println(context.getBean("myMathematicalOprBean"));
        } finally {
            context.close();
        }
    }
}

Output:

Spring EL Mathematical Operators in @Value Annotation

#{'John' + 2}->John2
#{#{3^2}}->9
#{'hello' * 2}->hellohello
#{'J' - 2}->H
#{2.5d + 2}->4.5
#{3 + 2 * 3 -1}->8.0
#{2.5d / 2}->1.25
#{2.5d div 2}->1.25
#{3 % 2}->1.0
#{3 Mod 2}->1.0
#{2.5d * 2}->5.0
#{3 * new java.lang.Double('2')}->6.0
#{2.5d - 2}->0.5

Spring EL Mathematical Operators in XML Context

#{'John' + 2}->John2
#{#{3^2}}->9
#{'hello' * 2}->hellohello
#{'J' - 2}->H
#{2.5d + 2}->4.5
#{3 + 2 * 3 -1}->8.0
#{2.5d / 2}->1.25
#{2.5d div 2}->1.25
#{3 % 2}->1.0
#{3 Mod 2}->1.0
#{2.5d * 2}->5.0
#{3 * new java.lang.Double('2')}->6.0
#{2.5d - 2}->0.5

Download the source code

This was an example about Spring EL Mathematical Operators.

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

Comments are closed.