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 collection-props.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="song1" class="org.kodejava.spring.core.Song">
        <property name="title" value="I Saw Her Standing There" />
        <property name="writer" value="Beatles" />
    </bean>

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

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

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

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

</beans>

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.spring.core;

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

public class DemoProps {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"collection-props.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, songs=[], publisher={}, props={color=Black, type=CD, duration=1 Hour}}

Maven Dependencies

Maven Dependencies

<dependencies>
    <!-- https://search.maven.org/remotecontent?filepath=org/springframework/spring-core/5.3.19/spring-core-5.3.19.jar -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.19</version>
    </dependency>
    <!-- https://search.maven.org/remotecontent?filepath=org/springframework/spring-beans/5.3.19/spring-beans-5.3.19.jar -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.19</version>
    </dependency>
    <!-- https://search.maven.org/remotecontent?filepath=org/springframework/spring-context-support/5.3.19/spring-context-support-5.3.19.jar -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.3.19</version>
    </dependency>
</dependencies>

Maven Central Maven Central Maven Central

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.