In the Spring Framework, Dependency Injection (DI) allows you to manage object dependencies cleanly. While injecting simple types is straightforward, real-world enterprise applications often require injecting collections of custom dependent objects—such as a Set<Address> or Set<Answer>—via constructor injection.
How Spring Handles a Set of Dependent Objects
Spring's IoC container uses the <constructor-arg> tag combined with the <set> and nested <ref bean="..."/> tags in the XML configuration. This ensures that unique instances of your custom bean are instantiated and passed directly into the target class constructor during bean creation.
Key Steps Covered in This Tutorial
Model Creation: Defining the dependent object class and the primary class with a constructor accepting a Set.
XML Bean Configuration: Configuring <constructor-arg> using <set> containing <ref> elements.
Application Context Loading: Initializing ClassPathXmlApplicationContext to fetch the configured bean.
Output Verification: Iterating through the injected Set to display the dependent object details.
publicclass Fruit { privateString name; privateString color;
public Fruit(String name, String color) { System.out.println("Fruit(String name, String color) "
+ "constructor is called by the Spring container"); this.name = name; this.color = color; }
@Override publicString toString() { return"Fruit [name=" + name + ", color=" + color + "]"; }
public FruitShop(String shopName, Set<Fruit> setOfAvailableFruits) { System.out.println( "FruitShop(String shopName, Set<Fruit> setOfAvailableFruits) "
+ "constructor is called by the Spring container"); this.shopName = shopName; this.setOfAvailableFruits = setOfAvailableFruits; }
FruitShop fruitShop = (FruitShop) context.getBean("fruitShop"); System.out.println( "Got fruitShop object from the ApplicationContext(Spring Container)");
fruitShop.displayFruitShopInfo();
}
}
Output
Apr 27, 2018 9:28:32 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b37e0d2: startup date [Fri Apr 27 09:28:32 IST 2018]; root of context hierarchy
Apr 27, 2018 9:28:32 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Fruit(String name, String color) constructor is called by the Spring container
Fruit(String name, String color) constructor is called by the Spring container
FruitShop(String shopName, Set<Fruit> setOfAvailableFruits) constructor is called by the Spring container
---------------------------------------
Got fruitShop object from the ApplicationContext(Spring Container)
FruitShop [shopName=Fruit Mart, setOfAvailableFruits=[Fruit [name=Apple, color=Red], Fruit [name=Grapes, color=Violet]]]
In the Spring Framework, Constructor Injection is a best practice used to pass mandatory dependencies into a bean at the time of instantiation. When a bean property requires a collection of unique elements, Spring provides dedicated XML configuration elements to inject a java.util.Set directly through the constructor.
By injecting collections via constructor arguments, your beans remain immutable and fully initialized from the moment they are created by the IoC container.
Configuring <set> in Spring XML
To pass a Set to a constructor, use the <constructor-arg> tag combined with the inner <set> element. Individual values inside the set are defined using the <value> sub-element:
The Spring container automatically parses these values, constructs the appropriate Set implementation (typically a LinkedHashSet to preserve insertion order), and passes it to the corresponding constructor.
Step-by-Step Execution Workflow
XML Loading: The Spring container loads bean metadata using ClassPathXmlApplicationContext.
Constructor Matching: The container inspects the target class constructors to find a matching signature accepting a Set.
Collection Assembly: The container populates the configured items into a concrete Set collection.
Instantiation: The target bean is instantiated with the fully prepared collection injected via its constructor.
Explore More Dependency Injection Tutorials
Continue mastering Spring dependency injection with these related tutorials from Ram N Java:
public FruitShop(String shopName, Set<String> setOfAvailableFruits) { System.out.println( "FruitShop(String shopName, Set<String> setOfAvailableFruits) "
+ "constructor is called by the Spring container"); this.shopName = shopName; this.setOfAvailableFruits = setOfAvailableFruits; }
FruitShop fruitShop = (FruitShop) context.getBean("fruitShop"); System.out.println( "Got fruitShop object from the ApplicationContext(Spring Container)");
fruitShop.displayFruitShopInfo();
}
}
Output
Apr 25, 2018 9:13:55 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b37e0d2: startup date [Wed Apr 25 09:13:55 IST 2018]; root of context hierarchy
Apr 25, 2018 9:13:55 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
FruitShop(String shopName, Set<String> setOfAvailableFruits) constructor is called by the Spring container
---------------------------------------
Got fruitShop object from the ApplicationContext(Spring Container)
FruitShop [shopName=Fruit Mart, setOfAvailableFruits=[apple, pineapple, orange]]
In Spring Framework applications, configuring collections containing custom objects is a regular requirement. Using constructor injection ensures that your bean is initialized with all its required collection elements in an immutable, ready-to-use state.
In this guide, you will learn how to inject a List containing Dependent Objects into a bean through its constructor using Spring XML configuration.
1. Create the Dependent Class (Fruit)
First, define the dependent class Fruit which holds attributes such as fruit name and color:
public class Fruit {
private String fruitName;
private String color;
public Fruit(String fruitName, String color) {
this.fruitName = fruitName;
this.color = color;
}
@Override
public String toString() {
return "Fruit [fruitName=" + fruitName + ", color=" + color + "]";
}
}
2. Create the Target Class (FruitShop)
Create the target class FruitShop that accepts a List<Fruit> through its constructor:
import java.util.List;
public class FruitShop {
private String shopName;
private List<Fruit> fruitList;
public FruitShop(String shopName, List<Fruit> fruitList) {
this.shopName = shopName;
this.fruitList = fruitList;
}
public void displayFruitShopInfo() {
System.out.println("Shop Name: " + shopName);
System.out.println("Fruit Inventory:");
for (Fruit fruit : fruitList) {
System.out.println(" - " + fruit);
}
}
}
3. Configure Spring XML with <list> and <ref>
In your applicationContext.xml, declare the Fruit beans, and then reference them inside the <list> element inside <constructor-arg> using <ref bean="..." />:
publicclass Fruit { privateString name; privateString color;
public Fruit(String name, String color) { System.out.println("Fruit(String name, String color) "
+ "constructor is called by the Spring container"); this.name = name; this.color = color; }
@Override publicString toString() { return"Fruit [name=" + name + ", color=" + color + "]"; }
public FruitShop(String shopName, List<Fruit> listOfAvailableFruits) { System.out.println( "FruitShop(String shopName, List<Fruit> listOfAvailableFruits) "
+ "constructor is called by the Spring container"); this.shopName = shopName; this.listOfAvailableFruits = listOfAvailableFruits; }
FruitShop fruitShop = (FruitShop) context.getBean("fruitShop"); System.out.println( "Got fruitShop object from the ApplicationContext(Spring Container)");
fruitShop.displayFruitShopInfo();
}
}
Output
Apr 26, 2018 9:42:33 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b37e0d2: startup date [Thu Apr 26 09:42:33 IST 2018]; root of context hierarchy
Apr 26, 2018 9:42:34 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Fruit(String name, String color) constructor is called by the Spring container
Fruit(String name, String color) constructor is called by the Spring container
FruitShop(String shopName, List<Fruit> listOfAvailableFruits) constructor is called by the Spring container
---------------------------------------
Got fruitShop object from the ApplicationContext(Spring Container)
FruitShop [shopName=Fruit Mart, listOfAvailableFruits=[Fruit [name=Apple, color=Red], Fruit [name=Grapes, color=Violet]]]