Spring Setter Injection with Collection

We can inject collection values by setter method in spring framework. There can be used three elements inside the property element.

It can be:

  • list
  • set
  • map

In this example, we are using list that can have duplicate elements, set that have only unique elements and map that have both key and value.

CollectionEx.java

This class contains List,Set,Map and Properties with setters and getters method that prints the information.

applicationContext.xml

Following is the configuration file applicationContext.xml which has configuration for all the type of collections.

TestApp.java

This class gets the bean from the applicationContext.xml file.

Output

List Elements :[INDIA, Australia, USA, USA] Set Elements :[INDIA, Australia, USA] Map Elements :{1=INDIA, 2=Australia, 3=USA, 4=USA} Properties Elements :{two=Australia, one=INDIA, three=USA, four=USA}
It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

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