How do I inject collections using props element in Spring?

This time we will demonstrate how to inject a java.util.Properties. This class store a key-value pairs of data where the key and the values are both in string. You can use the <props> element to wire a property collections.

The bean we use in this example is taken from the previous example How do I inject collections using list element in Spring?.

Let’s create the configuration file and call it CollectionProps.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <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 id="song2" class="org.kodejava.example.spring.collections.Song">
        <property name="title" value="Misery" />
        <property name="writer" value="Beatles" />

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

    <bean id="publisher" class="org.kodejava.example.spring.collections.Publisher">
        <property name="name" value="EMI Studios"/>

    <bean id="album" class="org.kodejava.example.spring.collections.Album">
        <property name="title" value="Please Please Me"/>
        <property name="year" value="1963"/>
        <property name="props">
                <prop key="color">Black</prop>
                <prop key="type">CD</prop>
                <prop key="duration">1 Hour</prop>


To wire property collections we use the <props> element. This element can have many <prop> in it. The key of the property defined by the key attribute of this element. The value of the property is set in the body of this element.

As usual let’s create a simple program to run it:

package org.kodejava.example.spring.collections;

import org.springframework.context.ApplicationContext;

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

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

When you run it, it will produce the following output:

Album = Album{title='Please Please Me', year=1963, 
    props={color=Black, type=CD, duration=1 Hour}}
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