Cover Image for Spring Setter Injection with Collection 2
181 views

Spring Setter Injection with Collection 2

The Spring, you can perform setter injection with collections for various types of collections like lists, sets, and maps. Here, I’ll provide examples for each type of collection using setter injection.

1. Setter Injection with a List:

Let’s start with setter injection of a list of objects into a Spring bean. Suppose you have a Person class as before and a Team class that contains a list of Person objects:

public class Person {
    private String name;
    // Getter and setter methods for 'name'
}

public class Team {
    private List<Person> members;
    // Getter and setter methods for 'members'
}

Configure these beans in your Spring configuration file (applicationContext.xml):

<bean id="person1" class="com.example.Person">
    <property name="name" value="John" />
</bean>

<bean id="person2" class="com.example.Person">
    <property name="name" value="Alice" />
</bean>

<bean id="team" class="com.example.Team">
    <!-- Setter injection with a list -->
    <property name="members">
        <list>
            <ref bean="person1" />
            <ref bean="person2" />
        </list>
    </property>
</bean>

You define two Person beans and inject them into the members property of the Team bean as a list.

2. Setter Injection with a Set:

Now, let’s see how to perform setter injection with a set of objects into a Spring bean. You can modify the Team class to use a Set for its members:

import java.util.Set;

public class Team {
    private Set<Person> members;
    // Getter and setter methods for 'members'
}

In the Spring configuration file, you can define the members property as a set:

<bean id="team" class="com.example.Team">
    <!-- Setter injection with a set -->
    <property name="members">
        <set>
            <ref bean="person1" />
            <ref bean="person2" />
        </set>
    </property>
</bean>

This configures the members property of the Team bean as a set.

3. Setter Injection with a Map:

Now, let’s see how to perform setter injection with a map of objects into a Spring bean. Modify the Team class to use a Map where the keys are team member roles (e.g., “developer,” “designer”):

import java.util.Map;

public class Team {
    private Map<String, Person> members;
    // Getter and setter methods for 'members'
}

In the Spring configuration file, you can define the members property as a map:

<bean id="team" class="com.example.Team">
    <!-- Setter injection with a map -->
    <property name="members">
        <map>
            <entry key="developer" value-ref="person1" />
            <entry key="designer" value-ref="person2" />
        </map>
    </property>
</bean>

This configures the members property of the Team bean as a map where each entry represents a team member’s role and the corresponding Person bean reference.

You can use setter injection with different types of collections (lists, sets, maps) depending on your requirements, and it allows you to manage dependencies and relationships between beans effectively in your Spring application.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS