Inject collection values by constructor

We can inject collection values by constructor in spring framework. There can be used three elements inside the constructor-arg element.

It can be:

  • list
  • set
  • map

Each collection can have string based and non-string based values.Consider the example where one Person has multiple Hobbies.

In this example, we are using list that can have duplicate elements, you may use set that have only unique elements. But, you need to change list to set in the applicationContext.xml file and List to Set in the file.

This class contains three properties, constructor and display() method that prints the information. Here, we are using List to contain the multiple answers.


The list element of constructor-arg is used here to define the list.

This class gets the bean from the applicationContext.xml file and calls the display() method.


ID:10 Name:John Question:What are your hobbies? Hobbies are:Watching and playing Cricket,Reading Novel Book,Volunteering and Community Participation

Leave a Reply

Your email address will not be published. Required fields are marked *