Injecting Collections via Spring XML Configuration

Defining a Bean with Primitive Collection Fields

To inject collections of primitive types, first define a class containing the necessary data structures.


package com.example.model;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PrimitiveCollectionHolder {
    private Long identifier;
    private List<String> stringList;
    private Map<String, String> stringMap;
    private Properties configProps;
    private Set<String> stringSet;
    private String[] stringArray;

    // Standard getters and setters omitted for brevity
}

XML Configuration for Primitive Collections

Spring provides dedicated XML tags for each collection type within a <property> element.


<bean id="primitiveHolderBean" class="com.example.model.PrimitiveCollectionHolder">
    <property name="identifier" value="100" />
    <property name="stringList">
        <list>
            <value>item-alpha</value>
            <value>item-beta</value>
            <value>item-gamma</value>
        </list>
    </property>
    <property name="stringMap">
        <map>
            <entry key="dict-key-1" value="dict-val-1"/>
            <entry key="dict-key-2" value="dict-val-2"/>
            <entry key="dict-key-3" value="dict-val-3"/>
        </map>
    </property>
    <property name="configProps">
        <props>
            <prop key="cfg.key.1">cfg.val.1</prop>
            <prop key="cfg.key.2">cfg.val.2</prop>
        </props>
    </property>
    <property name="stringSet">
        <set>
            <value>set-item-1</value>
            <value>set-item-2</value>
        </set>
    </property>
    <property name="stringArray">
        <array>
            <value>arr-element-1</value>
            <value>arr-element-2</value>
        </array>
    </property>
</bean>

Defining Beans for Object Collection Injection

When injecting collections of custom objects, define the individual entity classes first.


package com.example.model;

public class Department {
    private Long deptId;
    private String deptName;
    private String description;

    public Department() {}

    public Department(Long deptId, String deptName, String description) {
        this.deptId = deptId;
        this.deptName = deptName;
        this.description = description;
    }
    // Standard getters and setters
}

public class Employee {
    private Long empId;
    private String empName;
    // Standard getters and setters
}

Next, create the container class that holds the collections of these entities.


package com.example.model;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class OrganizationStructure {
    private Long orgId;
    private List<Department> departments;
    private Map<Department, Employee> assignments;
    private Set<Department> uniqueDepartments;
    // Standard getters and setters
}

XML Configuration for Object Collections

To populate collections with bean references, use the <ref> tag inside <list> and <set>, and the key-ref/value-ref attributes inside <map>.


<bean id="deptAlpha" class="com.example.model.Department">
    <property name="deptId" value="1"></property>
    <property name="deptName" value="Engineering"></property>
    <property name="description" value="Product development"></property>
</bean>

<bean id="deptBeta" class="com.example.model.Department">
    <property name="deptId" value="2"></property>
    <property name="deptName" value="Marketing"></property>
    <property name="description" value="Brand promotion"></property>
</bean>

<bean id="empAlpha" class="com.example.model.Employee">
    <property name="empId" value="101"></property>
    <property name="empName" value="Alice"></property>
</bean>

<bean id="empBeta" class="com.example.model.Employee">
    <property name="empId" value="102"></property>
    <property name="empName" value="Bob"></property>
</bean>

<bean id="orgStructureBean" class="com.example.model.OrganizationStructure">
    <property name="orgId" value="9001"></property>
    <property name="departments">
        <list>
            <ref bean="deptAlpha"/>
            <ref bean="deptBeta"/>
        </list>
    </property>
    <property name="assignments">
        <map>
            <entry key-ref="deptAlpha" value-ref="empAlpha"></entry>
            <entry key-ref="deptBeta" value-ref="empBeta"></entry>
        </map>
    </property>
    <property name="uniqueDepartments">
        <set>
            <ref bean="deptAlpha"/>
            <ref bean="deptBeta"/>
        </set>
    </property>
</bean>

Tags: Spring XML Configuration Dependency Injection java

Posted on Wed, 14 Oct 2026 16:49:21 +0000 by whitmard