Advertisement



< Prev
Next >



SpEL with a Map Collection



In this tutorial, we will explain how to access a collection of objects stored in Map Collection, using the Spring Expression Language(SpEL) of Spring Framework. Before that we are also going to explain how to configure a collection of objects stored within a Map collection using the <map> tag of Spring Framework within its configuration XML file.






Creating the Java class - Company


We are going to create a java class named Company within the decodejava package and this class contains a String property named name and a Map collection consisting of multiple <key-value> pair of an Integer value(Employee ID) as a Key and its associated Employee object as its value.

Besides this, we are also going to define a couple of getter and setter methods within this class to set the Map collection and the String property.


package decodejava;

import java.util.Map;

public class Company 
{
 public String name;
 public Map<Integer,Employee> employees;
 
 
public String getName() 
{
	return name;
}
public void setName(String name) 
{
	this.name = name;
}


public Map<Integer,Employee> getEmployees()
{
	return employees;
}

public void setEmployees(Map<Integer,Employee> employees)
{
	this.employees = employees;
}

}





Creating a Java Class - Employee


Next, we are going to add another Java class named Employee and it is going to contain a String property and a primitive int property.

The properties will be assigned a value by the Spring Container, when the Employee bean is created by it using the configuration xml file(to be created in the upcoming section).


package decodejava;

public class Employee 
{
	public String name;
	public int telNo;
	
	
	public String getName() 
	{
		return name;
	}
	
	
	public void setName(String name) 
	{
		this.name = name;
	}
	
	
	public int getTelNo() 
	{
		return telNo;
	}
	
	
	public void setTelNo(int telNo) 
	{
		this.telNo = telNo;
	}
	
}



Advertisement




Adding the Utility class that calls the Spring API


Next, we are going to create another class named - Utility, which is a simple java class.

Utility.java
package decodejava;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class Utility {

	public static void main(String[] args) 
	{
		ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
		Company company = context.getBean(Company.class);
		
		
		StandardEvaluationContext  stContext  = new StandardEvaluationContext(company);
		SpelExpressionParser parser = new SpelExpressionParser();
		
		Map<Integer,Employee> map = (Map<Integer,Employee>)parser.parseRaw("employees.?[key == 2]").getValue(stContext);
		
		Set<Map.Entry<Integer,Employee>> set = map.entrySet();
		
		for(Map.Entry<Integer,Employee> employee : set)
		{
			System.out.println("Employee ID : " + employee.getKey());
			System.out.println("Employee Name : " + employee.getValue().getName());
			System.out.println("Employee Tel No : " + employee.getValue().getTelNo());	
		}
			
	}
}





The Utility class uses the ApplicationContext container(an interface) of Spring Framework by creating its instance using its implemented class FileSystemXmlApplicationContext, which loads the configuration xml file - config.beans.xml and does the following -


The Utility class uses the ApplicationContext container(an interface) of Spring Framework by creating its instance using its implemented class FileSystemXmlApplicationContext, which loads the configuration xml file - config.beans.xml and does the following -






Adding a configuration file


Next, we are going to add a configuration file to our project. This configuration document is an Extensible Markup Language(XML) file, ending with .xml extension and we are going to name file as config.beans.xml.

In this file, we have configured a Company bean with a unique id and it contains a collection of Employee beans.


config.beans.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:util="springframework.org/schema/util"
       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
       https://www.springframework.org/context/spring-context.xsd
       ">
           
<bean id="CompanyBean" class="decodejava.Company">
<property name="employees">
	<map>
			<entry key="1" value-ref="Employee1"></entry>
			<entry key="2" value-ref="Employee2"></entry>
			<entry key="3" value-ref="Employee3"></entry>
	</map>
</property>
</bean>

<bean class="decodejava.Employee" id="Employee1">
			<property name="name" value="Emp1"></property>
			<property name="telNo" value="111111"></property>
</bean>

<bean class="decodejava.Employee" id="Employee2">
			<property name="name" value="Emp2"></property>
			<property name="telNo" value="222222"></property>
</bean>

<bean class="decodejava.Employee" id="Employee3">
			<property name="name" value="Emp3"></property>
			<property name="telNo" value="333333"></property>
</bean>

</beans>



This mapping document has a parent <beans> tag as the root element and its individual child elements, each with a <bean> tag, containing all the attributes such as -


Advertisement




Adding JARs


  • We are going to add some JARs files to our Java project. These JARs are required in order to successfully execute a Spring project.

    All these JARs are included in the folder named libs(within our Spring installation folder). So, we need to add all the JARs in the libs folder to our build path of our Java project.

    Note : Those who don't know how to add JARs to the build path of your Java project in Eclipse IDE, please refer to our section Adding JARs to your Spring project in Eclipse.






Directory Structure of Project




The picture above depicts how and where to arrange classes and interfaces comprising this Spring Project, in a specific directory structure.

Project Folder - SpELWithMap is the name of our Project and it is a top-level directory.






Execution


Finally, after executing Utility class, you will get the following output within the Console window. This output shown below, shows how the Utility class has used the ApplicationContext container of Spring Framework to load the configuration xml file - config.beans.xml, access the beans specified in it, instantiate the Company class and Employee class, by calling their respective setter methods.

This class has used StandardEvaluationContext class to evaluate the object of Company class and have used the SpelExpressionParse class to parse a String expression which returns a Map object containing the Employee object with their key value equals to "2" and have finally called the methods getName() and getTelNo() on these objects.

Aug 21, 2018 5:35:42 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@626b2d4a: startup date [Tue Aug 21 17:35:42 2018]; root of context hierarchy
Aug 21, 2018 5:35:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
Employee ID : 2
Employee Name : Emp2
Employee Tel No : 222222


This concludes configuring a Map collection with the Spring Framework and access it using Spring Expression Language(SpEL). In the next article, we are going to show you how to access a Arrays collection using Spring Expression Language(SpEL)




Please share this article -




< Prev
Next >
< SpEL with Array
SpEL with XML >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement