How do I inject collections using set element in Spring?

In this example you will see how to use the <set> element to wire a collection property of a bean. We will reuse the bean that we use in the previous example How do I inject collections using list element in Spring?.

The Album bean have a songs property that have a type of java.util.List. The <set> element doesn’t have to be used with java.util.Set. It can be used to wire a java.util.List collection. It just mean the it cannot contains duplicate values in it, so the collection will only contains a unique values.

Here how we configure our Spring context:

<?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="song1" class="org.kodejava.example.spring.collections.Song">
        <property name="title" value="I Saw Her Standing There" />
        <property name="writer" value="Beatles" />
    </bean>

    <bean id="song2" class="org.kodejava.example.spring.collections.Song">
        <property name="title" value="Misery" />
        <property name="writer" value="Beatles" />
    </bean>

    <bean id="song3" class="org.kodejava.example.spring.collections.Song">
        <property name="title" value="Anna (Go to Him)" />
        <property name="writer" value="Beatles" />
    </bean>


    <bean id="album" class="org.kodejava.example.spring.collections.Album">
        <property name="title" value="Please Please Me"/>
        <property name="year" value="1963"/>
        <property name="songs">
            <set>
                <ref bean="song1"/>
                <ref bean="song1"/>
                <ref bean="song1"/>
            </set>
        </property>
    </bean>

</beans>

The <set> configuration can bee seen in the album bean configuration. We set the songs property. Within this property element we use the <set> element. And then using the <ref> element we add some bean into the collection.

Create the following code to run it:

package org.kodejava.example.spring.collections;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoSet {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext(
                        new String[] {"CollectionSet.xml"});

        Album album = (Album) context.getBean("album");
        System.out.println("Album = " + album);
    }
}

You’ll see the following output in the screen. As you can see, although we set three beans into the songs property, the Album bean only contain a single song. This is because we use the <set> element to wire the collection. It doesn’t allow duplicate values.

Album = Album{title='Please Please Me', year=1963, 
    songs=[Song{title='I Saw Her Standing There', writer='Beatles'}, null], 
    publisher={}, 
    props={}}
Wayan Saryada

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.
Wayan Saryada

Leave a Reply