How do I access collections members using Spring EL?

In another posts about Spring EL examples you have learn the basic of Spring EL. In this post you will see another powerful examples of Spring EL. We are going to demonstrate how to use Spring EL to access collection members.

By using Spring EL you can select a single reference member of a collection. You can also select members of collection based on the values of their properties. Another thing you can do is extract properties out of the collection members to create another collection object.

To demonstrate this we are going to create a simple bean / pojo as our collection object. We will create a Book class with some properties (id, title, author).

package org.kodejava.example.spring.model;

public class Book {
    private Long id;
    private String title;
    private String author;
    private String type;

    // Getters & Setters

Next, we need to create the spring configuration file. In this configuration we will create a collection of books using the <util:list> element. And we also create a bean with its properties is obtained from one of the collection objects.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <util:list id="books">
        <bean class="org.kodejava.example.spring.model.Book"
                p:title="Essential C# 4.0" p:author="Michaelis"/>
        <bean class="org.kodejava.example.spring.model.Book"
              p:title="User Stories Applied" p:author="Mike Cohen"/>
        <bean class="org.kodejava.example.spring.model.Book"
              p:title="Learning Android" p:author="Marco Gargenta"/>
        <bean class="org.kodejava.example.spring.model.Book"
              p:title="The Ruby Programming Language"
              p:author="David Flanagan &amp; Yukihiro Matsumoto"/>
        <bean class="org.kodejava.example.spring.model.Book"
              p:title="Einstein" p:author="Walter Isaacson"/>

    <bean id="book" class="org.kodejava.example.spring.model.Book">
        <property name="title" value="#{books[1].title}"/>
        <property name="author" value="#{books[1].author}"/>

In the configuration above you have seen how we set the title and author of the book bean. We use the square-braces operator ([]) to access collection’s member by their index. It’s look like this:

<property name="title" value="#{books[1].title}"/>
<property name="author" value="#{books[1].author}"/>

Which can be read as: please give me the collection object at index number 1 and take the value of its title and author to be assigned to the book bean. And as you might already know that a collection object in Java is always zero-based index. So this will give us the book with title User Stories Applied.

And finally let’s create an example class that run our spring configuration above. It’s simply load the spell-collection.xml configuration we create above. Get a bean from the loaded ApplicationContext and print out its properties, title and author properties.

package org.kodejava.example.spring;

import org.kodejava.example.spring.model.Book;
import org.springframework.context.ApplicationContext;

public class SpELCollectionExample {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spel-collection.xml");

        Book book = (Book) context.getBean("book");
        System.out.println("book.getTitle() = " + book.getTitle());
        System.out.println("book.getAuthor() = " + book.getAuthor());

Executing the code above will give you the following result:

INFO: Loading XML bean definitions from class path resource [spel-collection.xml]
book.getTitle() = User Stories Applied
book.getAuthor() = Mike Cohen

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.

Leave a Reply