Spring Framework Constructor Injection with Collections Example
In this tutorial, we will modify our previous example and add a few collections so we can illustrate how we use constructor injection with differing collection types. Specifically, we will show how to perform constructor injection with List, Map and Set objects.
Client Class (Client.java)
package com.avaldes; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; public class Client { private int id; private String name; private String email; private boolean active; private ArrayList<String> children; private HashMap<String, String> phones; private HashSet<Integer> favoriteNumbers; public Client(int id, String name, String email, boolean active, ArrayList<String> children, HashMap<String, String> phones, HashSet<Integer> favoriteNumbers) { this.id = id; this.name = name; this.email = email; this.active = active; this.children = children; this.phones = phones; this.favoriteNumbers = favoriteNumbers; } @Override public String toString() { return "Client [id=" + id + ", name=" + name + ", email=" + email + ", active=" + active + ", children=" + children + ", phones=" + phones + ", favoriteNumbers=" + favoriteNumbers + "]"; } }
I have taken the liberty of removing all the getter and setter methods from the Client class and modified the constructor to allow us to pass in children, phones and favoriteNumbers (I know, its a stretch — humor me)…
Constructor Injection – Populating List Collection Elements
For the List collection, we use the constructor argument with name attribute of “children“, create the list node and add value nodes with each of Albert Einstein’s children.
<constructor-arg name="children"> <list> <value>Hans Albert Einstein</value> <value>Eduardo Einstein</value> </list> </constructor-arg>
Constructor Injection – Populating Map Collection Elements
Next, we will work on Map collection, which has key/value pairs. In this example, the collection is aptly named “phones“. We create the Map node, but will need to add entry key nodes with each key correctly labeled and the value for each phone in the corresponding value node.
<constructor-arg name="phones"> <map> <entry key="home"> <value>212-555-1212</value> </entry> <entry key="mobile"> <value>212-444-1313</value> </entry> <entry key="work"> <value>212-333-3456</value> </entry> </map> </constructor-arg>
Constructor Injection – Populating Set Collection Elements
In this last example, we are populating a Set which looks very similar to List, except that the node will correspond to set instead of list. The main differing quality between a Set and List is that sets do not contain duplicate elements.
<constructor-arg name="favoriteNumbers"> <set> <value>11</value> <value>17</value> <value>25</value> <value>39</value> </set> </constructor-arg>
Constructor Injection – Full (spring-config.xml)
<bean id="client6" class="com.avaldes.Client"> <constructor-arg name="email" value="albert.einstein@princeton.edu"> </constructor-arg> <constructor-arg name="active" value="false"></constructor-arg> <constructor-arg name="name" value="Albert Einstein"></constructor-arg> <constructor-arg name="id" type="int" value="789"></constructor-arg> <constructor-arg name="children"> <list> <value>Hans Albert Einstein</value> <value>Eduardo Einstein</value> </list> </constructor-arg> <constructor-arg name="phones"> <map> <entry key="home"> <value>212-555-1212</value> </entry> <entry key="mobile"> <value>212-444-1313</value> </entry> <entry key="work"> <value>212-333-3456</value> </entry> </map> </constructor-arg> <constructor-arg name="favoriteNumbers"> <set> <value>11</value> <value>17</value> <value>25</value> <value>39</value> </set> </constructor-arg> </bean>
Output – Working with different collections
Take a look at the completed object, and notice you the children, phones and favoriteNumbers are constructed and how the output looks.

Client [id=789, name=Albert Einstein, email=albert.einstein@princeton.edu, active=false, children=[Hans Albert Einstein, Eduardo Einstein], phones={home=212-555-1212, mobile=212-444-1313, work=212-333-3456}, favoriteNumbers=[11, 17, 25, 39]]
That’s It
As you can see from the above examples, adding collections as constructor arguments is not that difficult a task to perform. Enjoy Spring!
Related Posts
- Introduction to Spring Framework, IoC and Injection
In this tutorial we will cover a basic introduction the Spring framework, Inversion of Control and Dependency Injection. We will discuss the benefits of using it, and why it is so popular in today's environment. - Spring Framework Constructor Injection Example
In this tutorial, we will concentrate on Constructor Injection. As the name suggests, constructor injection is form of dependency injection that is accomplished when we supply the arguments to the constructor so that when the bean is instantiated by the factory it contains all of the necessary information to be property constructed. - Spring Framework Constructor Injection with Collections Example
In this tutorial, we will modify our previous example and add a few collections so we can illustrate how we use constructor injection with differing collection types. Specifically, we will show how to perform constructor injection with List, Map and Set objects. - Spring Framework Setter Injection with Examples In this tutorial we will discuss Spring Framework Setter Injection which is the main method of dependency injection in Spring. The property element is used to define the setter injection in Spring by using the name of the property element and looking for the corresponding setXXXXXX methods in the specified bean.
- Spring Bean Scopes Example
In this tutorial we will begin covering scope of Spring beans. In Spring when you define a bean in the Spring configuration file, you are telling Spring about its scope whether you define it or not. This is a powerful and flexible approach because you can choose the scope of the objects you create via configuration instead of having to hardcode the scope of an object at the Java class level. - Spring Bean Life Cycle Example – @PostConstruct, @PreDestroy, InitializingBean, DisposableBean, init-method and destroy-method
In this tutorial we will cover the Spring Bean Life Cycle and the methods, attributes and annotations used to hook into the management of the bean life cycle in the IoC container. - Integrating Java JMX Managed Beans using Spring and Annotations
This post will show how to create managed beans using the JMX standard using Spring. JMX stands for Java Management Extensions, and was created to implement a standard and uniform way to manage your applications. By using JMX remote can connection to a JVM and monitor running applications on that JVM. In addition to monitoring, remote clients are able to directly run methods and modify runtime parameters of the application that is being managed. - Integrating Spring Message Driven POJO with ActiveMQ
This post will show how to create a message driven POJO that can send and receive JMS message using ActiveMQ as the Message Oriented Middleware (MOM). We will discuss various approaches to creating the MDPs (Message Driven POJOs) in this tutorial and discuss setting up ActiveMQ from Apache.
Please Share Us on Social Media






Leave a Reply