Arithmethic Operations using Spring Expression Language

SpEL supports all of the basic arithmetic operators that Java supports, plus the carat (^) operator for performing a power of operation.

Following example demonstrates arithmetic operations using Spring Expression Language.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency> 
  </dependencies>

1. POJO

Create following classes

package com.kruders.bean;

public class Numbers {
	private Integer numberA;
	private Integer numberB;
	
	public Integer getNumberA() {
		return numberA;
	}
	public void setNumberA(Integer numberA) {
		this.numberA = numberA;
	}
	public Integer getNumberB() {
		return numberB;
	}
	public void setNumberB(Integer numberB) {
		this.numberB = numberB;
	}
}
package com.kruders.bean;

public class Operation {
	private Integer addResult;
	private Integer subtractResult;
	private Integer multiplyResult;
	private Integer divideResult;
	private Integer powerResult;
	private Integer modulusResult;
	
	public Integer getAddResult() {
		return addResult;
	}
	public void setAddResult(Integer addResult) {
		this.addResult = addResult;
	}
	public Integer getSubtractResult() {
		return subtractResult;
	}
	public void setSubtractResult(Integer subtractResult) {
		this.subtractResult = subtractResult;
	}
	public Integer getMultiplyResult() {
		return multiplyResult;
	}
	public void setMultiplyResult(Integer multiplyResult) {
		this.multiplyResult = multiplyResult;
	}
	public Integer getDivideResult() {
		return divideResult;
	}
	public void setDivideResult(Integer divideResult) {
		this.divideResult = divideResult;
	}
	public Integer getPowerResult() {
		return powerResult;
	}
	public void setPowerResult(Integer powerResult) {
		this.powerResult = powerResult;
	}
	public Integer getModulusResult() {
		return modulusResult;
	}
	public void setModulusResult(Integer modulusResult) {
		this.modulusResult = modulusResult;
	}
}

2. Configuration

Create Spring-Bean.xml and write the following code.

<?xml version="1.0" encoding="UTF-8"?>
<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="number" class="com.kruders.bean.Numbers">
        <property name="numberA" value="10" />
        <property name="numberB" value="5" />
    </bean>
    
    <bean id="operation" class="com.kruders.bean.Operation">
        <property name="addResult" value="#{number.numberA + number.numberB}" />
        <property name="subtractResult" value="#{number.numberA - number.numberB}" />
        <property name="multiplyResult" value="#{number.numberA * number.numberB}" />
        <property name="divideResult" value="#{number.numberA / number.numberB}" />
        <property name="powerResult" value="#{number.numberA ^ 2}" />
        <property name="modulusResult" value="#{number.numberA % 3}" />
    </bean>
</beans>

3. Run Program

Create Main.java class and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.kruders.bean.Numbers;
import com.kruders.bean.Operation;
 
public class Main {
    public static void main(String[] args) {
    	ApplicationContext context = new ClassPathXmlApplicationContext(
                "Spring-Bean.xml");
    	Numbers arithmetic = (Numbers) context.getBean("number");
        System.out.println("Number A : " + arithmetic.getNumberA());
        System.out.println("Number B : " + arithmetic.getNumberB());
        Operation operation = (Operation) context.getBean("operation");
        System.out.println("\nOperations");
        System.out.println("Addition : " + operation.getAddResult());
        System.out.println("Subtraction : " + operation.getSubtractResult());
        System.out.println("Multiplication : " + operation.getMultiplyResult());
        System.out.println("Division : " + operation.getDivideResult());
        System.out.println("Power : " + operation.getPowerResult());
        System.out.println("Modulus (10 % 3) : " + operation.getModulusResult());
    }
}

When you run the above example you’ll get an output like:

Number A : 10
Number B : 5

Operations
Addition : 15
Subtraction : 5
Multiplication : 50
Division : 2
Power : 100

The folder structure of the example is shown below in Figure 60.1

Figure 60.1 Figure 60.1

You can download the source code of this example here.


, , ,

No comments yet.

Leave a Reply