Cover Image for Spring Constructor Injection with Map
114 views

Spring Constructor Injection with Map

The Spring, you can use constructor injection to inject a Map into a bean’s constructor. This is useful when you want to provide a mapping of key-value pairs as dependencies to a bean. Here’s how to perform constructor injection with a Map in Spring:

1. Create Dependent Objects:

First, create the dependent objects (beans) that you want to inject into another bean’s constructor. These objects should be defined as separate Spring beans. For this example, let’s create a Person class:

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    // Getter and setter for 'name'
}

2. Configure Beans in XML:

In your Spring configuration file (e.g., applicationContext.xml), define the beans and specify the constructor injection by providing the constructor arguments as a map. Use the <map> element to define the map of dependencies. In this example, we’ll use a <map> to inject a map of Person objects into another bean’s constructor:

<bean id="john" class="com.example.Person">
    <constructor-arg value="John" />
</bean>

<bean id="alice" class="com.example.Person">
    <constructor-arg value="Alice" />
</bean>

<bean id="team" class="com.example.Team">
    <constructor-arg>
        <map>
            <entry key="leader" value-ref="john" />
            <entry key="member" value-ref="alice" />
        </map>
    </constructor-arg>
</bean>

In this XML configuration:

  • We define “john” and “alice” beans of class com.example.Person with different names.
  • We define a “team” bean of class com.example.Team and inject a map of “leader” and “member” entries, where the keys are strings, and the values are references to “john” and “alice” beans.

3. Create the Bean Receiving the Map:

Now, create the bean that will receive the map as a constructor argument. In this example, we’ll create a Team class that receives a map of Person objects:

import java.util.Map;

public class Team {
    private Map<String, Person> members;

    public Team(Map<String, Person> members) {
        this.members = members;
    }

    // Getter and setter for 'members'
}

4. Access the Target Bean:

You can access the target bean, “team,” by retrieving it from the Spring application context. The map of “leader” and “member” entries will be injected into the Team bean’s constructor when it is created.

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Team team = context.getBean("team", Team.class);

// Use the 'team' bean, which now has a map of 'Person' members

Constructor injection with a Map allows you to pass a set of key-value pairs to a bean’s constructor, making it suitable for scenarios where you need to configure a bean with a collection of related dependencies, each associated with a unique key.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS