Pages Navigation Menu

Coding is much easier than you think

Accessing Map and List attributes in spEL

Accessing Map and List attributes in spEL


Step 1 :Create the POJO


The bean is defined using @Component annotation. The values of the Map and List are set in the constructor.

File :

package com.simpleCodeStuffs.spEL;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;

public class Bean {

	private Map<String, String> name;

	private List<Integer> price;

	public Bean() {

		name = new HashMap<String, String>();
		name.put("a", "Sandy");
		name.put("b", "Poorni");
		name.put("c", "Gokul");
		price = new ArrayList<Integer>();

	public Map<String, String> getName() {
		return name;

	public void setName(Map<String, String> name) { = name;

	public List<Integer> getPrice() {
		return price;

	public void setPrice(List<Integer> price) {
		this.price = price;


Notice how values are injected using spEL in the below class

File :

package com.simpleCodeStuffs.spEL;

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

public class Customer {

	private String name;

	private double bill;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public double getBill() {
		return bill;

	public void setBill(double bill) {
		this.bill = bill;


In case of MAP {#beanID.<propertyName>[‘MapIndex’]} In case of List {#beanID.<propertyName>[indexValue]}


Step 2 : Create the main class

File :

package com.simpleCodeStuffs.spEL;

import org.springframework.context.ApplicationContext;

public class MainClass {

	public static void main(String[] args) {

		ApplicationContext context = 
			  new ClassPathXmlApplicationContext("elBeans.xml");
		Customer customer = (Customer) context.getBean("cust");
		System.out.println("Customer name: " + customer.getName());
		System.out.println("Bill price : " + customer.getBill());


Step 3 :

Note that the values are accessed using annotations in this example. So, do not provide values in the xml as well, as the values provided in xml usually override the values provided through annotations.


File : elBeans.xml

<beans xmlns=""
	xmlns:xsi="" xmlns:context=""

	<context:component-scan base-package="com.simpleCodeStuffs.spEL" />
	<bean id="cust" class="com.simpleCodeStuffs.spEL.Customer">


Step 4 : Run the program. The output is





About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.